Results 1 to 6 of 6
  1. #1
    javanoob123 is offline Member
    Join Date
    Feb 2017
    Posts
    7
    Rep Power
    0

    Default Printing from a toString stored in another class.

    I am trying to print the details from a
    Java Code:
    toString
    stored in Student class in Main class.
    The code in the Main class is:

    Java Code:
    public class JavaLecture3 {
    public static final int DEBUG = 0;
     
    public static void main(String [] args){
     
        //Create course object
        Course course  = new Course("CSE", "Tom", 5);
        Scanner scanner = new Scanner(System.in);
        String cmd = "yes";
         
     
        while(cmd.equals("yes")){
           Student student = new Student();
     
           System.out.print("Do You want to add a new student? ");
           cmd = scanner.next();
     
           if (cmd.equals("yes")){
               //Read student name
               System.out.print("Enter a student name: ");
               String name = scanner.next();
               student.setName(name);
     
               //Read student Age
               System.out.print("Enter a student age: ");
               int age = scanner.nextInt();
               student.setAge(age);
     
               //Read student Course
               System.out.print("Enter a student course: ");
               String stdent_course = scanner.next();
               student.setCourse(stdent_course);
                
               //Read student Phone Number
               System.out.print("Enter student phone number: ");
               String stdent_phoneNo = scanner.next();
               student.setPhoneNo(stdent_phoneNo);
                
                   }
        }
         
     
        scanner.close();
     
        System.out.println("Course Information: ");
        System.out.println("-------------------");
        System.out.println(course.toString());
        System.out.println();
        System.out.println("Student Information: ");
        System.out.println("-------------------");
        System.out.println(student.toString());
        System.out.println();
    }
    }
    When I try the above code I get the error saying cannot find symbol variable student.
    Please help.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Printing from a toString stored in another class.

    Your student variable only exists inside the while loop.

    Or, more likely, your code that prints student should probably be inside the while loop.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Printing from a toString stored in another class.

    What is the relationship between a Course object and a Student object? Would the Course object have a list of Students that are taking the course?
    Would the toString() method of Course include a list of Students taking the course?
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Printing from a toString stored in another class.

    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Printing from a toString stored in another class.

    This is also over on Code Ranch, along with another thread that highlights what I think you might be getting at, Norm.

    The students aren't getting added to the course, essentially.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Printing from a toString stored in another class.

    Thanks. Looks like the OP is getting good help on the other site
    and is wasting our time here.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Where in memory does a class get stored?
    By allDogsAreGood in forum New To Java
    Replies: 6
    Last Post: 01-08-2017, 06:13 PM
  2. Replies: 2
    Last Post: 09-09-2012, 12:30 AM
  3. Help with the tostring class
    By grim in forum New To Java
    Replies: 1
    Last Post: 02-04-2012, 02:42 AM
  4. Printing my toString method
    By Epidilius in forum New To Java
    Replies: 4
    Last Post: 06-18-2011, 03:32 AM
  5. Class Instances stored in an ArrayList
    By Ersk in forum New To Java
    Replies: 4
    Last Post: 12-12-2009, 04:13 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •