?
Solved

Why does this loop?

Posted on 2003-02-27
2
Medium Priority
?
179 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month8 days, 14 hours left to enroll

764 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