Results 1 to 5 of 5

Thread: while loop

  1. #1
    Join Date
    Jun 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default while loop

    hi, Ok i am new to java programming. I am currently trying to write a new program which enters awhile loop waiting for s,g,l to be entered to e to exit the loop.

    i have been trying to with no luck.

    any help of where to start what to declare, programming need.

    using methods if that help

    thanks

  2. #2
    Join Date
    Jun 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    oh are u reading complete string at a time or you want to read charector by charector

  3. #3
    Join Date
    Jun 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    the code is as following
    public static void Main() {
    char ch;
    do
    {
    ch = (char) Console.Read(); // get a char
    Console.WriteLine(ch);
    }
    while(ch != 's' && ch != 'g' && ch != 'l');



    }

  4. #4
    Join Date
    Jun 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    public static void Main() {
    char ch;
    do
    {
    ch = (char) Console.Read(); // get a char
    Console.WriteLine(ch);
    }while(ch != 's' && ch != 'g' && ch != 'l');


    }
    }

  5. #5
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    The code posted thus far won't work, but what follows is a rough framework that will at least get you started. Hopefully.

    Code:
    public class MyApplication {
        public MyApplication() {
        }
    
        public static void main(String[] arguments) {
            MyApplication application = new MyApplication();
            application.start();
        }
    
        public void start() {
            java.io.Reader reader = new java.io.InputStreamReader(System.in);
            while (true) {
                int input;
                try {
                    input = reader.read();
                } catch (java.io.IOException ioe) {
                    System.out.println("An unexpected error occurred whilst reading input.");
                    return;
                }
    
                switch (input) {
                case 'e':
                    return;
                case 'g':
                    /* ... */
                    break;
                case 'l':
                    /* ... */
                    break;
                case 's':
                    /* ... */
                    break;
                default:
                    /* ... */
                    break;
                }
            }
        }
    }
    Mike

Bookmarks

Posting Permissions

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