943,946 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 25321
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 23rd, 2007
0

Reading numbers from a text file

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
baltazar is offline Offline
46 posts
since Jul 2007
Jul 23rd, 2007
0

Re: Reading numbers from a text file

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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Jul 23rd, 2007
0

Re: Reading numbers from a text file

I recommend 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 ints
I'd go with "a".
Last edited by venomlash; Jul 23rd, 2007 at 11:13 am. Reason: oops
Reputation Points: 86
Solved Threads: 2
Junior Poster
venomlash is offline Offline
143 posts
since Oct 2006
Jul 23rd, 2007
0

Re: Reading numbers from a text file

Click to Expand / Collapse  Quote originally posted by venomlash ...
I recommend 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 ints
I'd go with "a".
You can set the delimeter to any pattern you wish with the useDelimeter() method. You don't need to convert anything prior to scanning.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Jul 24th, 2007
0

Re: Reading numbers from a text file

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.

Java Syntax (Toggle Plain Text)
  1. import java.lang.*;
  2. import java.util.*;
  3.  
  4. public class Assignment5
  5. {
  6. int i = 0;
  7.  
  8. Scanner st = new Scanner(new File("testCase")).useDelimiter();
  9. int numNodes = st.nextInt();
  10. System.out.println(numNodes);
  11.  
  12.  
  13. while(i <= numNodes)
  14. {
  15. Scanner st = new Scanner(new File("testCase")).useDelimiter();
  16. int k = st.nextInt();
  17. System.out.println(k);
  18. }
  19.  
  20.  
  21. public static void main(String[] args)
  22. {
  23. Assignment5 A5 = new Assignment5();
  24. }//end of main
  25.  
  26.  
  27. }//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
Reputation Points: 10
Solved Threads: 1
Light Poster
baltazar is offline Offline
46 posts
since Jul 2007
Jul 24th, 2007
0

Re: Reading numbers from a text file

You do not have much of your code inside a method - it's in the class file. Place that code inside a method, either main or another method that you call from main to execute like A5.run() or something.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Jul 25th, 2007
0

Re: Reading numbers from a text file

Ah yes, I saw my error. It was a noob mistake.
Reputation Points: 10
Solved Threads: 1
Light Poster
baltazar is offline Offline
46 posts
since Jul 2007
Jul 25th, 2007
0

Re: Reading numbers from a text file

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:
Java Syntax (Toggle Plain Text)
  1. import java.lang.*;
  2. import java.util.*;
  3. import java.io.File;
  4.  
  5. public class Assignment5
  6. {
  7. public Assignment5()
  8. {
  9. int numNodes = 0;
  10. Scanner st = null;
  11. try
  12. {
  13. st = new Scanner(new File("testCase.txt")).useDelimiter(",");
  14. numNodes = st.nextInt();
  15. System.out.println(numNodes);
  16. }
  17. catch(Exception e)
  18. {
  19. e.printStackTrace();
  20. }
  21.  
  22.  
  23.  
  24. while(st.hasNextInt())
  25. {
  26. int k = st.nextInt();
  27. System.out.println(k+",");
  28. }
  29. }
  30.  
  31.  
  32. public static void main(String[] args)
  33. {
  34. Assignment5 A5 = new Assignment5();
  35. }//end of main
  36.  
  37.  
  38. }//end
Reputation Points: 10
Solved Threads: 1
Light Poster
baltazar is offline Offline
46 posts
since Jul 2007
Jul 25th, 2007
0

Re: Reading numbers from a text file

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) :
Java Syntax (Toggle Plain Text)
  1. String sample = "1 , 2 , 3 ,4, 5";
  2. st = new Scanner(sample).useDelimiter("\\s*,\\s*");
  3. int k;
  4. while(st.hasNextInt())
  5. {
  6. k = st.nextInt();
  7. System.out.println(k);
  8. }
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Jul 25th, 2007
0

Re: Reading numbers from a text file

You should use a BufferedReader to read the lines and then use either String.split() or the Scanner to separate the items for your use. This is exactly what Masijade told you in the first response to your post by the way.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Panels, different kinds, and how they work together.
Next Thread in Java Forum Timeline: Java Semantic Analyzer





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC