I wrote a program to read a file. It reads the file correctly. I tested using print statements. But after reading the last line in the file, the program doesn't stop. It goes into an infinite loop. I am guessing that my while loop keeps reading blank characters as the next line. I don't know how to fix it.
If I put a break in the else part, it just reads the first line and breaks out of the while loop. I am not sure why.
Please help me out.
Here is the code :
public static void InterpretMessageFromFile() throws FileNotFoundException{
File inputfile = new File("FilePath");
Scanner reader = new Scanner(inputfile);
try
{
while (reader.hasNextLine())
{
//if the type of order is add order to existing Order Book
if (reader.hasNext("A")){
reader.next();
String retrieve_ts = reader.next();
int ts = Integer.parseInt(retrieve_ts);
//int ts = Integer.parseInt(retrieve_ts, 2); //for binary file
String retrieve_id = reader.next();
int id = Integer.parseInt(retrieve_id);
//int id = Integer.parseInt(retrieve_id ,2); // for binary file
String or_side = reader.next();
String retrieve_share = reader.next();
int share = Integer.parseInt(retrieve_share);
// int share = Integer.parseInt(retrieve_share, 2); //for binary file
String retrieve_price = reader.next();
int price = Integer.parseInt(retrieve_price);
//int price = Integer.parseInt(retrieve_price, 2); //for binary file
System.out.println("Add Order : Id is " + id );
AddOrderToExistingBook.AddNewOrder(id, ts, or_side, share, price);
}
//if it is cancel order
else if (reader.hasNext("X")){
reader.next();
String retrieve_ts = reader.next();
int ts = Integer.parseInt(retrieve_ts);
//int ts = Integer.parseInt(retrieve_ts, 2); //for binary file
String retrieve_id = reader.next();
int id = Integer.parseInt(retrieve_id);
System.out.println("Cancel Order : Id is " + id + " time stamp is : " + ts );
//int id = Integer.parseInt(retrieve_id, 2); //for binary file
//String retrieve_share = reader.next();
// int share = Integer.parseInt(retrieve_share, 2); // need to add back later, removing it for testing purposes
CancelOrder.CancelPartOfOrder(id, ts);
}
//if it is delete order
else if (reader.hasNext("D")){
reader.next();
String retrieve_ts = reader.next();
int ts = Integer.parseInt(retrieve_ts, 2);
String retrieve_id = reader.next();
int id = Integer.parseInt(retrieve_id, 2);
DeleteOrder.DeleteOrderFromBook(id, ts);
}
else{
// unexpected token.
// basically log as info and ignore.
}
}
}
finally
{
reader.close();
}
}
When handling file reading operations, it is always good to check couple of conditions -
1) Does the file has next line? 2) Is the next line not null.
while (reader.hasNextLine() && (line = reader.nextLine()) != null)
And then you can use this String line in your code.
In you code, although you are getting the next value by reader.next(), It does not advance the pointer. That is the reason the loop never exits.
Ref: Scanner
The last else block does not actually consume the line. Therefore if you've got any characters after the last line of real data in your input the program will always have a next line. Try consuming the file one line at a time and then using String.split() to get the individual tokens in each line. This guarantees that you will consume every line in the file.
Related
I have the following method which takes user input and applies an algorithm to it. However when I try to print the String process_name, stored in the fcfs ArrayList it comes out empty. But the burst_time and arrival_time fields in the same fcfs ArrayList get output to the console exactly as the user inputted the data. Not really sure what could be wrong.
public static void algorithm() {
ArrayList<Process> fcfs = new ArrayList<>();
Scanner scan = new Scanner(System.in);
System.out.println("Process name,CPU Burst Time,Arrival time\n ");
while (!scan.next().equalsIgnoreCase("finish")) {
Process p = new Process();
String pn = "";
String bt = "";
String at = "";
pn = input.nextLine();
bt = input.nextLine();
at = input.nextLine();
System.out.println("Process name, CPU Burst Time, Arrival time\n ");
p.process_name = pn;
p.burstTime = Float.parseFloat(bt);
p.arrivalTime = Float.parseFloat(at);
fcfs.add(p);
}
{
Collections.sort(fcfs, new comp());
}
result(fcfs, fcfs.size(),false)
}
This is the Process class:
class Process {
String process_name;
float burstTime;
float arrivalTime;
float compTime = 0;
boolean status = false;
}
scan.next() function gets the next input string. That is why you get an empty line for process name because the name is already taken by the next function in the while condition. Either use hasNext() to check if there is a next line or get and put input to a string variable and compare it with the word 'finish'.
You can see the explanation in the documentation: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#next()
Quoting from the documentation: "next : Finds and returns the next complete token from this scanner"
The problem is because of using next() instead of nextLine(). Check Scanner is skipping nextLine() after using next() or nextFoo()? to learn more about it.
Replace
while (!scan.next().equalsIgnoreCase("finish"))
with
while (!scan.nextLine().equalsIgnoreCase("finish"))
Also, it's better to use do...while which guarantees to execute its body at least once i.e.
do {
Process p = new Process();
String pn = "";
String bt = "";
String at = "";
pn = input.nextLine();
bt = input.nextLine();
at = input.nextLine();
System.out.println("Process name, CPU Burst Time, Arrival time\n ");
p.process_name = pn;
p.burstTime = Float.parseFloat(bt);
p.arrivalTime = Float.parseFloat(at);
fcfs.add(p);
} while (!scan.nextLine().equalsIgnoreCase("finish"));
I want to send studentGrade array to "calculate" method to calculate the average of grades but if the first line of text file is parameter, I can't. When the "if" method running, it goes back to while loop, even though two strings are equal.
I've tried to change the first line of .txt, in case of there is a problem. But the result was the same. It never does compare if the wanted person is in the first line.
static int studentNumber = 0;
static String[] studentGrade;
static String studentName = "";
static void makeList(String name) {
try(Scanner sc = new Scanner(new BufferedReader(new FileReader("C:\\Users\\User\\Desktop\\new_1.txt")))) {
boolean flag = true;
while (sc.hasNextLine()) {
flag = true;
String studentLine = sc.nextLine();
studentGrade = studentLine.split(",");
studentName = studentGrade[0];
if (studentName.equalsIgnoreCase(name)){
calculate(studentGrade);
flag = false;
break;
}
}
if (flag)
System.out.println("Couldn't found!");
}
catch (FileNotFoundException e) {
System.out.println("An error occured when the file was tried to opened.");
}
}
static void calculate(String[] a) {
int note1 = Integer.parseInt(a[1]);
int note2 = Integer.parseInt(a[2]);
int note3 = Integer.parseInt(a[3]);
double avg = Math.ceil((double)(note1 + note2 + note3) / 3);
System.out.println(a[0] + "'s average is: " + (int)avg);
}
I expect the if case would be true and sent the array to "calculate" method. It does its job except the student is in the first line of .txt file. For example if user input is Michael, it says "Couldn't found!" but if the input is John, it gives its average.
//First lines of .txt file
Michael,70,90,20
John,90,80,60
Molly,60,30,50
I created a file with the values you are giving:
Michael,70,90,20
John,90,80,60
Molly,60,30,50
And when I try your code, it seems to work fine:
makeList("Michael");
makeList("John");
makeList("Molly");
return
60
77
47
My suspicion is that you have some kind of invisible character at the very beginning of your file, and that is what makes your equality fail. I encountered this kind of issue several time when parsing XML and the parser would complain that my file doesn't start with an XML tag.
Can you try to make a brand new file with these 3 lines and try your program again on this new file?
Here is a much simpler and clearer way to do this:
try (Stream<String> lines = Files.lines("C:\\Users\\User\\Desktop\\new_1.txt")) {
Optional<String[]> studentGradesOpt =
lines.map(line -> line.split(","))
.filter(row -> row[0].equalsIgnoreCase(name))
.findFirst();
studentGradesOpt.ifPresent(grades -> calculate(grades));
if (!studentGradesOpt.isPresent()) {
System.out.println("Couldn't find student " + name);
}
}
I have the below integers in File :
758 29
206 58
122 89
I have to read these integers in an integer array and then need to store the values in key value pair. Then print the output as :
Position 29 has been initialized to value 758.
Position 89 has been initialized to value 122.
I have tried as of now :
private static Scanner readFile() {
/*
* Your program will prompt for the name of an input file and the read
* and process the data contained in this file. You will use three
* integer arrays, data[], forward[] and backward[] each containing 100
* elements
*/
int data[] = new int[100];
int forward[] = new int[100];
int backward[] = new int[100];
System.out.print("Please enter File Name : ");
#SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
String filename = scanner.nextLine();
File inputFile = new File(filename);
Scanner linReader = null;
try {
linReader = new Scanner(new File(filename));
while (linReader.hasNext()) {
String intStringSplit = linReader.nextLine();
String[] line = intStringSplit.split("\t",-1);
data = new int[line.length];
for (int i = 0; i < data.length; i++) {
data[i] = Integer.parseInt(line[i]);
}
System.out.println(data);
}
linReader.close();
} catch (Exception e) {
System.out.println("File Not Found");
}
return linReader;
}
I am not able to figure out how to get the key and value from the read data.
When posting information related to your question it is very important that you provide the data (in file for example) exactly as it is intended in reality so that we can make a more positive determination as to why you are experiencing difficulty with your code.
What you show as an in file data example indicates that each file line (which contains actual data) consists of two specific integer values. The first value being the initialization value and the second being the position value.
There also appears to be a blank line after ever line which contains actual data. This really doesn't matter since the code provided below has a code line to take care of such a thing but it could be the reason as to why you may be having difficulty.
To me, it looks like the delimiter used to separate the two integer values in each file line is indeed a whitespace as #csm_dev has already mentioned within his/her comment but you claim you tried this in your String.split() method and determined it is not a whitespace. If this is truly the case then it will be up to you to determine exactly what that delimiter might be. We couldn't possibly tell you since we don't have access to the real file.
You declare a File object within your provided code but yet nowhere do you utilize it. You may as well delete it since all it's doing is sucking up oxygen as far as I'm concerned.
When using try/catch it's always good practice to catch the proper exceptions which in this case is: IOException. It doesn't hurt to also display the stack trace as well upon an exception since it can solve a lot of your coding problems should an exception occur.
This code should work:
private static Scanner readFile() {
/*
* Your program will prompt for the name of an input file and the read
* and process the data contained in this file. You will use three
* integer arrays, data[], forward[] and backward[] each containing 100
* elements
*/
int data[] = new int[100];
int forward[] = new int[100];
int backward[] = new int[100];
System.out.print("Please enter File Name : ");
Scanner scanner = new Scanner(System.in);
String filename = scanner.nextLine();
File inputFile = new File(filename); // why do you have this. It's doing nothing.
Scanner linReader = null;
try {
linReader = new Scanner(new File(filename));
while (linReader.hasNext()) {
String intStringSplit = linReader.nextLine();
// If the file line is blank then just
// continue to the next file line.
if (intStringSplit.trim().equals("")) { continue; }
// Assuming at least one whitespace is used as
// the data delimiter but what the heck, we'll
// use a regular expression within the split()
// method to handle any number of spaces between
// the integer values.
String[] line = intStringSplit.split("\\s+");
data = new int[line.length];
for (int i = 0; i < line.length; i++) {
data[i] = Integer.parseInt(line[i]);
}
System.out.println("Position " + data[1] +
" has been initialized to value " +
data[0] + ".");
// do whatever else you need to do with the
// data array before reading in the next file
// line......................................
}
linReader.close();
}
catch (IOException ex) {
System.out.println("File Not Found");
ex.printStackTrace();
}
return linReader;
}
So, I'm trying to set up a simple config for a project. The goal here is to read certain values from a file and, if the file does not exist, to write said file. Currently, the creation of the file works fine, but my Scanner is acting a bit funny. When I reach the code
case "resolution": resolution = readConfig.next();
it makes the value of resolution "1024x768\nvsync" whereas it should only be "1024x768". If it were working as I planned, then the next value for
readingConfig = readConfig.next();
at the beginning of my while loop would be "vsync", which my switch statement would then catch and continue editing the values to those of the file.
Why is my Scanner picking up on the "\n" that is the 'enter' to the next line in the text document?
public static void main(String[] args) {
int musicVol = 0;
int soundVol = 0;
String resolution = null;
boolean vsync = false;
Scanner readConfig;
String readingConfig;
File configFile = new File(gameDir + "\\config.txt");
if (configFile.exists() != true) {
try {
configFile.createNewFile();
PrintWriter writer = new PrintWriter(gameDir + "\\config.txt");
writer.write("resolution = 1024x768 \n vsync = true \n music = 100 \n sound = 100");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
readConfig = new Scanner(configFile);
readConfig.useDelimiter(" = ");
while (readConfig.hasNext()) {
readingConfig = readConfig.next();
switch (readingConfig) {
case "resolution":
resolution = readConfig.next();
break;
case "vsync":
vsync = readConfig.nextBoolean();
break;
case "music":
musicVol = readConfig.nextInt();
break;
case "sound":
soundVol = readConfig.nextInt();
break;
}
}
readConfig.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
Instead of using .hasNext and .next(), you will have to use .hasNextLine() and .nextLine(). I would write this as a comment, but do not have enought rep to comment yet.
You are using next() which will not delimit your lines, try using nextLine() instead:
String nextLine() Advances this scanner past the current line and
returns the input that was skipped.
I'd suggest not using the delimiter, and get the whole line instead as a string, and then split the string to the parts you want.
Something like
String nextLine = readConfig.nextLine();
String[] split = nextLine.split(" = ");
String resolution = split[1]; // just an example
...
Aha, solved!
What this does is pull the entire text file into a String (using Scanner.nextLine() removes the '\n') and then adds the " = " at the end of each line instead. Thus, when the Scanner runs back over the String for the switch, it will already be ignoring the " = " and pull the desired information from the String.
String config = "";
try {
readConfig = new Scanner(configFile);
while (readConfig.hasNext()) {
config += readConfig.nextLine() + " = ";
readConfig = new Scanner(config);
readConfig.useDelimiter(" = ");
I am getting java.util.NoSuchElementException error. We get this error for the following reasons.
If we don't check if the file has next line before reading it,
then it throws exception after reading last line since it is trying
to read a line which doesn't exist.
Format of the file is messed up
I think that the format of the file I m using is correct and I am also checking for next line before reading it but I am still getting the error.
When I debug it using print statement, it prints all the line and throws java.util.NoSuchElementException error after reading last line.
Please help me out
Here is the code :
public static void InterpretMessageFromFile() throws FileNotFoundException{
File inputfile = new File("filepath");
Scanner reader = new Scanner(inputfile);
try {
while (reader.hasNextLine()) {
String MessageType = reader.next();
int IsAdd = MessageType.compareToIgnoreCase("A");
int IsCancel = MessageType.compareToIgnoreCase("X");
int IsDelete = MessageType.compareToIgnoreCase("D");
int IsExecute = MessageType.compareToIgnoreCase("E");
int IsReplace = MessageType.compareToIgnoreCase("U");
//if the type of order is add order to existing Order Book
if (IsAdd == 0) {
String retrieve_ts = reader.next();
int ts = Integer.parseInt(retrieve_ts);
String retrieve_id = reader.next();
int id = Integer.parseInt(retrieve_id);
String or_side = reader.next();
String retrieve_share = reader.next();
int share = Integer.parseInt(retrieve_share);
String retrieve_price = reader.next();
int price = Integer.parseInt(retrieve_price);
System.out.println("Add Order : Id is " + id );
AddOrderToExistingBook.AddNewOrder(id, ts, or_side, share, price);
}
//if it is cancel order
if (IsCancel == 0){
String retrieve_ts = reader.next();
int ts = Integer.parseInt(retrieve_ts);
String retrieve_id = reader.next();
int id = Integer.parseInt(retrieve_id);
System.out.println("Cancel Order : Id is " + id + " time stamp is : " + ts );
CancelOrder.CancelPartOfOrder(id, ts);
}
}
}
}
finally {
reader.close();
}
}
Exception (copied from comments):
Exception in thread "main" java.util.NoSuchElementException at
java.util.Scanner.throwFor(Scanner.java:907) at
java.util.Scanner.next(Scanner.java:1416) at
OrderBook.InterpretOrderBookUpdateMessage.InterpretMessageFromFile(InterpretOrde‌​rBookUpdateMessage.java:20)
at OrderBook.MainMethod.main(MainMethod.java:50)
you are trying to consume a token that is not there.
you do a number of next() calls without checking if there is next.
in your case, I suspect a newline at the end of your file gives you an empty line as input.
the scanner will see a new line, but as it doesn't contain tokens, calling "next()" will cause an error.
the same would happen if you have empty lines between blocks in your file.
one thing you can use is:
public boolean hasNext(String pattern)
instead of
next()
this will let you do a lookahead without consuming a token.
so instead of:
String MessageType = reader.next();
int IsAdd = MessageType.compareToIgnoreCase("A");
int IsCancel = MessageType.compareToIgnoreCase("X");
// .... left out other classes
//if the type of order is add order to existing Order Book
if (IsAdd == 0){
// .. do stuff
}
you can do something like:
if (reader.hasNext("A") {
reader.next(); // consume A
int ts = reader.nextInt(); // get id
// ... etcetera
} else if (reader.hasNext("X") {
}
I would also recommend you use nextInt() instead of nextString and then calling parseInt
One other thing: you can even make your code better to read by doing:
if (reader.hasNext("A") {
handleAddition(reader);
}
and then later on define a method that only handles this case.
your main method will look like:
try
{
while (reader.hasNextLine())
{
if (reader.hasNext("A")) {
handleAdd(reader);
} else if (reader.hasNext("X")) {
handleCancel(reader);
} else if (reader.hasNext("D")) {
handleDelete(reader);
} else if (reader.hasNext("E")) {
handleExecute(reader);
} else if (reader.hasNext("R")) {
handleReplace(reader);
} else {
// unexpected token. pretty sure this is the case that triggers your exeception.
// basically log as info and ignore.
reader.nextLine();
}
}
}
finally
{
reader.close();
}
Now your method is nice and short, and all the specific actions are taken in methods with their own name.
the only thing I'm not 100% about if it it's good practice to consume A, X, R, etc... inside the main loop, or the actual handler method. I prefer to consume inside the method personally.
hope it helps.