Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Issue with the way my program is processing input and output  RSS feed

 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings everyone, I'm making a student Database utilizing an ArrayList Data Structure. The program works well for the most part except for when trying to add a student, a few things get really wonky. What I mean by wonky is that as soon as it prompts for a student ID, the program immediately follows it with a prompt for a netID and does not allow the user to input a value for both. The program also ends abruptly when prompting the student for gender. Here is the output:


  • Welcome to the UI Student Directory Database!
    Please choose from the Choices below:
    1. Add a Student
    2. Remove a Student
    3. Display Student Directory
    4. Exit Database
    ---------------------------------------------
    1
    Please enter the first name of the Student:
    Dan
    Please enter the last name of the Student:
    Steward
    Please enter the major of the Student:
    CE
    Please enter the student's GPA:
    3.2
    Please enter the student's UIN:
    Please enter the Student's netID:
    876578
    Please enter the Student's age:
    30
    Please enter the Student's gender:
    ---------------------------------------------
    Welcome to the UI Student Directory Database!
    Please choose from the Choices below:
    1. Add a Student
    2. Remove a Student
    3. Display Student Directory
    4. Exit Database
    ---------------------------------------------
    m
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at Lab3.main(Lab3.java:25)



  • Here is the actual code:



    I don't see where I'm going wrong. I'm a bit flabbergasted. The addStudent method in the Lab3 class seems like it should work, but it behaves in a wonky fashion. Thanks!
     
    Naziru Gelajo
    Ranch Hand
    Posts: 175
    1
    Java Netbeans IDE Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    BTW the error that says Lab3.java line 25 should actually be line 19. I removed the comments at the beginning of the program that illustrates the author and stuff.
     
    Tushar Goel
    Ranch Hand
    Posts: 934
    4
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    the problem is with using int and then immediately after you are using nextLine.

    What happening is once you enter int value and press enter than it is consider it as an input and when you try to use nextLine() then that input  √Čnter' is consumed and goes to another line.

    See this post for more explanation.
     
    Naziru Gelajo
    Ranch Hand
    Posts: 175
    1
    Java Netbeans IDE Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Tushar Goel wrote:the problem is with using int and then immediately after you are using nextLine.

    What happening is once you enter int value and press enter than it is consider it as an input and when you try to use nextLine() then that input  √Čnter' is consumed and goes to another line.

    See this post for more explanation.


    Excellent,

    Thank you very much for that.
     
    Campbell Ritchie
    Marshal
    Posts: 55751
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Why are all the members of your class static?
    Why are you using multiple Scanner objects? You only need one Scanner object to read from the keyboard. That is why I wrote a utility class, which you will find out about in the older links.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!