Input from a txt file!
Author |
Message |
Bhai
|
Posted: Wed May 23, 2007 7:17 pm Post subject: 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
Description: |
If we have a txt file that looks like this how can i read the imformation and input it in seprate arrays for mass and volume and title etx and output this on the screen in a table format |
|
Filesize: |
23.61 KB |
Viewed: |
79 Time(s) |
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
syntax_error
|
Posted: Mon Sep 17, 2007 8:47 pm Post subject: Re: Input from a txt file! |
|
|
I am not sure but I can refer you to this site here
its a nice tut
you can read it and see if you understand
|
|
|
|
|
|
Euphoracle
|
Posted: Mon Sep 17, 2007 9:23 pm Post subject: 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.
Java: | import java.io.*;
import java.util.Vector;
class DanDictionary {
public static void main ( String[] arg ) {
if ( arg. length < 2 ) {
System. out. println( "Syntax: DanDictionary <readPath> <writePath>" );
System. exit( 0 );
return;
}
String readPath = arg [ 0 ], writePath = arg [ 1 ];
try {
// Open an input stream
BufferedReader in = new BufferedReader( new FileInputStream( readPath ) );
// Open an output stream
PrintStream out = new PrintStream( new FileOutputStream( writePath ) );
// Temp vars
String buff = "";
String curLetter = null;
boolean isHackerDan = false;
Vector english = new Vector( );
Vector hackerDan = new Vector( );
// Header
out. println( "// DanDictionary - Euphoracle\n" );
out. println( "on *:TEXT:*:* {\n" );
out. println( " if ( Dan isin $nick ) {\n" );
out. println( " set %msg $1-\n" );
// Do the work
while ( ( buff = in. readLine( ) ) != null ) {
if ( buff == "" || buff. length( ) < 1 || buff. contains( "[edit]" ) ) {
// Do nothing!
} else if ( buff. charAt( 0 ) == '-' ) {
if ( curLetter != null ) {
for ( int i = 0; i < english. size( ); i++ ) {
out. println( " set %msg $replace( %msg, " + ( String )hackerDan. get( i ) + ", " + ( String )english. get( i ) + " )" );
}
}
curLetter = Character. toString( buff. charAt( 1 ) );
isHackerDan = false;
english. clear( );
hackerDan. clear( );
} else if ( buff. contains( "\t" ) ) {
isHackerDan = true;
} else {
if ( isHackerDan ) {
hackerDan. add( buff );
System. out. println( " HackerDanish: " + buff );
} else {
english. add( buff );
System. out. println( " English: " + buff );
}
}
}
// Finish it up
out. println( "\n if ( %msg != $1- ) {\n" );
out. println( " echo -a Translation: (Dan) %msg" );
out. println( "\n }" );
out. println( "\n }" );
out. println( "\n}\n" );
// Close our streams
in. close( );
out. close( );
System. out. println( "File written successfully." );
} catch ( IOException e ) {
System. out. println ( "Unable to write to file" );
System. exit( 0 );
return;
}
}
} |
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.
I'll comment the rest of it for you to understand.
Java: |
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[ ][ ] massAndVolume = new double[ 2 ][ dataCount ];
// 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 [ c ][ i ] = Double. parseDouble( in. readLine( ) ); // Will throw a nasty eror if it doesn't get what it wants
}
}
// We're done, lets close the stream
in. close( );
// Do something with it?
System. out. println( "Data for " + experimentNumber + " (" + experimentTitle + ")" );
for ( int c = 0; c < 2; c++ )
for ( int i = 0; i < dataCount; i++ )
System. out. println( ( ( c == 0 ) ? "Mass: " : "Volume: " ) + massAndVolume [ c ][ i ] );
// Or do something else, just an example.
} catch ( Exception e ) {
// Obviously you should specialize the exceptions, but this is just an example, right?
System. out. println( "Error!" );
e. printStackTrace( );
}
|
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:
Java: |
// 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 [ i ][ c ] = Double. parseDouble( in. readLine( ) ); // Will throw a nasty eror if it doesn't get what it wants
}
}
|
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.
|
|
|
|
|
|
|
|