String Tokenizer giving exception while taking space saperated values using BufferedReader - java

I tried to start using BufferedReader instead of Scanner. While coding for a question on codechef (SMRSTR), I tried taking space separated inputs by using StringTokenizer but it is raising exception i.e NumberFormatException. I found some question on StackOverflow regarding it but I think my problem is different, so I posted one.
Input: 1
2 3
2 3
5 100 8
I am getting:
Exception in thread "main" java.lang.NumberFormatException: For
input string: "2 3"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at A.main(A.java:11)
I am getting first input t correctly from br.readLine();
But next inputs n,q are giving the mentioned exception. I think the problem is in the nextToken from StringTokenizer, but still not getting it clearly.
Here is the code:
import java.io.*;
import java.util.*;
class A{
public static void main(String arg[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer s = new StringTokenizer(br.readLine());
int t= Integer.parseInt(br.readLine());
while(t-->0)
{
int n,q,i;
n=Integer.parseInt(s.nextToken());
q=Integer.parseInt(s.nextToken());
int D[]= new int[n];
int Q[]=new int[q];
long x=1;
for(i=0;i<n;i++)
{
D[i]=Integer.parseInt(s.nextToken());
x=x*D[i];
}
for(i=0;i<q;i++)
{
Q[i]=Integer.parseInt(s.nextToken());
if(x>1000000000)
Q[i]=0;
else
Q[i]=(int)(Q[i]/x);
}
for(i=0;i<q;i++)
System.out.print(Q[i]+" ");
System.out.println("");
}
}
}

Assuming your first line is a single number and your second line a string of space separated numbers (if not, edit your question with your actual input)
I think you want to read t this way:
int t = Integer.parseInt(s.nextToken());
Then read your next line into your tokenizer
s = new StringTokenizer(br.readLine());
The code before the while loop should be:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer s = new StringTokenizer(br.readLine());
int t = Integer.parseInt(s.nextToken());
s = new StringTokenizer(br.readLine());
EDIT
You need to read each line in the tokenizer before using the next Int method. This should work.
Input:
1
2 3
2 3
5 100 8
Output:
0 16 1
Working code:
public static void main(String arg[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// read first line in tokenizer
StringTokenizer s = new StringTokenizer(br.readLine());
//parse t
int t = Integer.parseInt(s.nextToken());
// read second line in tokenizer
s = new StringTokenizer(br.readLine());
while(t-->0) {
int n,q;
// parse n and q (2, 3)
n=Integer.parseInt(s.nextToken());
q=Integer.parseInt(s.nextToken());
int D[]= new int[n];
int Q[]=new int[q];
long x=1;
// read third line in tokenizer
s = new StringTokenizer(br.readLine());
for(int i=0;i<n;i++) {
D[i]=Integer.parseInt(s.nextToken());
x=x*D[i];
}
// read fourth line in tokenizer
s = new StringTokenizer(br.readLine());
for(int i=0;i<q;i++) {
Q[i]=Integer.parseInt(s.nextToken());
if(x>1000000000)
Q[i]=0;
else
Q[i]=(int)(Q[i]/x);
}
for(int i=0;i<q;i++)
System.out.print(Q[i]+" ");
System.out.println("");
}
}

Related

How to avoid NumberFormatException while using BufferedReader?

I used BufferedReader to read data from the user, but it gives NumberFormat Exception prvovided I give the input in a certain way. Suppose in C, I write the code like this:
scanf("%d %d", &x, &y);
and i try to give the input to the console like this : 23 45, it assigns values like this:
x = 23, y = 45
How can I do something like this in java. I tried like this :
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
String line1 = br1.readLine();
String line2 = br2.readLine();
int x = Integer.parseInt(line1);
int y = Integer.parseInt(line2);
But this gives:
Exception in thread "main" java.lang.NumberFormatException: For input
string: "23 45" at
java.lang.NumberFormatException.forInputString(Unknown Source) at
java.lang.Integer.parseInt(Unknown Source) at
java.lang.Integer.parseInt(Unknown Source) at AB.main(AB.java:23)
readLine() reads until there is a newline, usually meaning that the user pressed enter.
If you input 12 34, this is one line, a line that isn't formatted like an integer.
You can either read a single char at a time by using read() instead, split the line manually and parse the results, or preferably use the Scanner class instead. It has a more user-IO oriented api, exposing methods such as nextInt() or next() (returning the next "token" instead of the next char) and so on.
Scanner in = new Scanner(System.in);
int x = in.nextInt();
int y = in.nextInt();
If you really want to pass both parameters on the same line, split the line on the white spaces, then use each token separately :
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
String line1 = br1.readLine();
String[] tokens = line1.split("\\s+")
int x = Integer.parseInt(tokens[0]);
int y = Integer.parseInt(tokens[1]);
Now here we are - many, many answers just explaining how to read correct input. But you'll get NumberFormatExceptions any other odd way. Like if your input would be...
98765432109876543210 9876543210987654321
This would in turn throw NumberFormatException as these are too large to fit into int.
So while splitting at " " and addressing tokens manually is good advice, you'd also run into ArrayIndexOutOfBounds if for instance only one number was given.
My recommendation would be to check if the input looks valid before you do any further parsing:
Scanner s = new Scanner(System.in);
String line = s.nextLine();
Pattern p = Pattern.compile("(\\d{1,10})\\s+(\\d{1,10})");
Matcher m = p.matcher(line);
if(m.matches()) {
long x1 = Long.parseLong(m.group(1));
long y1 = Long.parseLong(m.group(2));
if(x1 <= Integer.MAX_VALUE&& y1 <= Integer.MAX_VALUE) {
int x = (int)x1;
int y = (int)y1;
System.out.printf("Your input: %d / %d", x, y);
} else {
System.err.println("Numbers too big");
}
} else {
System.err.println("Input does not match criteria");
}
One InputStreamReader is enough:
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
String line1 = br1.readLine();
String[] args = line1.split(" ");
if (args.length != 2) {
throw new IllegalArgumentException("The input is wrong");
}
int x = getIntFromInput(args[0]);
int y = getIntFromInput(args[1]);
and as Jan pointed out you will need a convertion method:
public static int getIntFromInput(String inputString) {
//sanity checks
if (inputString == null || inputString.trim().length() == 0) {
throw new IllegalArgumentException("empty or null string passed");
}
try {
return new BigInteger(inputString, 10).intValueExact();
} catch (Exception ignored) {
throw new IllegalArgumentException(String.format("input string is not a valid integer number: '%s'", inputString));
}
}
This will solve your purpose
int x, y;
String[] input;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
input = str.split(" ");
x = Integer.parseInt(input[0]);
y = Integer.parseInt(input[1]);
You have to do something like that:
String in = *getline*;
String[] tab = in.split(' ');
List<int> nums = new ArrayList<int>();
for(String a : tab){
nums.add(Integer.parseInt(a)); // or push() not remember...
}
// Your nums have your integers now
You can read data from file and split string based upon 1 or more space and can handle multiple inputs separated by space.
Scanner s = new Scanner(System.in);
String line = s.nextLine();
//String line = "98765432109876543210 9876543210987654321 ";
String[] splited = line.split("\\s+");
Then compare each string to check whether it is valid number.
Then create a new BigDecimal from that number. A BigDecimal on 64bit jvm with with memory permitting can hold upto 646456993 digits.
for (int i=0;i<splited.length;i++) {
if(splited[i].matches("[0-9]*")){
BigDecimal num = new BigDecimal(splited[i]);
System.out.println("num "+(i+1)+"="+num);
}
}
Results
line ="98765432109876543210 9876543210987654321 "
num 1=98765432109876543210
num 2=9876543210987654321
line ="1212312312312312312312313 123432432432432423432423432"
num 1=1212312312312312312312313
num 2=123432432432432423432423432

infinite execution while taking input through console

I am not a total newbie but I got caught in a strange problem while taking input through console in java! Here is my code:
package com.test;
import java.io.*;
import java.util.Scanner;
public class EvenSum {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a1 = Integer.parseInt(br.readLine());
// tried through scanner class even
Scanner scan = new Scanner(System.in);
int a3 = scan.nextInt();
String input = br.readLine();
int a2 = Integer.parseInt(br.readLine());
DataInputStream in = new DataInputStream(System.in);
int a = in.readInt();
scan.close();
}
}
I have tried doing it by all the four ways separately but the execution of the code simply doesn't end it keeps going on and on. Did I break something?
Thanks in advance!
you have used br.readLine() 4 times and scan.nextInt(); 1 time your code will ask to input value 5 times, instead create object of Scanner and read int or String according to your requirement.
Try this:
Scanner input = new Scanner(System.in);
int num = input.nextInt();

Printing the last number only from a loop

If I have a while loop that goes through a file and prints the numbers, how would I make it to where it only prints the very last number.
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
This is the output I'm currently getting. How would I make get it to where it would just print out 19
And how would I get it to work to where the numbers start at 1 instead of 0?
Currently my loop looks like this:
if (math == last){
System.out.println(Score++);
}
math is another method which computes equations, and last is the answer inputed from a file, and the loop currently just checked if the math matches the inputed answer in order to "grade" the problems.
I can't use arrays, try/catch, or regex.
Just read through the file normally and store each line in a temporary variable. Once the reader finishes reading, print out the temporary variable.
public class ReaderExample {
public static void main(String[] args) throws IOException {
File file = new File("input.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String line = "";
String copy = "";
while((line = br.readLine() )!= null){
copy = line;
}
System.out.println(copy);
}
}
Using a Scanner
The same principle applies with a Scanner:
public class ReaderExample {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(new FileReader("input.txt"));
String line = "";
while(in.hasNext()){
line = in.nextLine();
}
System.out.println(line);
}
}
Without Invoking Scanner
public String getLast(Scanner scanner){
String line = "";
while(in.hasNext()){
line = in.nextLine();
}
return line;
}

Writing integers to a file errors

Here's my code:
public static void main(String[] args) throws IOException {
writeToFile ("c:\\scores.txt");
processFile("c:\\scores.txt");
}
public static void writeToFile (String filename) throws IOException {
BufferedWriter outputWriter = new BufferedWriter(new FileWriter(filename));
Scanner reader = new Scanner (System.in);
int Num;
System.out.println ("Please enter 7 scores");
for (int i = 0; i < 7; i++) {
Num= reader.nextInt ();
outputWriter.write(Num);
if(i!=6) {
outputWriter.newLine();
}
}
outputWriter.flush();
outputWriter.close();
}
public static void processFile (String filename) throws IOException, FileNotFoundException
{
double sum=0.00;
double number;
double average;
int count = 0;
BufferedReader inputReader = new BufferedReader (new InputStreamReader(new FileInputStream(filename)));
String line;
while((line = inputReader.readLine()) != null)
{
number= Double.parseDouble(line);
System.out.println(number);
count ++;
sum += (number);
System.out.print ("Total Sum: ");
System.out.println(sum);
System.out.print("Average of Scores: ");
average=sum/count;
System.out.println(average);
}
inputReader.close();
}
This is what my output is.
Please enter 7 scores
2
3
5
6
8
9
1
Exception in thread "main" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1011)
at java.lang.Double.parseDouble(Double.java:540)
at writefile.Writefile.processFile(Writefile.java:52)
at writefile.Writefile.main(Writefile.java:19)
Java Result: 1
I do not know how to fix this. I'm not sure how to fiz the floating decimal or empty string error.The file has weird symbols in it, no integers. How do I fix this? Please be specific please as I'm only a beginner at Java.
outputWriter.write(1); does not mean outputWriter.write("1");
you need change outputWriter.write(Num); to outputWriter.write(""+Num);
please refer outputstream.write(int)
The error happens because line is an empty string at some point, test for this before parsing the string:
while ((line = inputReader.readLine()) != null) {
if (line.trim().length() > 0) {
number = Double.parseDouble(line);
// rest of loop
}
}
That is, assuming that the line contains only numbers. If there are other characters, you'll have to perform a more careful validation before parsing the line.
The file has weird symbols in it, no integers.
From BufferedWriter#write(int):
Writes a single character.
So it's not writing the int value you're sending to it, instead its character representation.
It would be better if you just write the numeric value as String and then retrieve it as String and parse it. In your writeToFile method, modify this part
for (int i = 0; i < 7; i++) {
Num = reader.nextInt ();
outputWriter.write(Integer.toString(Num));
if(i!=6) {
outputWriter.newLine();
}
}

Taking values from an input file and adding to an integer array line by line (java)

Essentially I have a file of lines of integers. Each line has 9 digits. And I want to read the file. And then input each line into its an array. I want the array to be the same one each time. As I am going to do some processing to the array created from the first line. And then process the same array using a different line.
My input file is as follows:
8 5 3 8 0 0 4 4 0
8 5 3 8 0 0 4 2 2
And the current code that I am using is:
BufferedReader br = new BufferedReader(new FileReader("c:/lol.txt"));
Scanner sc = new Scanner(new File("c:/lol.txt"));
String line;
while (sc.hasNextLine()){
line = sc.nextLine();
int k = Integer.parseInt(line);
Now clearly I should be doing something more, I am just not really sure how to go about it.
Any help is greatly appreciated.
Try:
import java.util.Scanner;
import java.io.File;
public class Test {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(new File("c:/lol.txt"));
while (sc.hasNext()) {
String line = sc.nextLine();
// get String array from line
String[] strarr = line.split(" "); // attention: split expect regular expression, not just delimiter!
// initialize array
int[] intarr = new int[strarr.length];
// convert each element to integer
for (int i = 0; i < strarr.length; i++) {
intarr[i] = Integer.valueOf(strarr[i]); // <= update array from new line
}
}
}
}
Of course, you should handle exception instead to pass it.

Categories