Results 1 to 2 of 2
  1. #1
    virwhaoo87 is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default User entering int into keyboard

    Hello everybody. I am taking an introduction java class in college and having a hard time with this assignment. We are to create a menu option allowing the user to select an option and then do whatever the option states for it to do. For example in the program below, if the user selects option 1 then he should be able to enter two numbers and then have the average of those two numbers shown on the screen. I am having a hard time getting java to except either a string or int typed in by a user. The program is posted below. This is my first ever thread so sorry if i did not post correctly. Thanks in advance for any help.

    import java.util.*;

    public class InputMenu
    {
    public void display_menu()
    {
    System.out.println(" 1) Display average of two numbers\n 2) Display the smaller of two numbers");
    System.out.println(" 3) Display the larger of two numbers\n 4) Display a message in reverse\n 5) Count the number of characters in a message");
    System.out.print("Selection: ");
    }

    public void question()
    {
    System.out.println("Would you like to proceed or quit?");
    System.out.println("To proceed enter 9.");
    System.out.println("If you wish to quit enter 0.");

    Scanner q = new Scanner(System.in);

    switch (q.nextInt())
    {
    case 0:
    System.out.println ("Thank you and godbye.");
    break;

    case 9:
    System.out.println ("Please proceed.");
    new InputMenu();
    break;

    default:
    System.err.println ( "Unrecognized option" );
    break;
    }
    }

    public InputMenu()
    {
    Scanner in = new Scanner(System.in);
    display_menu();

    switch (in.nextInt())
    {
    case 1:
    System.out.println ( "You picked option 1. Please enter two numbers." );
    question();
    break;

    case 2:
    System.out.println ( "You picked option 2. Please enter two numbers." );
    question();
    break;

    case 3:
    System.out.println ( "You picked option 3. Please enter two numbers." );
    question();
    break;

    case 4:
    System.out.println ( "You picked option 4. Please type a message." );
    question();
    break;

    case 5:
    System.out.println (" You picked option 5. Please type a message." );
    question();
    break;

    default:
    System.err.println ( "Unrecognized option" );
    break;
    }
    }

    public static void main (String[]args)
    {
    new InputMenu();
    }
    }

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    I am having a hard time getting java to (accept) either a string or int typed in by a user.
    Try to follow a particular route through your program, for example, if i enter the system this is what happens:
    1) InputMenu() is called
    2) InputMenu sets up a Scanner and calls display_menu()
    3) display_menu() prints out 5 options for the user and we go back to InputMenu
    4) The switch statement is called, depending on scanner input number a String message is printed and question() is called
    5) question() prints out 3 options and sets up a new scanner, then has a 2nd switch option using integer input from the user again
    6) The second switch statement prints out more options to exit or run the system again

    In which part of this process did you plan on asking the user for a string input?

    My suggestion is that before question() is called to give the user the option to exit the system or re-run, you should make a scanner and take user input there.
    Last edited by ozzyman; 03-14-2011 at 10:03 PM.

Similar Threads

  1. Stop user entering letters in GUI, numbers only
    By africanhacker in forum New To Java
    Replies: 3
    Last Post: 03-11-2011, 02:25 PM
  2. Entering a number/name and getting a response
    By coding in forum Advanced Java
    Replies: 3
    Last Post: 02-01-2011, 03:15 PM
  3. entering textfile into database
    By UJJAL DHAR in forum New To Java
    Replies: 11
    Last Post: 06-15-2010, 02:55 AM
  4. Entering a space as argument
    By Arne Bjarne in forum Eclipse
    Replies: 1
    Last Post: 03-02-2009, 01:43 PM
  5. logic to restrict the user from entering the password morethan thrice?
    By kavitha_164951 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-12-2008, 03:18 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
  •