Results 1 to 3 of 3
  1. #1
    Romally is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default Need help with displayMessage

    I've been reading this book called "Java How to Program 7th Edition". Its a very good book, its about the only one I can understand. I'm following along with the exercises the book gives me, and I decided to play around a little bit at the end of one of the chapters. I can't understand why the displayMessage is displaying values for a primary input.

    Here is the Main:

    Java Code:
    // Main application for practicing
    
    // Import
    
    import java.util.Scanner;
    
    
    public class Main { 
    
    	
    	// Begin Main Method
    	
    	
    	public static void main(String[] args) {
    		
    		// Scanner Listen set variable
    		
    		Scanner input = new Scanner( System.in);
    		
    		// Class object create from GradeBook
    		
    		GradeBook myGradeBook = new GradeBook( "Test");
    		GradeBook myGradeBook2 = new GradeBook( "Test2");
    		GradeBook myGradeBook3 = new GradeBook( "Test3");
    		
    		//Begin display initial
    		
    		System.out.printf( "Initial course name is : %s\n",
    				myGradeBook.getCourseName() );
    		System.out.printf( "Initial course name is : %s\n",
    				myGradeBook2.getCourseName() );
    		System.out.printf( "Initial course name is : %s\n",
    				myGradeBook3.getCourseName() );
    		//End display initial
    		
    		// Display string and wait for input
    		
    		System.out.println( "Please enter your course name:");
    		String nameOfCourse = input.nextLine(); // Input prompt
    		myGradeBook.setCourseName( nameOfCourse ); // Store name
    		
    		System.out.println( "Please enter your course name:");
    		String nameOfCourse2 = input.nextLine(); // Input prompt
    		myGradeBook2.setCourseName( nameOfCourse ); // Store name
    		
    		
    		System.out.println( "Please enter your course name:");
    		String nameOfCourse3 = input.nextLine(); // Input prompt
    		myGradeBook3.setCourseName( nameOfCourse ); // Store name
    		
    		
    		// Call GradeBook method
    		
    		myGradeBook.displayMessage();
    		myGradeBook2.displayMessage();
    		myGradeBook3.displayMessage();
    		
    		
    		
    
    	}
    
    }
    And here is my secondary class file:

    Java Code:
    // Gradebook method
    
    
    
    public class GradeBook 
    
    {
    	 // Creates variable courseName
    	
    	private String courseName; // course name for this GradeBook
    	
    	// Stores default variable for GradeBook based on Main method's new GradeBook's parameters
    	
    	public GradeBook( String name )
    	
    	{
    		courseName = name;
    	}// end method
    	
    	// Store course name
    	
    	public void setCourseName( String name )
    		
    	{
    		courseName = name; // Gets course name input from Main and stores it
    	} // end method
    
    	// Get course name
    	
    	public String getCourseName()
    	
    	{
    		return courseName; //Returns course name to Main
    	}// end method
    
    	//Display Welcome Message
    	
    	public void displayMessage()
    	
    	{
    		System.out.printf( "Welcome to the grade book for %s!\n",
    				getCourseName() );
    	}// end method
    	
    	
    	
    } // End Class GradeBook
    Now I've triple checked the instances I created to make sure they're assigning values to the variables they contain, this should be correct. Near the end of the program, it doesn't display 3 different values, instead, it displays the value of the first input from the user. What am I doing wrong?

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    Java Code:
    		System.out.println( "Please enter your course name:");
    		String nameOfCourse2 = input.nextLine(); // Input prompt
    		myGradeBook2.setCourseName( nameOfCourse ); // Store name
    		
    		
    		System.out.println( "Please enter your course name:");
    		String nameOfCourse3 = input.nextLine(); // Input prompt
    		myGradeBook3.setCourseName( nameOfCourse ); // Store name
    myGradeBook3.setCourseName( nameOfCourse );

    nameOfCourse != nameOfCourse2 != nameOfCourse3 :)

  3. #3
    Romally is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

Posting Permissions

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