
-----------------------------------
Bhai
Wed May 23, 2007 7:17 pm

Input from a txt file!
-----------------------------------
Hey I just wanted to know how to inputs  a specific value from a specific line ex. from line 2 i want to pull out the number and put it into a 2-D Array. The thing is I can read the txt file but i dont know how to put a a specific value in a 3-array 

for example i have a txt file with the following information

Experiment #9
Mass vs Volume
10
63.5
110.89
432.0
239.7
42.8
54.678
984.3
76.01
85.4
109.42
11.5
19.3
75
41.3
7.51
9.6
170.96
13.77
15.23
20.1


i can read the file but dont know how to put it in 2d arrays and then outpout the result if you do can i get some help

-----------------------------------
syntax_error
Mon Sep 17, 2007 8:47 pm

Re: Input from a txt file!
-----------------------------------
I am not sure but I can refer you to this site [url=http://java.sun.com/docs/books/tutorial/essential/io/]here
its a nice tut
you can read it and see if you understand

-----------------------------------
Euphoracle
Mon Sep 17, 2007 9:23 pm

RE:Input from a txt file!
-----------------------------------
Well, here's something I wrote to create an mirc script of "Hacker Dan's Dictionary".  It has a basic view of how to read/write files using streams.  If you need clarification, just ask.

import java.io.*;
import java.util.Vector;

class DanDictionary {
  
  public static void main( String


Essentially, you'd first want to create an object that can be used for sequential reading.  We'll do this using the BufferedReader and FileInputStream classes.

      // Open an input stream
      BufferedReader in = new BufferedReader( new FileInputStream( "PathToFile.txt" ) );


I'll comment the rest of it for you to understand.


    try {
	
	  // Open an input stream
      BufferedReader in = new BufferedReader( new FileInputStream( "PathToFile.txt" ) );
	  
	  // Acquire our information
	  String experimentNumber = in.readLine( ); // Read the first line
	  String experimentTitle = in.readLine( ); // The second
	  int dataCount = Integer.parseInt( in.readLine( ) ); // Get the third line.  This can throw a nasty exception if it doesn't get what it wants
	  
	  // Prepare our holder
	  double

The variables will contain the following information:

experimentNumber - Experiment #9
experimentTitle - Mass vs Volume
dataCount - 5
massAndVolume:
Major Index (0)
0 - 63.5
1 - 110.89
2 - 432.0
3 - 239.7
4 - 42.8 
Second Index (1)
0 - 11.5
1 - 19.3
2 - 75
3 - 41.3
4 - 7.51 

The output if you were to run this should be (untested, written in notepad, etc.):
> Data for Experiment #9 (Mass vs Volume)
Mass:  63.5
Mass:  110.89
Mass:  432.0
Mass:  239.7
Mass:  428
Volume:  11.5
Volume:  19.3
Volume:  75
Volume:  41.3
Volume:  7.51

If you wanted it, however to work in the opposite (IE, major index is the row, and second index is the mass or volume) you could change the following section:


	  // Now we have our count, so lets loop that many times, twice
	  for ( int c = 0; c < 2; c++ ) {
	  
	    for ( int i = 0, i < dataCount; i++ ) { // This will allow us to insert it into the correct indice of the first dimension, and the second
		
		  massAndVolume

This will change the format of massAndVolume to:
Major Index (0)
0 - 63.4
1 - 11.5
Second Index (1)
0 - 110.89
1 - 19.3
etc.
