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: While Loop Problem :(

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post While Loop Problem :(

    Hi....can any one please tell me where is the problem in my code? Actually a very simple thing i am trying to do here. User will enter 1/-1 from console. If the user enters -1 then the program will terminate and if the user enters 1 then some other activities will start......i am using the while loop as follows but that is not working:

    in my MAIN method i have written:

    int choice;
     
    choice = DisplayMenu();
     
            while (choice!=-1){
     
            	System.out.println("\nI am inside while loop !\n");
     
            }          
       System.out.println("\nI am outside while loop ! Program ends...\n");

    And in my DISPLAYMENU method i have written:

    public static int DisplayMenu()        
    {
        int choice = 0;
        System.out.println("\t\t\nPlease choose your option to deal with this system\n\t'1' -> To go into the while loop\n\t'-1' -> To terminate the program\n");
    	choice = (int)console.next().indexOf(choice);
        return choice;
    }

    It is urgent....really i will be appreciated if any one can help me to run that while loop correctly and inform me where i was doing mistake...

    Regards


  2. #2
    Member
    Join Date
    May 2010
    Posts
    36
    Thanks
    0
    Thanked 13 Times in 12 Posts

    Default Re: While Loop Problem :(

    try this

    import java.io.Console;
     
    public class DisplayMenu {
     
    	public static int DisplayMenu() {
    		int choice = 0;
    		Console cons = System.console();
    		System.out
    				.println("\t\t\nPlease choose your option to deal with this system\n\t'1' -> To go into the while loop\n\t'-1' -> To terminate the program\n");
    		String line = cons.readLine();
    		Integer.parseInt(line);
    		return choice = Integer.parseInt(line);
    	}
     
    	public static void main(String[] args) {
    		DisplayMenu menu = new DisplayMenu();
     
    		int choice;
     
    		choice = menu.DisplayMenu();
     
    		while (choice != -1) {
     
    			System.out.println("\nI am inside while loop !\n");
     
    		}
    		System.out.println("\nI am outside while loop ! Program ends...\n");
    	}
    }

    don't start this code inside an ide but only from a os-box with java DisplayMenu

  3. The Following 2 Users Say Thank You to j2me64 For This Useful Post:

    faisalnet5 (May 10th, 2010), JavaPF (May 10th, 2010)

  4. #3
    Junior Member
    Join Date
    May 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs up Re: While Loop Problem :(

    Thanks a lot j2me64...Here i solved that problem in this way:

    In the MAIN method i had written:

    int choice;
     
    choice = DisplayMenu();
     
            while (choice != -1){   	
            	System.out.println("\nI am inside while loop !\n"); 	
            }      
       System.out.println("\nThanks for using this program !\n");

    And in the DISPLAYMENU method i had written:

    public static int DisplayMenu()        // User-defined method to display a menu
    {
        int choice;
        System.out.println(" Please select how you want to deal with the system\n '1' -> Go inside of the while loop\n '-1' -> Go outside of the while loop...Program Ends !\n");
     
        Scanner in = new Scanner(System.in);
        choice=in.nextInt();
     
        return choice;
    }

    And this is running exactly like the way i want too.......

Similar Threads

  1. Problem with loop theory
    By Lord eMO in forum Loops & Control Statements
    Replies: 0
    Last Post: February 11th, 2010, 02:34 PM
  2. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 05:27 PM
  3. Little Loop Problem
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: October 17th, 2009, 05:40 AM
  4. [SOLVED] Array loop problem which returns the difference between the value with fixed value
    By uplink600 in forum Loops & Control Statements
    Replies: 5
    Last Post: May 15th, 2009, 05:31 AM
  5. [SOLVED] Java for loop problem and out put is not coming
    By thewonderdude in forum Loops & Control Statements
    Replies: 9
    Last Post: March 15th, 2009, 03:31 PM

Tags for this Thread