| | |
Reading numbers from a text file
![]() |
•
•
Join Date: Jul 2007
Posts: 34
Reputation:
Solved Threads: 0
I need to read numbers from a text file and then store them in variables to use as input for the Java code I am writing.
The text file will be in this format:
--------------------------------------------------------
5------------- this is the number of nodes
0------------- starting node
0,2,1---------node a,node b, distance from a-b
3,4,5--------- " , " , "
2,6,5
1,4,2
1,5,3
----------------------------------------------------------
Any ideas?
The text file will be in this format:
--------------------------------------------------------
5------------- this is the number of nodes
0------------- starting node
0,2,1---------node a,node b, distance from a-b
3,4,5--------- " , " , "
2,6,5
1,4,2
1,5,3
----------------------------------------------------------
Any ideas?
What do you have so far, and what problem is it giving you?
FileReader with BufferedReader and String.split,
or Scanner are good starting points, though.
FileReader with BufferedReader and String.split,
or Scanner are good starting points, though.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
I recommend
a. you replace the commas w/spaces and use
b. you keep the commas, use
I'd go with "a".
Scanner highly. This will work nicely if:a. you replace the commas w/spaces and use
nextInt()b. you keep the commas, use
next(), and set up an elaborate system to trim the commas out of the Strings made and convert said Strings to intsI'd go with "a".
Last edited by venomlash; Jul 23rd, 2007 at 11:13 am. Reason: oops
Beware of the Rancor. I'm not kidding.
If it doesn't compile, try saying "By the power of MegaMan!!!" <this has kinda worked for me, actually...>
Scotland is NOT North Britain, Glasgow does NOT rhyme with "cow", and Robbie Burns is...well, if you don't already know who he was, you're kinda screwed.
If it doesn't compile, try saying "By the power of MegaMan!!!" <this has kinda worked for me, actually...>
Scotland is NOT North Britain, Glasgow does NOT rhyme with "cow", and Robbie Burns is...well, if you don't already know who he was, you're kinda screwed.
•
•
•
•
I recommendScannerhighly. This will work nicely if:
a. you replace the commas w/spaces and usenextInt()
b. you keep the commas, usenext(), and set up an elaborate system to trim the commas out of theStrings made and convert saidStrings to ints
I'd go with "a".
•
•
Join Date: Jul 2007
Posts: 34
Reputation:
Solved Threads: 0
Ok, so I used Scanner to read input from a text file. I'm trying to test this by just reading in input from a text file and printing it out. But I'm getting some weird errors.
My testCase file is:
5
55
555
5555
55555
The errors I can't seem to figure out are:
Assignment5.java:18: <identifier> expected
System.out.println(numNodes);
^
Assignment5.java:18: <identifier> expected
System.out.println(numNodes);
^
Assignment5.java:21: illegal start of type
while(i <= numNodes);
^
Assignment5.java:21: <identifier> expected
while(i <= numNodes);
^
Assignment5.java:21: <identifier> expected
while(i <= numNodes);
^
5 errors
Java Syntax (Toggle Plain Text)
import java.lang.*; import java.util.*; public class Assignment5 { int i = 0; Scanner st = new Scanner(new File("testCase")).useDelimiter(); int numNodes = st.nextInt(); System.out.println(numNodes); while(i <= numNodes) { Scanner st = new Scanner(new File("testCase")).useDelimiter(); int k = st.nextInt(); System.out.println(k); } public static void main(String[] args) { Assignment5 A5 = new Assignment5(); }//end of main }//end of Assignment5 class
My testCase file is:
5
55
555
5555
55555
The errors I can't seem to figure out are:
Assignment5.java:18: <identifier> expected
System.out.println(numNodes);
^
Assignment5.java:18: <identifier> expected
System.out.println(numNodes);
^
Assignment5.java:21: illegal start of type
while(i <= numNodes);
^
Assignment5.java:21: <identifier> expected
while(i <= numNodes);
^
Assignment5.java:21: <identifier> expected
while(i <= numNodes);
^
5 errors
Last edited by baltazar; Jul 24th, 2007 at 3:40 am. Reason: typo
•
•
Join Date: Jul 2007
Posts: 34
Reputation:
Solved Threads: 0
I am still confused about how to write the usDelimiter in the scanner.
I need it to read number seperated by commas.
What would the useDelimiter parameter be?
I have tried a couple of things (.useDelimiter(",") and .useDelimiter(,)) but they didn't work
Here is my code:
I need it to read number seperated by commas.
What would the useDelimiter parameter be?
I have tried a couple of things (.useDelimiter(",") and .useDelimiter(,)) but they didn't work
Here is my code:
Java Syntax (Toggle Plain Text)
import java.lang.*; import java.util.*; import java.io.File; public class Assignment5 { public Assignment5() { int numNodes = 0; Scanner st = null; try { st = new Scanner(new File("testCase.txt")).useDelimiter(","); numNodes = st.nextInt(); System.out.println(numNodes); } catch(Exception e) { e.printStackTrace(); } while(st.hasNextInt()) { int k = st.nextInt(); System.out.println(k+","); } } public static void main(String[] args) { Assignment5 A5 = new Assignment5(); }//end of main }//end
You need to specify the delimiter keeping in mind that its a regular expression pattern. If your input from the file is nothing but numbers separated by ',' without any space in between them, the code posted by you must work.
I am assuming that your code won't work since the delimiter you gave was only a single ',' while your input must have had spaces between the number and comma. (eg. 1, 3, 3). You should have input in this format (1,2,3,4).
If you want your code to work for something like(1, 2, 3,4, 5) :
I am assuming that your code won't work since the delimiter you gave was only a single ',' while your input must have had spaces between the number and comma. (eg. 1, 3, 3). You should have input in this format (1,2,3,4).
If you want your code to work for something like(1, 2, 3,4, 5) :
Java Syntax (Toggle Plain Text)
String sample = "1 , 2 , 3 ,4, 5"; st = new Scanner(sample).useDelimiter("\\s*,\\s*"); int k; while(st.hasNextInt()) { k = st.nextInt(); System.out.println(k); }
I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- problems with reading random access line from a file (C++)
- C++ Reading from a text file (C++)
- Help Please, how do i read from text file into array? (Visual Basic 4 / 5 / 6)
- Need Help in Reading characters from a text file (C++)
- Inputting text file data into an array, please help! (C++)
- Reading in a text file string is not complete (Pascal and Delphi)
Other Threads in the Java Forum
- Previous Thread: Panels, different kinds, and how they work together.
- Next Thread: Java Semantic Analyzer
| Thread Tools | Search this Thread |
-xlint android api applet application array arrays automation bi binary blackberry block bluetooth class client code compile compiler component database developmenthelp eclipse equation error event fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide image int integer j2me j2seprojects java javac javaprojects jetbrains jni jpanel jtable julia learningresources lego linux list login loops mac main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying number online pearl problem program programming project qt recursion scanner screen server set singleton sms sort spamblocker sql string swing system textfields thread threads time title tree tutorial-sample update variablebinding windows working xor






