2013-11-26 2 views
-1

Мой код не компилируется, и моя самая большая проблема заключается в том, что строка 32 компилируется, а 42 нет, а методы, из которых они происходят, написаны точно так же. Сообщение об ошибке - ошибка не может найти символ.Проблема с вызовами методов из других классов

import java.util.*; 
import java.text.*; 

public class CourseApp{ 
    public static void main(String[] args){  
     Scanner s = new Scanner (System.in); 
     DecimalFormat df = new DecimalFormat("$0.00"); 

     int initialSize; 
     boolean q = true; 


     System.out.println("Enter how many courses you would like to enter info rmation for? "); 
     try{initialSize = s.nextInt();} 
     catch(NumberFormatException sonic){ 
      while(initialSize <= 0){ 
       System.out.println("Please enter an integer greater than 0 (ie5). "); 
       initialSize = s.nextInt();}} 
     ArrayList <Course> courseArrayList = new ArrayList<Course> (initialSize);  

     String number=""; 
     String name=""; 
     String instr=""; 
     String text=""; 

     for (int i = 0; i < initialSize; i++){ 
      courseArrayList.add(new Course(number, name, instr, text)); 
      System.out.println("Enter in course information. "); 
      do{ 
       courseArrayList.get(i).setNumber(""); 
       System.out.println("Please enter the course number: "); 
       courseArrayList.get(i).setNumber(s.nextLine()); 
      }while(courseArrayList.get(i).getNumber().equals("")); 
      do{ 
       courseArrayList.get(i).setName(""); 
       System.out.println("Please enter the course name: "); 
       courseArrayList.get(i).setName(s.nextLine()); 
      }while(courseArrayList.get(i).getName().equals("")); 
      do{ 
       courseArrayList.get(i).setLastName(""); 
       System.out.println("Please enter the Instructor's last name: "); 
       courseArrayList.get(i).setLastName(s.nextLine()); 
      }while(courseArrayList.get(i).getLastName().equals("")); 
      do{ 
       courseArrayList.get(i).setFirstName(""); 
       System.out.println("Please enter the Instructor's first name: "); 
       courseArrayList.get(i).setFirstName(s.nextLine()); 
      }while(courseArrayList.get(i).getFirstName().equals("")); 
      do{ 
       courseArrayList.get(i).setUserName(""); 
       System.out.println("Please enter the Instructor's user name: "); 
       courseArrayList.get(i).setUserName(s.nextLine()); 
      }while(courseArrayList.get(i).getUserName().equals("")); 
      String[] instrr = new String[3]; 
       instrr[0]=courseArrayList.get(i).getLastName()+", "; 
       instrr[1]=courseArrayList.get(i).getFirstName()+"\n"; 
       instrr[2]=courseArrayList.get(i).getUserName()+"@K-State.ksu"; 
      instr = instrr[0]+instrr[1]+instrr[2]; 
      System.out.println("Please enter the required text book's title: "); 
      courseArrayList.get(i).setTitle(s.nextLine()); 
      System.out.println("Please enter the required text book's author: "); 
      courseArrayList.get(i).setAuthor(s.nextLine()); 
      System.out.println("Please enter the required text book's price: "); 
      try{courseArrayList.get(i).setPrice(s.nextDouble());} 
      catch(NumberFormatException shadow){ 
       while(courseArrayList.get(i).setPrice(s.nextDouble()) < 0){ 
        System.out.println("Please enter a positive numerical value. "); 
        courseArrayList.get(i).setPrice(s.nextDouble());}} 
      String[] textt = new String[3]; 
       textt[0]=courseArrayList.get(i).getTitle()+"\n"; 
       textt[1]=courseArrayList.get(i).getAuthor()+"\n"; 
       textt[2]=df.format(courseArrayList.get(i).getPrice())+"\n"; 
      text = textt[0]+textt[1]+textt[2];} 

      for (int i = 0; i < initialSize; i++){ 
       System.out.println("Press enter to display each of the courses"); 
       s.nextLine(); 
       courseArrayList.get(i).toString();} 

      System.out.println("Please enter a course number"); 
      String a = s.nextLine(); 
      for (int i = 0; i < initialSize; i++){ 
       if (a.equals(courseArrayList.get(i).getNumber())){ 
        courseArrayList.remove(i);} 
       else if (! a.equals(courseArrayList.get(initialSize-1).getNumber())){ 
        do { 
         System.out.println("Course number not found."); 
         System.out.println("Please enter a valid course number: "); 
         a = s.nextLine(); 
         for (int j = 0; j < initialSize; j++){ 
          if (a.equals(courseArrayList.get(j).getNumber())){ 
           q=false; 
           courseArrayList.remove(j);}} 
        } while (q=true);} 
       else {}} 

      for (int i = 0; i < initialSize; i++){ 
       System.out.println("Press enter to display each of the courses"); 
       s.nextLine(); 
       courseArrayList.get(i).toString();}}} 

это из другого класса и эта часть не составляет

public void setLastName(String lname){ 
    lastName=lname;} 

класса инструктора:

public class Instructor 
{ 
    private String lastName;  // Last name 
    private String firstName; // First name 
    private String userName;  // Username ID 

    public Instructor(String lname, String fname, String un){ 
     lastName = lname; 
     firstName = fname; 
     userName = un;} 

    public Instructor(Instructor object2){ 
     lastName = object2.lastName; 
     firstName = object2.firstName; 
     userName = object2.userName;} 

    public void setLastName(String lname){ 
     lastName=lname;} 

    public String getLastName(){ 
      return lastName;} 
    public void setFirstName(String fname){ 
     firstName=fname;} 
    public String getFirstName(){ 
     return firstName;} 
    public void setUserName(String un){ 
      userName=un;} 
    public String getUserName(){ 
     return userName;} 

    public String toString() 
    { 
     return str; 
    } 
} 

это из другого класса и эта часть компилируется

public void setName(String name){ 
    courseName=name;} 

класс курс:

public class Course 
    { 
     private String courseNumber; // e.g. CIS 200 
     private String courseName;  // e.g. Programming Fundamentals 
     private Instructor instructor; // Course instructor (object) 
    private TextBook textBook;  // Required Course textbook (object) 

    public Course(String number, String name, String instr, String text){ 
     courseNumber = number; 
     courseName = name; 
     instructor = new Instructor(instr); 
     textBook = new TextBook(text);} 

    public String getName(){ 
     return courseName;} 

    public void setName(String name){ 
     courseName=name;} 

    public String getNumber(){ 
     return courseNumber;} 

    public void setNumber(String number){ 
     courseNumber=number;} 

public Instructor getInstructor(){return new Instructor(instructor);} 

    /**getTextBook method 
    @return A reference to a copy of this course's TextBook object.*/ 

    public TextBook getTextBook(){return new TextBook(textBook);} 

    /**toString method 
    @return A string containing the course information.*/ 

    public String toString(){ 
     String str = courseNumber + " " + courseName+ "\n"+ 
     instr+"\n" + 
     text; 
     return str;}} 

И, наконец, класс TextBook:

import java.text.*; 
import java.util.*; 

public class TextBook 
{ 
    DecimalFormat df = new DecimalFormat("$0.00"); 
    private String title; // Title of the book 
    private String author; // Author's last name 
    private double price; // Wholesale cost of the book 

    public TextBook(String t, String a, double p){ 
     title = t; 
     author = a; 
     price = p;} 

    public TextBook(TextBook object2){ 
     title = object2.title; 
     author = object2.author; 
     price = object2.price;} 

    public void setTitle(String t){ 
     title=t;} 
    public void setAuthor(String a){ 
     author=a;} 
    public void setPrice(String p){ 
     price=p;} 

    public void getTitle(){ 
     return title;} 
    public void getAuthor(){ 
     return author;} 
    public void getPrice(){ 
     return price;} 

    public String toString(){ 
String str = "Required Textbook: \n" + "  " + title+", " + author + ", " + df.format(price); 
      return str;}} 

почему .setLastName() не работает, но .setName() делает

+1

@peeskillet: Это не кажется правильным. На исходный плакат, пожалуйста, поделитесь своим кодом курса курса. –

+0

Как мы можем помочь, если вы даже не сообщите нам, что такое сообщение об ошибке при сбое компиляции? – gerrytan

+2

Какое сообщение об ошибке? –

ответ

0

В своем классе, который имеет этот

public void setLastName(String lname){ 
    lastName=lname; 
} 

Убедитесь lastName существует как переменный класс. Я предполагаю, что это должно быть так:

private String lastName; 

Если вы разместите код Course.java, мы все были бы лучше подготовлены, чтобы объяснить, что может отсутствовать.

Смежные вопросы