Reputation: 22956
i have to read from the following format.
1
12
23
34
So, All inputs are separated by a line.
I have tried the following
br = new Scanner(System.in);
num = Integer.parseInt(br.next());
while(br.hasNextInt()) {
num = br.nextInt() ;
System.out.println(num);
}
But it is not working as i expected. If i enter first input, it started processing it and prints. it is not waiting for me to enter next line. In C, i can make use of sscanf. but in java i have no idea how to allow user to enter multiline input? plese suggest some ideas
Upvotes: 3
Views: 358
Reputation: 5109
br = new BufferedReader(new InputStreamReader(System.in));
int ch;
while ((ch = br.read()) != -1) {
if (ch == 10) {
System.out.println();
}
}
Upvotes: 0
Reputation: 71
Scanner scanner = new.scanner(System.in);
String abc = scanner.next();
int a = scanner.nextInt();
scanner.close();
Upvotes: 0
Reputation: 136
Your question isn't entirely clear; however if you're trying to get the user input- then print it later, you can save the inputs to an ArrayList.
import java.util.ArrayList;
import java.util.Scanner;
public class tu {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter numbers, when you're done, enter a letter.");
char firstLetter = '*';
ArrayList<String> al = new ArrayList<>();
while(!Character.isLetter(firstLetter)) {
String input = "";
boolean nullInput = false;
do{
if(nullInput)
System.out.println("Error; you can't enter an empty string.");
nullInput = false;
input = in.nextLine();
if(input.length() == 0)
nullInput = true;
}while(nullInput);
firstLetter = input.charAt(0);
al.add(input);
}
al.remove(al.size()-1);
for(int i = 0; i < al.size(); i++) {
System.out.println(); //creates newline in console
System.out.println(al.get(i));
}
}
}
Upvotes: 0
Reputation: 8849
if you willing to read line by line . This code read line by line and exits when you input a empty line
br = new Scanner(System.in);
int num=0;
String input;
input = br.nextLine();
while(input.length()!=0) {
num = Integer.parseInt(input);
System.out.println(num);
input = br.nextLine();
}
Upvotes: 0
Reputation:
You must check for next available input and then get the input
br = new Scanner(System.in);
//num = Integer.parseInt(br.next());//remove this line
while(br.hasNextInt()) {//if number is avaialable
num = br.nextInt(); //get that number
System.out.println(num);
}
Below is sample code:
import java.util.Scanner;
class Ideone
{
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
while(sc.hasNextInt())
System.out.printf("input was: %d\n",sc.nextInt());
sc.close();
}
}
Upvotes: 1
Reputation: 112
It's not entirely obvious what you require , but this code ( below ) will allow you to read and store multiple lines of input , and then print them all when you're finished .
systemInScanner = new Scanner ( System.in ) ;
ArrayList < String > arrayListOfStrings = new ArrayList < > ( ) ;
while ( systemInScanner . hasNextLine ( ) )
{
arrayListOfStrings . add ( systemInScanner . NextLine ( ) ) ;
}
for ( String line : input )
{
System . out . println ( line ) ;
}
Upvotes: 0
Reputation: 31
Try
br = new Scanner(System.in);
while (true) {
int num = Integer.parseInt(br.nextLine());
System.out.println(num);
}
Upvotes: 1
Reputation: 2006
br = new Scanner(System.in);
num = Integer.parseInt(br.next());
br.nextLine();
while(br.hasNextInt()) {
num = br.nextInt() ;
br.nextLine();
System.out.println(num);
}
you need to add br.nextLine();
to start to read next line.
Upvotes: 0