digi_mind
asked on
How to read from a file ?
Could you please give me the source code for a program that reads data from a file.
Example - My data.txt contains this:
1 2 3 4 5
Now: I want to read each number, knowing that there is a white space in between each os them. Later on the program should print in the screen:
1
2
3
4
5
Can you please code this for me and post it ?
Example - My data.txt contains this:
1 2 3 4 5
Now: I want to read each number, knowing that there is a white space in between each os them. Later on the program should print in the screen:
1
2
3
4
5
Can you please code this for me and post it ?
You need a BufferedReader to read the line of numbers. Then you need a StringTokenizer to split the numbers up into substrings. And finally, if you need the actual values of the numbers, they need to be parsed using the Integer class. School assignment?
ASKER
Yes, actually is a program that I have to make for school, but the main topic is not java, but c++.
Can you code it for me ?
Can you code it for me ?
if it's for school we cannot provide you with code.
post the code you have written so far and we can guide you.
post the code you have written so far and we can guide you.
ASKER
This is what I have:
******************
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Random;
import java.util.StringTokenizer;
public class WriteText {
/** * Constructor for WriteText. */
public WriteText() {
super();
}
public static void main(String[] args) {
String SEP = " ";
String FILENAME = "data.txt";
try {
StringTokenizer st = null;
BufferedReader reader = new BufferedReader(new FileReader(FILENAME));
String s = null;
String[][] myArray = new String[1][6];
int col;
int row;
String element = null;
while ((s = reader.readLine()) != null ) {
st = new StringTokenizer(s,SEP);
element = st;
}
System.out.println("VALUE : " + element );
reader.close();
}catch(Exception fe) {
fe.printStackTrace(System. out);
}
}
}
************************** ********** **********
My data.txt file contains 7 strings:
one two three four five six seven
I would like to have this strings in an array, then print in the screen position 4 of the array, so I will print:
four
Give me a hand, I am almost there...
******************
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Random;
import java.util.StringTokenizer;
public class WriteText {
/** * Constructor for WriteText. */
public WriteText() {
super();
}
public static void main(String[] args) {
String SEP = " ";
String FILENAME = "data.txt";
try {
StringTokenizer st = null;
BufferedReader reader = new BufferedReader(new FileReader(FILENAME));
String s = null;
String[][] myArray = new String[1][6];
int col;
int row;
String element = null;
while ((s = reader.readLine()) != null ) {
st = new StringTokenizer(s,SEP);
element = st;
}
System.out.println("VALUE : " + element );
reader.close();
}catch(Exception fe) {
fe.printStackTrace(System.
}
}
}
**************************
My data.txt file contains 7 strings:
one two three four five six seven
I would like to have this strings in an array, then print in the screen position 4 of the array, so I will print:
four
Give me a hand, I am almost there...
ASKER
My new code and better:
************************
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Random;
import java.util.StringTokenizer;
public class WriteText {
/** * Constructor for WriteText. */
public WriteText() {
super();
}
public static void main(String[] args) {
String SEP = " ";
String FILENAME = "data.txt";
try {
StringTokenizer st = null;
BufferedReader reader = new BufferedReader(new FileReader(FILENAME));
String s = null;
String[] myArray = new String[3];
int col = 0;
String element = null;
while ((s = reader.readLine()) != null ) {
st = new StringTokenizer(s,SEP);
element = (String) st.nextElement();
myArray[col] = element;
col = col + 1 ;
}
System.out.println("VALUE : " + myArray[0] );
reader.close();
}catch(Exception fe) {
fe.printStackTrace(System. out);
}
}
}
************************** ********** ********** ****
The problem is that when I print myArray[1] I get a value of null... and when I print myArray[0] I get "one" so the first value works...
i need to be able to store the other values in my array .
How ?
************************
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Random;
import java.util.StringTokenizer;
public class WriteText {
/** * Constructor for WriteText. */
public WriteText() {
super();
}
public static void main(String[] args) {
String SEP = " ";
String FILENAME = "data.txt";
try {
StringTokenizer st = null;
BufferedReader reader = new BufferedReader(new FileReader(FILENAME));
String s = null;
String[] myArray = new String[3];
int col = 0;
String element = null;
while ((s = reader.readLine()) != null ) {
st = new StringTokenizer(s,SEP);
element = (String) st.nextElement();
myArray[col] = element;
col = col + 1 ;
}
System.out.println("VALUE : " + myArray[0] );
reader.close();
}catch(Exception fe) {
fe.printStackTrace(System.
}
}
}
**************************
The problem is that when I print myArray[1] I get a value of null... and when I print myArray[0] I get "one" so the first value works...
i need to be able to store the other values in my array .
How ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you need to loop on your enumeration (while loop):
use < st.hasMoreElements(); > to test if you have more elements.
use < st.hasMoreElements(); > to test if you have more elements.