• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Issue with the way my program is processing input and output

 
Ranch Hand
Posts: 186
1
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 186
    1
    Netbeans IDE Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Ranch Hand
    Posts: 954
    4
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 186
    1
    Netbeans IDE Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Marshal
    Posts: 79240
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
    reply
      Bookmark Topic Watch Topic
    • New Topic