I am not able to understand why I am getting null from bufferedreader in following code (2nd line of output), while it worked fine at some places (1st line of output).
I have used several system.out.println's just for debugging purpose.
Although the BufferedReader.readLine() returns null only when the end of the stream is reached, the input is being provided (as shown in input below the program). Please help me in getting the reason of getting null and suggest a solution.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
import java.lang.Integer;
import java.io.*;
class TestClass {
public static void main(String args[] ) throws Exception {
//* Read input from stdin and provide input before running
List a2=new ArrayList();
String[] a1=new String[2];
int count=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
/*for (String retval: line.split(" "))
a2.add(retval);*/
a1=line.split(" ");
//System.out.println("here 0"+a1[0]+" "+a1[1]);
/*int N = Integer.parseInt((a2.get(0)).toString());
int Q= Integer.parseInt((a2.get(1)).toString());*/
int N = Integer.parseInt(a1[0].toString());
int Q= Integer.parseInt(a1[1].toString());
System.out.println("here xxxxxxxx" + N +" " +Q);
String[] names=new String[N];
for(int i=0;i<N;i++){
//names[i] = (new BufferedReader(new InputStreamReader(System.in))).readLine();
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
names[i] = br1.readLine();
/*Scanner sc=new Scanner(System.in);
names[i]=sc.nextLine();*/
}
System.out.println("here 111" + names[0]);
for(int i=0;i<Q;i++) {
br = new BufferedReader(new InputStreamReader(System.in));
String line1 = br.readLine();
try{
System.out.println("here 1" + line1);
int M = Integer.parseInt(line1);
System.out.println("here 2");
if(M<=20){
System.out.println("here 3");
count++;
}
}
catch(Exception e){
System.out.println("here 4");
if(!((Arrays.asList(names)).contains(line))){
System.out.println("here 5");
count++;
}
}
}
System.out.println(count);
}
}
Input
First line of the input will contain two space separated integers denoting N and Q.
Next N lines will contain strings
Next Q lines will contain either an integer or a string denoting the name of a person. Different logics have to be implemented depending on whether it is aString or an Integer.
enter code here
Inputs and outputs are as follows:
Input:
2 4
pranjul
sachin
21
19
pranjul
vipul
Output:
here xxxxxxxx2 4
here 111null
here 1null
here 4
here 5
here 1null
here 4
here 5
here 1null
here 4
here 5
here 1null
here 4
here 5
4
You are trying to open more than one reader on the same input stream.
When you read the contents in your first br.readLine(), here is what happens:
The BufferedReader has an internal buffer it needs to fill up. It calls the read method from the underlying InputStreamReader in order to fill it.
The InputStreamReader, in order to convert the bytes in the input stream into characters, uses a StreamDecoder. So it calls that StreamDecoder's read method. Internally, it also has a buffer, and it reads as many bytes as it can from the underlying stream into that buffer.
This means that as soon as you read one line, you also read several characters beyond that line. The default size is of the StreamDecoder byte buffer is 8K, so it reads 8K bytes from System.in if they are available.
If you use System.in in interactive mode, each read only fills the buffer with as many bytes as are available right then. So it fills just one line up to the point where the user pressed Enter and so, when you open your other BufferedReader instances, they will get the next input the user enters.
But if System.in is redirected from a file or other stream where it is not blocking on end-of-line, it will read the entire file (assuming the file is smaller than 8K) on the first call to readLine. That data waits in that BufferedReader's buffer or the underlying StreamDecoder's buffer.
So when you open a new BufferedReader on System.in, there is no more data in that stream - it has already been read by the first BufferedReader. That's why it's not recommended to open more than one reader on a single stream.
first of all you dont need the first two rows if you use the io.*;
secondly why did you use so many streams, if 1 is enough
import java.util.*;
import java.lang.Integer;
import java.io.*;
class TestClass {
public static void main(String args[] ) throws Exception {
List a2=new ArrayList();
int count=0;
Scanner br = new Scanner(System.in);
String line = br.nextLine();
String[] a1=line.split(" ");
int N = Integer.parseInt(a1[0]);
int Q= Integer.parseInt(a1[1]);
System.out.println("here xxxxxxxx" + N +" " +Q);
String[] names=new String[N];
for(int i=0;i<N;i++){
names[i]=br.nextLine();
}
System.out.println("here 111" + names[0]);
for(int i=0;i<Q;i++) {
String line1 = br.nextLine();
try{
System.out.println("here 1" + line1);
int M = Integer.parseInt(line1);
System.out.println("here 2");
if(M<=20){
System.out.println("here 3");
count++;
}
}
catch(Exception e){
System.out.println("here 4");
if(!((Arrays.asList(names)).contains(line))){
System.out.println("here 5");
count++; } } }
System.out.println(count);
br.close();}}
i didnt test the code but it should work
i did it with scanner but you can use bufferedreader aswell
Related
My code is designed to read the contents of a text file and check if the contents are entered in a format that is as follows:
john : martin : 2 : 1
and if that format is followed then it will output it in the format:
john [2] | martin [1]
or else it will be counted as an invalid result and the total numbers will not be added to it whereas if the results are in the format then they will get added to the total so with the example it would display the number of vaild results as 1, invalid as 0 and total number as 3.
My question is that my code doesn't work properly as I get this error:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at reader.main(reader.java:33)
So how would I go about fixing this and reading and displaying the data in thee way that I want? Thanks in advance.
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Locale;
import java.util.Scanner;
public class reader {
/**
* #param args
* #throws FileNotFoundException
* #throws FileNotFoundException
* #throws FileNotFoundException when the file cannot be loaded
*/
public static void main(String[] args) throws FileNotFoundException {
String hteam;
String ateam;
int hscore;
int ascore;
Scanner s = new Scanner(new BufferedReader(new FileReader("results2.txt"))).useDelimiter(":");
// create a scanner which scans from a file and splits at each colon
while ( s.hasNext() ) {
hteam = s.next(); // read the home team from the file
ateam = s.next(); // read the away team from the file
hscore = s.nextInt(); //read the home team score from the file
ascore = s.nextInt(); //read the away team score from the file
System.out.print(hteam); // output the line of text to the console
System.out.print(hscore);
System.out.print(ateam);
System.out.println(ascore);
}
System.out.println("\nEOF"); // Output and End Of File message.
}
}
You're looking for s.next() instead of s.nextLine().
hteam = s.nextLine() reads the entire line "john : martin : 2 : 1", leaving nothing left for ateam.
Edit:
As you've said this still isn't working, I'd guess that you have an extra newline at the end of your input file, which is causing s.hasNext() to evaluate to true. This would cause the Scanner to trip up when it's getting the next input line.
Try Scanner s = new Scanner(System.in).useDelimiter("\\s*:\\s*|\\s*\\n\\s*"); to read multiple lines.
See implementation: http://ideone.com/yfiR2S
To verify that a line is in the correct format, I'd (with inspiration from osoblanco's answer) check that there are 4 words and that the last two are integers:
public static boolean verifyFormat(String[] words) {
// see endnote for isInteger()
return words.length == 4 && /*isInteger(words[2]) && isInteger(words[3])*/;
}
public static void main(String[] args) throws FileNotFoundException {
String hteam;
String ateam;
int hscore;
int ascore;
Scanner s = new Scanner(new BufferedReader(
new FileReader("results2.txt"))).useDelimiter("\\s*:\\s*|\\s*\\n\\s*");
while (s.hasNext()) {
String line = s.nextLine();
String[] words = line.split("\\s*:\\s*");
if(verifyFormat(words)) {
hteam = words[0]; // read the home team
ateam = words[1]; // read the away team
hscore = Integer.parseInt(words[2]); //read the home team score
ascore = Integer.parseInt(words[3]); //read the away team score
System.out.print(hteam); // output the line of text to the console
System.out.print(hscore);
System.out.print(ateam);
System.out.println(ascore);
}
}
System.out.println("EOF");
}
isInteger() can be found here.
I think scanning isn't quite what you want here. I would just use a BufferedReader and do ReadLine to handle 1 line each time through the for loop.
Then verify each line by the following:
1) String.split(":") and verify 4 pieces.
String [] linePieces = nextLine.split(":");
if(linePieces.length!=4)
{
//mark invalid, continue loop
}
2) Trim each piece
for(int i =0; i<4; i++)
linePieces[i] = linePieces[i].trim();
3) Verify piece 3 and piece 4 are numbers, Integer.parseInt with try/catch. In the catch block, count that the line is invalid.
try
{
name1=linePieces[0];
name2=linePieces[1];
score1=Integer.parseInt(linePieces[2]);
score2=Integer.parseInt(linePieces[3]);
//count as success and do logic
}catch(NumberFormatException e){
//invalid line
}
I am a noobie at programming and I can't seem to figure out what to do.
I am to write a Java program that reads in any number of lines from a file and generate a report with:
the count of the number of values read
the total sum
the average score (to 2 decimal places)
the maximum value along with the corresponding name.
the minimum value along with the corresponding name.
The input file looks like this:
55527 levaaj01
57508 levaaj02
58537 schrsd01
59552 waterj01
60552 boersm01
61552 kercvj01
62552 buttkp02
64552 duncdj01
65552 beingm01
I program runs fine, but when I add in
score = input.nextInt(); and
player = input.next();
The program stops working and the keyboard input seems to stop working for the filename.
I am trying to read each line with the int and name separately so that I can process the data and complete my assignment. I don't really know what to do next.
Here is my code:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;
public class Program1 {
private Scanner input = new Scanner(System.in);
private static int fileRead = 0;
private String fileName = "";
private int count = 0;
private int score = 0;
private String player = "";
public static void main(String[] args) {
Program1 p1 = new Program1();
p1.getFirstDecision();
p1.readIn();
}
public void getFirstDecision() { //*************************************
System.out.println("What is the name of the input file?");
fileName = input.nextLine(); // gcgc_dat.txt
}
public void readIn(){ //*********************************************
try {
FileReader fr = new FileReader(fileName + ".txt");
fileRead = 1;
BufferedReader br = new BufferedReader(fr);
String str;
int line = 0;
while((str = br.readLine()) != null){
score = input.nextInt();
player = input.next();
System.out.println(str);
line++;
score = score + score;
count++;
}
System.out.println(count);
System.out.println(score);
br.close();
}
catch (Exception ex){
System.out.println("There is no shop named: " + fileName);
}
}
}
The way you used BufferReader with Scanner is totally wrong .
Note: you can use BufferReader in Scanner constructor.
For example :
try( Scanner input = new Scanner( new BufferedReader(new FileReader("your file path goes here")))){
}catch(IOException e){
}
Note: your file reading process or other processes must be in try block because in catch block you cannot do anything because your connection is closed. It is called try catch block with resources.
Note:
A BufferedReader will create a buffer. This should result in faster
reading from the file. Why? Because the buffer gets filled with the
contents of the file. So, you put a bigger chunk of the file in RAM
(if you are dealing with small files, the buffer can contain the whole
file). Now if the Scanner wants to read two bytes, it can read two
bytes from the buffer, instead of having to ask for two bytes to the
hard drive.
Generally speaking, it is much faster to read 10 times 4096 bytes
instead of 4096 times 10 bytes.
Source BufferedReader in Scanner's constructor
Suggestion: you can just read each line of your file by using BufferReader and do your parsing by yourself, or you can use Scanner class that gives you ability to do parsing tokens.
difference between Scanner and BufferReader
As a hint you can use this sample for your parsing goal
Code:
String input = "Kick 20";
String[] inputSplited = input.split(" ");
System.out.println("My splited name is " + inputSplited[0]);
System.out.println("Next year I am " + (Integer.parseInt(inputSplited[1])+1));
Output:
My splited name is Kick
Next year I am 21
Hope you can fixed your program by given hints.
I wrote a program in Java using a Scanner to get two elements separated by a space from each line to put it in an object stocked in an ArrayList. It works perfectly, but when it comes to inputs with 10000's of lines, it becomes very long. I read a few topics and websites (such as this) telling BufferedReader would be a lot efficient than Scanner but I did not see any improvement while trying.
Here are the lines I use so far to parse each line of my input:
String charsetName = "UTF-8";
Scanner scanner = new Scanner(new BufferedInputStream(System.in), charsetName);
Then I have a loop running during the number of lines calling:
String[] mid = scanner.nextLine().split(" ");
So I tried to replace the Scanner by:
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String[] base = reader.readLine().split(" ");
It did not change anything (8 seconds for 12000 lines in both case)
Am I going in the right direction to get the program work a lot faster? Or does the problem come from using a loop to go through each line?
I used the following code to read through a file of 280,000 lines (consisting of two words per line separated by a space) and split them on a space. It took 0.105 seconds. So I would like to know more about the line you are parsing and what you are doing with it. Paste more code please.
public static void main(String args[]) throws Exception {
Date start = new Date();
BufferedReader b = new BufferedReader(new FileReader("aa.txt"));
String line;
while ((line = b.readLine())!=null) {
String[] splat = line.split(" ");
}
b.close();
Date end = new Date();
System.out.println("Took " + (end.getTime() - start.getTime()) / 1000.0 + " seconds");
}
I modified the code above to add each splat array to an array list (not sure why you want to do this, but I am guessing this is what you are trying to do, from your OP). The code slowed down to 0.244 seconds. Still way less than a second. More info please.
Supplement - FULL CODE (compile with javac Julien.java). Remember to replace aa.txt with your file name.
import java.util.*;
import java.io.*;
public class Julien {
public static void main(String args[]) throws Exception {
Date start = new Date();
// List arrl = new ArrayList();
BufferedReader b = new BufferedReader(new FileReader("aa.txt"));
String line;
while ((line = b.readLine())!=null) {
String[] splat = line.split(" ");
// arrl.add(splat);
}
b.close();
Date end = new Date();
System.out.println("Took " + (end.getTime() - start.getTime()) / 1000.0 + " seconds");
}
}
I am writing a program that read string and integers from file, then copy the data and write to another file. Data entries should be separated by a space.
My input should and output should follow the following format, the first two set of numbers are string while the others are integers:
123123 242323 09 08 06 44
I get Exception in thread "main" java.util.NoSuchElementException when I run my code, I do not know why
import java.util.Scanner;
import java.io.*;
public class Billing {
public static void main(String[] args) throws IOException {
//define the variables
String callingnumber;
String callednumber;
String line;
int startinghour;
int startingminute;
int endinghour;
int endingminute;
//open input and output files
FileReader freader = new FileReader("BillingData.txt");
BufferedReader inFile = new BufferedReader(freader);
FileWriter fwriter = new FileWriter("BillingOutput.txt");
PrintWriter outFile = new PrintWriter (fwriter);
// set space between the numbers
line=inFile.readLine();
while(line!=null)
{
//creat a scanner to use space between the numbers
Scanner space = new Scanner(line).useDelimiter(" ");
callingnumber=space.next();
callednumber=space.next();
startinghour=space.nextInt();
startingminute=space.nextInt();
endinghour=space.nextInt();
endingminute=space.nextInt();
// writing data to file
outFile.printf("%s %s %d %d %d %d", callingnumber, callednumber,startinghour, startingminute, endinghour, endingminute);
line=inFile.readLine();
}//end while
//close the files
inFile.close();
outFile.close();
}//end of mine
}//end of class
I suspect that the scanner has run out of data in the line - probably because there are less than 6 values in it. To avoid the error you should do something like this:
if (space.hasNextInt()) {
startingHour = space.nextInt();
}
Your Scanner is trying to read in a token that either doesn't exist or is of the wrong type. Myself, I'd split the String, line using " " as my delimiter and then deal with the array returned.
I want to read this string (from console not file) for example:
one two three
four five six
seven eight nine
So I want to read it per line and put every line in an array.
How can I read it? Because if I use scanner, I can only read one line or one word (nextline or next).
what I mean is to read for example : one two trhee \n four five six \n seven eight nine...
You should do by yourself!
There is a similer example:
public class ReadString {
public static void main (String[] args) {
// prompt the user to enter their name
System.out.print("Enter your name: ");
// open up standard input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String userName = null;
// read the username from the command-line; need to use try/catch with the
// readLine() method
try {
userName = br.readLine();
} catch (IOException ioe) {
System.out.println("IO error trying to read your name!");
System.exit(1);
}
System.out.println("Thanks for the name, " + userName);
}
} // end of ReadString class
To answer the question as clarified in the comment on the first answer:
You must call Scanner's nextLine() method once for each line you wish to read. This can be accomplished with a loop. The problem you will inevitably encounter is "How do I know big my result array should be?" The answer is that you cannot know if you do not specify it in the input itself. You can modify your programs input specification to require the number of lines to read like so:
3
One Two Three
Four Five
Six Seven Eight
And then you can read the input with this:
Scanner s = new Scanner(System.in);
int numberOfLinesToRead = new Integer(s.nextLine());
String[] result = new String[numberOfLinesToRead];
String line = "";
for(int i = 0; i < numberOfLinesToRead; i++) { // this loop will be run 3 times, as specified in the first line of input
result[i] = s.nextLine(); // each line of the input will be placed into the array.
}
Alternatively you can use a more advanced data structure called an ArrayList. An ArrayList does not have a set length when you create it; you can simply add information to it as needed, making it perfect for reading input when you don't know how much input there is to read. For example, if we used your original example input of:
one two trhee
four five six
seven eight nine
You can read the input with the following code:
Scanner s = new Scanner(System.in);
ArrayList<String> result = new ArrayList<String>();
String line = "";
while((line = s.nextLine()) != null) {
result.add(line);
}
So, rather than creating an array of a fixed length, we can simply .add() each line to the ArrayList as we encounter it in the input. I recommend you read more about ArrayLists before attempting to use them.
tl;dr: You call next() or nextLine() for each line you want to read using a loop.
More information on loops: Java Loops
Look at this code:
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class SearchInputText {
public static void main(String[] args) {
SearchInputText sit = new SearchInputText();
try {
System.out.println("test");
sit.searchFromRecord("input.txt");
System.out.println("test2");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void searchFromRecord(String recordName) throws IOException {
File file = new File(recordName);
Scanner scanner = new Scanner(file);
StringBuilder textFromFile = new StringBuilder();
while (scanner.hasNext()) {
textFromFile.append(scanner.next());
}
scanner.close();
// read input from console, compare the strings and print the result
String word = "";
Scanner scanner2 = new Scanner(System.in);
while (((word = scanner2.nextLine()) != null)
&& !word.equalsIgnoreCase("quit")) {
if (textFromFile.toString().contains(word)) {
System.out.println("The word is on the text file");
} else {
System.out.println("The word " + word
+ " is not on the text file");
}
}
scanner2.close();
}
}