Mistu4u
Mistu4u

Reputation: 5416

How to take input in Java this way?

I want to take the inputs for the length, breadth and height of my "Box" class repectively. Now I want to take it as a stream of integers and then set them individually to each of the dimension of the box. The stream will be taken as i/p until the user presses 0. So I wrote it this way (I am just mentioning the main method, I defined the box class seperately):

public static void main(String args[])
{

    System.out .print("Enter length, breadth and height->> (Press '0' to end the i/p)");
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    while((br.read())==0)
    {
        // What should I write here to take the input in the required manner?
    }
}

PS: I cannot use scanner, console or DataInputStream. So help me here with BufferedReader.

Upvotes: 1

Views: 204

Answers (3)

yilmazburk
yilmazburk

Reputation: 917

Maybe you can try this one.

public static void main(String args[])
{
    String input = 0;
    ArrayList<int> list = new ArrayList<int>();
    boolean exit = true;
    System.out.print("Enter length, breadth and height->> (Press '0' to end the i/p)");
    try {
       BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       while((input = br.readLine()!=null && exit)
       { 
         StringTokenizer t = new StringTokenizer(input);
         while(t.hasMoreToken()){
             if(Integer.parseInt(t.nextToken()) != 0){
                list.add(input);
             }
             else{
                exit = false;
             }
         }

       }
       //list contains your needs. 
    } catch(Exception ex) {
       System.out.println(ex.getMessage());
    }
}

Upvotes: 0

Paul Richter
Paul Richter

Reputation: 11072

Since you indicated you absolutely must use the BufferedReader, I believe one way to do this is to use the BufferedReader#readLine() method instead. That will give you the full line as entered by the user, up to the line termination (either a '\r' or '\n' according to the documentation).

As Zong Zheng Li already said, while the Scanner class can tokenize the input line for you, since you cannot use that, you'll have to do that yourself manually.

At this moment, one way that springs to mind is to simply split on a space (\s) character. So your code might look something like this:

System.out .print("Enter length, breadth and height->> (Press '0' to end the i/p)");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String inputLine = br.readLine(); // get user input

String[] inputParts = inputLine.split("\\s+"); // split by spaces

int width = Integer.parseInt(inputParts[0]);
int height = Integer.parseInt(inputParts[1]);
int breadth = Integer.parseInt(inputParts[2]);

Note that I'm not showing any error or range checking, or input validation, as I'm just showing a general example.

I'm sure there's plenty of other ways to do this, but this is the first idea that popped in my mind. Hope it helps somewhat.

Upvotes: 2

Zong
Zong

Reputation: 6230

Is there a particular reason you're not just using Scanner? It tokenizes and parses values for you:

Scanner sc = new Scanner(System.in);
int width = sc.nextInt();
int height = sc.nextInt();

Upvotes: 2

Related Questions