?
Solved

Why does this loop?

Posted on 2003-02-27
2
Medium Priority
?
182 Views
Last Modified: 2010-03-31
I'm trying to get user input from the console, which sort of works.  What is wierd is that it loops three times after the first input is collected.  It is intended to loop until user inputs "X".

What causes the looping and what do I do to stop it?



class ArrayExample {
     static BufferedReader stdin =
               new BufferedReader(new InputStreamReader(System.in));


 static void main(String args[]) throws IOException {
          int[] iargs = new int[args.length];
          char uInp = 0;
          getInput inp = new getInput();
          char cInput;
          System.out.print("[");

     for (int i = 0; i < iargs.length - 1; i++) {
          iargs[i] = Integer.parseInt(args[i]);
               System.out.print(iargs[i] + ",");
          }

     System.out.println(args[args.length - 1] + "]");
          ArrayManip cManip = new ArrayManip(iargs);
          while (uInp != 88) {
         
               System.out.println("Not an X");
               
               uInp = inp.userInput();
                      System.out.println(uInp);
               switch (uInp) {
                         case 65:
                                            System.out.println(uInp);
               }
          }

     }

}






class getInput {

     public char userInput() throws IOException {
         
     System.out.println("what do you want to do? ");
     System.out.println("A reverse the array ");
     System.out.println("B double the array ");
     System.out.println("C square the array ");
     System.out.println("D sum the array ");
     System.out.print("X exit    ");
     byte[] data = new byte[1];
     System.in.read(data);
     System.out.println("");
     System.out.println("");
     System.out.println("");          
     return Character.toUpperCase((char) data[0]);
     }

}
0
Comment
Question by:dowland
  • 2
2 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 8038666
My somewhat hazy explanation for this is the the call to System.in.read was not blocking as one might expect, so i sorted out the problem by using your 'stdin' instead of System.in.read. I've not altered anything much otherwise, apart from commenting out your ArrayManip class which is not present so it could compile:

import java.io.*;

public class ArrayExample {
    static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));


public static void main(String args[]) throws IOException {
         int[] iargs = new int[args.length];
         char uInp = 0;
         getInput inp = new getInput();
         char cInput;
         System.out.print("[");

    for (int i = 0; i < iargs.length - 1; i++) {
         iargs[i] = Integer.parseInt(args[i]);
              System.out.print(iargs[i] + ",");
         }

    System.out.println(args[args.length - 1] + "]");
         //ArrayManip cManip = new ArrayManip(iargs);
         while ((uInp = inp.userInput()) != 'X') {

              switch (uInp) {
                 case 'A':
                   System.out.println("Reversing...");
                   break;
                 case 'B':
                   System.out.println("Doubling...");
                   break;
                 case 'C':
                   System.out.println("Squaring...");
                   break;
                 case 'D':
                   System.out.println("Summing...");
                   break;
                 case 'X':
                   break;
                 default:
                   break;
              }
         }

    }

}






class getInput {

    public char userInput() throws IOException {

    System.out.println("what do you want to do? ");
    System.out.println("A reverse the array ");
    System.out.println("B double the array ");
    System.out.println("C square the array ");
    System.out.println("D sum the array ");
    System.out.print("X exit    ");
    char c = ArrayExample.stdin.readLine().toUpperCase().charAt(0);
    System.out.println("");
    System.out.println("");
    System.out.println("");
    return c;
    }

}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8038671
case 'X' needn't be in the switch statement of course ;-)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month9 days, 7 hours left to enroll

621 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