Practicing reading from file.
Hello, I am practicing reading from a text-file into arrays. I have encountered a problem I am not able to solve.
I have a file called "nameage.txt":
Code :
Tom 44
Arnold 98
Susie 24
Johnny 17
My goal is to read this info into two different arrays, a string array called name, and an integerer array called age. Here is my attempt:
Code java:
import java.util.Scanner;
import java.io.File;
class nameAge {
public static void main(String[] args) {
String[] name = new String[4];
int[] age = new int[4];
try {
Scanner reader = new Scanner(new File("nameage.txt"));
String[] line = new String[2];
int i = 0;
while (reader.hasNextLine()) {
line = reader.nextLine().split(" ");
name[i] = line[0];
//age[i] = Integer.parseInt(line[1]);
i++;
}
}
catch(Exception e) {
System.out.println("Reading didn't work.");
}
}
}
Please note the part I have commented out, it is the reading of the ages. If I don't have that part with me, the reading of the names goes perfectly fine and I have checked that the array is correct. But if I try to uncomment the one line and read in the ages aswell the program will compile but an exception is caught, can you guys see what is wrong with the code?
Re: Practicing reading from file.
You need to see what the exception is. Change the catch block to call the printStackTrace() method which will print out the text of the error message. Given that you can work on the problem.
Also try debugging the code by adding some println statements.
Print out the contents of the line that is read from the file and the contents of the array created by split.
The Arrays class's toString() method will format the array for printing:
Code :
System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
Re: Practicing reading from file.
Code :
[Tom, , , , , , , , 44]
[Arnold, , , , , 98]
[Susie, , , , , , 24]
[Johnny, , , , , 17]
Thanks I used your last codebit, and this is what i got. The problem is in the split it seems. What I want is that the split is to delete all the spaces and create and array where the element is what is between the spaces.
From what I read it seems that split("\\s") should do it, but it still does not work, hm.
Re: Practicing reading from file.
What is printed out when the contents of the line that is read (returned by nextLine) was printed?
Re: Practicing reading from file.
Quote:
Originally Posted by
Norm
What is printed out when the contents of the line that is read (returned by nextLine) was printed?
I added this code in the while loop: System.out.println(Arrays.toString(line));
And the output became:
Code :
[Tom, , , , , , , , 44]
[Arnold, , , , , 98]
[Susie, , , , , , 24]
[Johnny, , , , , 17]
It is one array for each pass in the while-loop. It seems that I split the string from the text-file, but the spaces are still there.
Re: Practicing reading from file.
What is printed out when the contents of the line that is read (returned by nextLine) was printed?
You posted the output from the Arrays class's toString() method.
I guess your problem is with what regex to use to skip any number of spaces.
Re: Practicing reading from file.
Quote:
Originally Posted by
Norm
What is printed out when the contents of the line that is read (returned by nextLine) was printed?
You posted the output from the Arrays class's toString() method.
I guess your problem is with what regex to use to skip any number of spaces.
Ok, sorry I misunderstood what you wanted. I changed the code so that in each pass in the while loop it also prints out each of the content in the line array:
Code java:
import java.util.*;
import java.io.File;
class nameAge {
public static void main(String[] args) {
String[] name = new String[4];
int[] age = new int[4];
try {
Scanner reader = new Scanner(new File("nameage.txt"));
String[] line = new String[2];
int i = 0;
while (reader.hasNextLine()) {
line = reader.nextLine().split(" ");
for (String s: line) {////////Here I print out the content.
System.out.println(s);////
}//////////////////////7//////
}
}
catch(Exception e) {
e.printStackTrace() ;
}
}
}
The output in the terminal is:
Code :
Tom
44
Arnold
98
Susie
24
Johnny
17
It still seems that what I wrote earlier happens. The spaces does not disappear.
Re: Practicing reading from file.
That's not what I was asking. I asked for the line that was read to be printed. Something like this:
Code :
String theLine = reader.nextLine(); // Read the line
System.out.println("theLine="+theLine); // print the line
You need to research how to write a regex to skip one or more spaces.
Re: Practicing reading from file.
Quote:
Originally Posted by
Norm
That's not what I was asking. I asked for the line that was read to be printed. Something like this:
Code :
String theLine = reader.nextLine(); // Read the line
System.out.println("theLine="+theLine); // print the line
You need to research how to write a regex to skip one or more spaces.
Yeah, you are correct. I thought it would automatically would be done with split(" ") or split("\\s"), but I need to use split("\\s+"). Then the code works.
Thank you veru much for your time! :)