Results 1 to 4 of 4
  1. #1
    sstokes76 is offline Member
    Join Date
    Feb 2011
    Posts
    1
    Rep Power
    0

    Default Java Method Problems

    Hi All,

    I am having an issue with a first homework assignment. The assignment is essentially complete with the exception of one issue with the use of Java methods nextInt() nextLine() and next()

    Our assignment was to create a Mock GradeBook for two courses that upon user input displays a Course Name, Section Number, and Instructor Name, while also validating to make sure students haven't signed up for two identical courses and section numbers. The program GradeBookTest works off of GradeBook. So, compiling GradeBookTest will compile both programs.

    There seems to be an issue that persists where my code will skip over the second Section Number and move right to the second instructor. I don't know how to fix this (get the program to stop skipping over the 2nd section number). It seems to be some interplay between nextInt() nextLine() and next() that is causing this. If I only use nextLine() and nextInt() the problem exists. If I use only next() and nextInt() the problem exists. If i use a mixture of all three, the problem still exists.

    Any help in fixing this issue would be much appreciated.

    Here is the GradeBook class code:

    // GradeBook class that contains a courseName 3 instance variables
    // and 2 methods to set and get its values.

    public class GradeBook
    {
    private String courseName; // course name for this GradeBook
    private int sectionNumber; // section number for this GradeBook
    private String instructorName; // instructor name for this GradeBook

    // method to set the course name
    public void setCourseName( String name )
    {
    courseName = name; // store the course name
    } // end method setCourseName

    // method to set the section number
    public void setSectionNumber( int number )
    {
    sectionNumber = number; // store the section number
    } // end method setSectionNumber

    // method to set the instructor name
    public void setInstructorName( String instructor )
    {
    instructorName = instructor; //store the instructor name
    } // end method setInstructorName

    // method to retrieve the course name
    public String getCourseName()
    {
    return courseName;
    } // end method getCourseName

    // method to retrieve the section number
    public int getSectionNumber()
    {
    return sectionNumber;
    } // end method getSectionNumber

    // method to retrieve the instructor name
    public String getInstructorName()
    {
    return instructorName;
    } // end method getInstructorName

    // display a Course Name, Section Number, and Instructor Name to the GradeBook user
    public void displayMessage()
    {
    // calls getCourseName to get the name of the course this GradeBook represents
    System.out.printf( "Welcome to %s",
    getCourseName() );

    // calls getSectionNumber to get the section number the GradeBook represents
    System.out.printf( " Section %s",
    getSectionNumber() );

    // calls getInstructorName to get the name of the instructor for this GradeBook
    System.out.printf( " taught by Professor %s!\n",
    getInstructorName() );


    } // end method displayMessage
    } // end class GradeBook


    Here is the GradeBookTest code:

    // Creating and manipulating two GradeBook objects.

    import java.util.Scanner; // program uses Scanner

    public class GradeBookTest
    {
    // main method begins program execution
    public static void main( String[] args )
    {
    // create Scanner to obtain input from command window
    Scanner input = new Scanner( System.in );

    // create a GradeBook object and assign it to myGradeBook
    GradeBook myGradeBook1 = new GradeBook();
    GradeBook myGradeBook2 = new GradeBook();

    // prompt for and read course name

    System.out.println( "Please enter course name:" );

    String theName1 = input.nextLine(); // read a line of text
    myGradeBook1.setCourseName( theName1 ); // set the course name
    System.out.println(); // outputs a blank line

    // prompt for and read section number

    System.out.println( "Please enter section number:" );


    int theSection1 = input.nextInt(); // read a line of text
    myGradeBook1.setSectionNumber( theSection1 ); // set the section number
    System.out.println(); // outputs a blank line

    // prompt for and read instructor name

    System.out.println( "Please enter Instructor's name:" );

    String theInstructor1 = input.next(); // read a line of text
    myGradeBook1.setInstructorName( theInstructor1 ); // set the instructor name
    System.out.println(); // outputs a blank line



    // prompt for and read course name

    System.out.println( "Please enter course name:" );

    String theName2 = input.next(); // read a line of text
    myGradeBook2.setCourseName( theName2 ); // set the course name
    System.out.println(); // outputs a blank line



    // prompt for and read section number

    System.out.println( "Please enter section number:" );

    int theSection2 = input.nextInt(); // read a line of text
    myGradeBook2.setSectionNumber( theSection2 ); // set the section number
    System.out.println(); // outputs a blank line



    // prompt for and read instructor name

    System.out.println( "Please enter Instructor's name:" );

    String theInstructor2 = input.next(); // read a line of text
    myGradeBook2.setInstructorName( theInstructor2 ); // set the instructor name
    System.out.println(); // outputs a blank line


    // display welcome message after specifying course name, section number, and instructor name
    myGradeBook1.displayMessage();


    if (theSection1 == theSection2 && theName1.equals(theName2))
    System.out.println( "ERROR. You registered twice to the same course section");

    else if (theSection1 != theSection2 && theName1.equals(theName2))
    System.out.println( "ERROR. You registered to two sections of the same course");

    else myGradeBook2.displayMessage();


    } // end main
    } // end class GradeBookTest

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Use Code Tags while posting your code. That would make it properly readable to all of us...

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I haven't read the code closely. (It's a bit hard to follow. Try posting with code tags.)

    Scanner can be tricky and you would do well to read the Scanner API docs for the methods you are using. Pay attention both to what these methods return and to where you end up looking at within the stream.

    Another way to see what's what is to use System.out.println() to display the things that the scanner method has returned (including things like empty strings that may surprise you).

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,512
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by sstokes76 View Post
    Hi All,

    I am having an issue with a first homework assignment. The assignment is essentially complete with the exception of one issue with the use of Java methods nextInt() nextLine() and next()

    Our assignment was to create a Mock GradeBook for two courses that upon user input displays a Course Name, Section Number, and Instructor Name, while also validating to make sure students haven't signed up for two identical courses and section numbers. The program GradeBookTest works off of GradeBook. So, compiling GradeBookTest will compile both programs.

    There seems to be an issue that persists where my code will skip over the second Section Number and move right to the second instructor. I don't know how to fix this (get the program to stop skipping over the 2nd section number). It seems to be some interplay between nextInt() nextLine() and next() that is causing this. If I only use nextLine() and nextInt() the problem exists. If I use only next() and nextInt() the problem exists. If i use a mixture of all three, the problem still exists.

    Any help in fixing this issue would be much appreciated.
    I didn't read all of your code either but some parts attracted my attention. If you're reading an int (nextInt()) or a String (next()) note that those methods don't read the end-of-line character sequence from the input. A following call to get a nextLine() happily consumes that character sequence 'thinking' that an empty line was read. Check this.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Method problems
    By Turismo in forum New To Java
    Replies: 8
    Last Post: 07-18-2010, 06:57 AM
  2. problems to find the main method
    By christina in forum Eclipse
    Replies: 2
    Last Post: 08-06-2007, 07:51 PM
  3. problems with replace method
    By cecily in forum New To Java
    Replies: 1
    Last Post: 08-02-2007, 09:11 PM
  4. Problems with method
    By ai_2007 in forum Advanced Java
    Replies: 1
    Last Post: 06-28-2007, 06:49 PM
  5. Problems with Find method in EJB
    By Nick15 in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 05-14-2007, 01:29 PM

Posting Permissions

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