Results 1 to 9 of 9
- 03-22-2010, 06:58 AM #1
Member
- Join Date
- Nov 2007
- Posts
- 2
- Rep Power
- 0
Scanning text file and inserting contents into array
Hi, am trying to read a text file using scanner in java and inserting the contents
into two arrays. One is 1D and the other is 2D.The content of the text file is as
follows:
Nancy -1.290 36.820
Mom -4.040 39.660
Nelly -0.280 36.070
Kim -0.090 34.750
Elly 0.52 35.270
Tom -1.040 37.090
Ron -1.140 36.960
Kelly 1.030 34.990
Oscar 0.290 34.730
Here is my code sample which is'nt working:
Any help or suggestions are highly welcomed.Java Code:import java.io.File; import java.io.FileReader; import java.util.Scanner; public class Names { public static void main(String[] args) throws Exception { File file = null; FileReader fr = null; Scanner sc = null; int count = 0; int count2 = 0; String name[] = new String[25]; float codes[][] = new float[25][2]; try { file = new File("namedata.txt"); fr = new FileReader(file); sc = new Scanner(fr); while(sc.hasNext()){ name[count] = sc.next(); codes[count][count2] = sc.nextFloat(); codes[count][(count2+1)] = sc.nextFloat(); count++; count2 = 0; //reset count2 System.out.println("Names = "+name[count]+"\n"); } }catch (Exception ex) { ex.printStackTrace(); }finally { fr.close(); sc.close(); } } }
- 03-23-2010, 01:53 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
is not legit.Java Code:float codes[][] = new float[25][2];
instead, you need to do
Java Code:float codes [][] = new float [25][]; for (int i = 0; i < 25; i ++) { codes [i] = new float [2]; }
- 03-23-2010, 02:58 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Static allocations can cause lots of issues in runtime. For an example if your file content is more than 25 lines, then how can you read them all?
- 03-23-2010, 02:59 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 03-23-2010, 10:36 AM #5
Member
- Join Date
- Mar 2010
- Posts
- 22
- Rep Power
- 0
Can I ask why this isn't legit?
I have a piece of code that uses
and it works exactly as I want. Your replacement code seems to make a 2D array but give the second dimension no value, then populate the first column with new Arrays (suggesting you don't need the second dimension at all?). I would just like to understand better. Thanks.Java Code:public static int[][] cache = new int[numBlocks][blockSize];
- 03-23-2010, 01:46 PM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yeah, that's why ask the explanation from iluxa. The way you define the array is correct. There are many ways to initialize an arrays in Java. This is one of them.
- 03-23-2010, 01:53 PM #7
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Is this specifically an exercise about loading data into arrays? Because if it is not, I would recommend a more object-oriented approach. Let me know if you want more explanation.
-Gary-
- 03-23-2010, 01:57 PM #8
Member
- Join Date
- Mar 2010
- Posts
- 22
- Rep Power
- 0
I thought as much, thanks for clearing that up. If the OP could post exactly what problem is occuring (what happens when you run it?) it would be easier to help out.
- 03-24-2010, 12:33 PM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
He may be still workaround on that. :p
Similar Threads
-
How can I get the file contents to display as readable text?
By xtiano77 in forum New To JavaReplies: 6Last Post: 03-12-2010, 04:54 AM -
problem trying to display the contents of a text file in JTextArea
By warship in forum New To JavaReplies: 17Last Post: 07-13-2009, 05:44 AM -
problems trying to view the contents of a text file in JTextArea
By warship in forum New To JavaReplies: 1Last Post: 07-18-2007, 11:20 PM -
problem trying to view the contents of a text file in JTextArea
By warship in forum AWT / SwingReplies: 0Last Post: 07-17-2007, 03:30 PM -
viewing the contents of a text file in JTextArea
By warship in forum New To JavaReplies: 0Last Post: 07-17-2007, 02:29 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks