0
import java.io.*;
public class inputting {
/**
* @param args
* @throws IOException 
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("number??");
String str;
     int i=0;
    while (i<5) {
            str=br.readLine();
            int n = Integer.parseInt(str);
            System.out.println(n);
        i++;}
}

}

if i want to read 5 integers how do i do that? what extra code i need to write?

10
  • 3
    Think about using a "loop" such as a for loop if you know in advance how many ints you want the user to add, or a do-while loop if you don't. Also, you might want to consider formatting that code with appropriate indentations, cowboy. Commented Aug 18, 2011 at 1:39
  • On the same line or different lines? Commented Aug 18, 2011 at 1:39
  • @Amir Raminfar what edits do i need on this code.? Commented Aug 18, 2011 at 1:41
  • @Karan: the edits you need are to use a loop as has been suggested many many times in the answers below and in my comment above. Give it a try, you won't be disappointed. Commented Aug 18, 2011 at 1:50
  • @Hovercraft Full Of Fuels it says str cannot be resolved, i guess i need to define str before or something else? Commented Aug 18, 2011 at 1:52

3 Answers 3

3

You should always use a Java Scanner to read inputs.

To your existing code, assuming String str = br.readLine(); makes str contain the line of at least one integer, eg. "10 20 30 40 50"

What you need to do is:

Scanner sc = new Scanner(str);
While (sc.hasNext())
{
   System.out.println(sc.nextInt());
   // ...or assign it to an array elment...your choice!
}
Sign up to request clarification or add additional context in comments.

6 Comments

i know but how do i use the loops for dividing that string into integers?
@Karan: that comment makes no sense. If you're using Scanner, the input already is in int form.
It's called string tokenisation, and that's precisely the whole point of the Java Scanner class - to abstract away the work for you.
@Hovercraft Full Of Eels i am not using scanner, using the same code.
@evandrix how do i tokenize it.. plz help?
|
0

If you want to ask multiple times the question number?? you just need to use a for or while loop around these three lines:

System.out.println("number??");
String  str = br.readLine();
int n = Integer.parseInt(str);

and add the read numbers to a list so you'd just need to change the last line.

Comments

0

wrap your readline in a while loop that checks for user input to quit

        while ( !(str = br.readLine()).equalsIgnoreCase("q")) {
            int n = Integer.parseInt(str);
            System.out.println(n);
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.