CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2009
    Posts
    45

    switch statement

    I do not need the last break; statement right? Anyways, my real question is: how do I modify the following code to take an input that is letters instead of numbers?

    Code:
    import java.util.Scanner;
    public class test2
    
    {
    	public static void main (String [] args)
    		
    		{
    			Scanner kb;
    			kb = new Scanner(System.in);
    			
    			
    			int x = kb.nextInt();
    			
    			switch(x)
    			
    			{
    			case 1:
    				System.out.print("One"); break;
    			case 2:
    				System.out.print("Two"); break;
    			case 3:
    				System.out.print("Three"); break;
    			default:
    				System.out.print("Not 1,2 or 3"); break;
    			}
    }
    Any input would be greatly appreciated!
    Thanks in advance!
    Last edited by s3a; September 13th, 2009 at 05:28 PM.

  2. #2
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    Re: switch statement

    No, you don't need that last break, but there is no harm in having it. In this case (default) it is completely unnecessary, but if you had a switch with no default it is good practice to have that last break in case you later extend it with more cases.

    Regarding your real question, just use nextString().

    And a piece of advice, read the APIs. I gave you the URL to the Scanner API documentation in response to another of your posts. Stop posting one question for each method you stumble upon and read the documentation first. That will save you (and us) a lot of time.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured