Reading numbers from a text file

Reply

Join Date: Jul 2007
Posts: 34
Reputation: baltazar is an unknown quantity at this point 
Solved Threads: 0
baltazar baltazar is offline Offline
Light Poster

Reading numbers from a text file

 
0
  #1
Jul 23rd, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,355
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is online now Online
Nearly a Posting Maven

Re: Reading numbers from a text file

 
0
  #2
Jul 23rd, 2007
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.
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 143
Reputation: venomlash is an unknown quantity at this point 
Solved Threads: 2
venomlash's Avatar
venomlash venomlash is offline Offline
Junior Poster

Re: Reading numbers from a text file

 
0
  #3
Jul 23rd, 2007
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
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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 509
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Reading numbers from a text file

 
0
  #4
Jul 23rd, 2007
Originally Posted by venomlash View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 34
Reputation: baltazar is an unknown quantity at this point 
Solved Threads: 0
baltazar baltazar is offline Offline
Light Poster

Re: Reading numbers from a text file

 
0
  #5
Jul 24th, 2007
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.

  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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 509
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Reading numbers from a text file

 
0
  #6
Jul 24th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 34
Reputation: baltazar is an unknown quantity at this point 
Solved Threads: 0
baltazar baltazar is offline Offline
Light Poster

Re: Reading numbers from a text file

 
0
  #7
Jul 25th, 2007
Ah yes, I saw my error. It was a noob mistake.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 34
Reputation: baltazar is an unknown quantity at this point 
Solved Threads: 0
baltazar baltazar is offline Offline
Light Poster

Re: Reading numbers from a text file

 
0
  #8
Jul 25th, 2007
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:
  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
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Reading numbers from a text file

 
0
  #9
Jul 25th, 2007
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) :
  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. }
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 509
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Reading numbers from a text file

 
0
  #10
Jul 25th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC