Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Problem with option scanner input for a switch statement

  1. #1
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    54
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Problem with option scanner input for a switch statement

    I'm just trying to create a simple switch statement structure:

    SwitchStatement.java:
    public class SwitchStatement {
     
    	public void displayMenu() {
    		System.out.println("Select a bloodtype:\n Option 1: AB-negative\n Option 2: B-negative\n Option 3: AB-positive\n"
    				+ " Option 4: A-negative\n Option 5: O-negative\n Option 6: B-positive\n Option 7: A-positive\n Option 8: O-positive");
    	}
     
    	public String Menu() {
    		Scanner menuSelect = new Scanner(System.in);
    		String bloodtype;
    		switch(menuSelect.NextInt()) {
    		case 1:
    			bloodtype = "AB-negative";
    			System.out.println("You selected the " + bloodtype + " bloodtype.");
    			break;		
    		case 2:
    			bloodtype = "B-negative";
    			System.out.println("You selected the " + bloodtype + " bloodtype.");
    			break;
    		case 3:
    			bloodtype = "AB-positive";
    			System.out.println("You selected the " + bloodtype + " bloodtype.");
    			break;
    		case 4:
    			bloodtype = "A-negative";
    			System.out.println("You selected the " + bloodtype + " bloodtype.");
    			break;
    		case 5:
    			bloodtype = "O-negative";
    			System.out.println("You selected the " + bloodtype + " bloodtype.");
    			break;
    		case 6:
    			bloodtype = "B-positive";
    			System.out.println("You selected the " + bloodtype + " bloodtype.");
    			break;
    		case 7:
    			bloodtype = "A-positive";
    			System.out.println("You selected the " + bloodtype + " bloodtype.");
    			break;
    		case 8:
    			bloodtype = "O-positive";
    			System.out.println("You selected the " + bloodtype + " bloodtype.");
    			break;
    		default:
    			System.err.println("Invalid option. Choose an option between 1 and 8.");
    			break;
    		}
    		return bloodtype;			
    	}
    }

    Main.java:
    [CODE]

    Line 15 error: "The method NextInt() is undefined for the type Scanner"

    What am I missing?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with option scanner input for a switch statement

    Line 15 error: "The method NextInt() is undefined for the type Scanner"
    Check the spelling. java is case sensitive
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jul 2019
    Posts
    36
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default Re: Problem with option scanner input for a switch statement

    + add bloodtype initialization before return it eg in switch default:
    default:
    bloodtype = "";

Similar Threads

  1. How to turn my If statement into a case/switch statement?
    By blobby404 in forum What's Wrong With My Code?
    Replies: 23
    Last Post: June 19th, 2014, 03:11 PM
  2. How can I create an option menu with while and switch statements?
    By DABBISH in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 16th, 2013, 06:36 AM
  3. [SOLVED] Please help me with my String variable and switch statement problem.
    By ace1 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 7th, 2013, 09:15 PM
  4. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 04:19 PM
  5. Replies: 1
    Last Post: January 16th, 2013, 08:55 AM