switch statement in for loop

Posted on 2012-09-09
Last Modified: 2012-09-09
Hello I am trying to have a switch statement work inside of a for loop-
am attaching sample-
in the example - want user to be able to choose 1 or 2 -
can you see mistakes and what I am not doing to make this work?
thank you
Question by:davetough
    LVL 12

    Accepted Solution

    Your Scanner statement should be inside main().
    import java.util.Scanner;
    public class Test_Program
        public static void main(String[] args) 
            System.out.printf( "Java switch statement inside for loop" );
            //for loop      
            for ( int value = 1; value <= 10; value++ )
                System.out.printf( "Enter 1 or 2: ");
                Scanner input = new Scanner(;
                int choice = input.nextInt();
                switch (choice)
                    case 1:
                    System.out.println("Test 1");
                    case 2:
                    System.out.println("Test 2");

    Open in new window


    Author Closing Comment

    thank you

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    The article will include the best Data Recovery Tools along with their Features, Capabilities, and their Download Links. Hope you’ll enjoy it and will choose the one as required by you.
    Healthcare organizations in the United States must adhere to the guidance of both the HIPAA (Health Insurance Portability and Accountability Act) and HITECH (Health Information Technology for Economic and Clinical Health Act) for securing and protec…
    The viewer will learn how to implement Singleton Design Pattern in Java.
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now