Results 1 to 2 of 2
Thread: User entering int into keyboard
- 03-14-2011, 07:01 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 1
- Rep Power
- 0
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();
}
}
-
Try to follow a particular route through your program, for example, if i enter the system this is what happens:I am having a hard time getting java to (accept) either a string or int typed in by a user.
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 09:03 PM.
Similar Threads
-
Stop user entering letters in GUI, numbers only
By africanhacker in forum New To JavaReplies: 3Last Post: 03-11-2011, 01:25 PM -
Entering a number/name and getting a response
By coding in forum Advanced JavaReplies: 3Last Post: 02-01-2011, 02:15 PM -
entering textfile into database
By UJJAL DHAR in forum New To JavaReplies: 11Last Post: 06-15-2010, 01:55 AM -
Entering a space as argument
By Arne Bjarne in forum EclipseReplies: 1Last Post: 03-02-2009, 12:43 PM -
logic to restrict the user from entering the password morethan thrice?
By kavitha_164951 in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 08-12-2008, 02:18 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks