BufferedReader space separated input
Asked Answered
C

6

5

first I'd like to mention that I am not realy experienced in java, and I searched StackOverFlow for a solution to my problem and either I didn't find it or didn't understand the answer, so I am asking now:

i wanted to start working with BufferedReader and didn't find any guide that i understood propely, so i picked up bits from here and there and wrote this example :

BufferedReader input = new BufferedReader (new InputStreamReader (System.in));
int x = Integer.parseInt(input.readLine());
String y = input.readLine();
System.out.println(x);

this code worked for the input 34 then enter then abc, but at what im trying to achieve i need the input 34 abc separated by space to be inputed together and that x will get 34 and y will get abc. this will work when using Scanner, but the problem is Scanner times out the exercise i'm doing because it's slow.

is there any simple way to get those input space separated like it was with Scanner?

Capitalistic answered 3/6, 2015 at 8:22 Comment(3)
as i mentioned, im not really well learned in java and didn't know what the split method is, though it wasn't idle to my situation, sanira gave me a solution including StringTokenizer. i have no idea what it is but it worked perfectly, so guess i'll go with that.Capitalistic
That's interesting. Because if the string.split(" ") is not working but StringTokenizer is working, that means that 2 inputs are not separated by SPACE but by some other whitecase character. StringTokenizer is kinda equal to string.split("\\s") where \\s means any whitecase characterLorin
actually it was my mistake of understanding how split works, i usued split eventually (because of misunderstanding the tokenizer, which now works too, but guess that was the whole point, now i understand both of them)Capitalistic
B
7

Try this,

StringTokenizer tk = new StringTokenizer(input.readLine());
int m = Integer.parseInt(tk.nextToken());
String s = tk.nextToken();

this is faster than string.split();

Brookner answered 3/6, 2015 at 8:28 Comment(3)
thanks a lot! it worked. also, what is this StringTokenizer and where can i learn more about it?Capitalistic
thanks. last question though, how do i move to the the next line? like when the input is 23 sdf 35 gtr i need it to jump the line and get the next input from the line belowCapitalistic
yeah, figured it out eventually, but untill i did i solved th problem with the split method. anyway, many thanks!Capitalistic
H
0

If you want to read 2 values from the same line you can't parse the whole line to Integer. This gives NumberFormatException.

First read the line, then split it on ' ', and then parse 1st part to Integer.

String line = reader.readLine();
String[] splitLine = line.split(" ");
Integer x = Integer.parseInt(splitLine[0]);
String y = splitLine[1];
Heyde answered 3/6, 2015 at 8:27 Comment(2)
line.split will return a String[] so there will be a Type Mismatch.Gander
Thanks, fixed it.Heyde
I
0
String line = input.readLine();
String []tokens = line.split(" ");
int x = Integer.parseInt(tokens[0]);
String y = tokens[1];

Split the input with split() function and access it from the array

Inappreciative answered 3/6, 2015 at 8:29 Comment(0)
H
0

Were you not able to use String.split() function on the input.readLine()?

String line = input.readLine();
int x = Integer.parseInt(line.split(" ")[0]);

Hypertension answered 3/6, 2015 at 8:29 Comment(0)
G
0

You can read multiple integers in the same row separated by spaces like: 1 5 5; by using the following example.

StringTokenizer tk = new StringTokenizer(input.readLine());
int a = Integer.parseInt(tk.nextToken());
int b = Integer.parseInt(tk.nextToken());
int c = Integer.parseInt(tk.nextToken());
Galluses answered 14/10, 2017 at 6:13 Comment(0)
S
0

Ref: https://www.hackerearth.com/practice/codemonk/ I was trying to find a solution for getting user input for this problem, (for a n * n size space-separated 2d array, if i try to get user input using BufferedReader java class, I can use below code.)

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int t = Integer.parseInt(br.readLine());
    while (t-->0) {
        int n = Integer.parseInt(br.readLine());
        int[][] arr = new int[n][n];
        for (int i=0; i<n; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            for (int j=0; j<n; j++) {
                arr[i][j] = Integer.parseInt(st.nextToken());
            }
        }

        System.out.println(Arrays.deepToString(arr));
    }
Scholarship answered 25/6 at 3:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.