Results 1 to 11 of 11
- 12-26-2007, 09:09 PM #1
Member
- Join Date
- Dec 2007
- Posts
- 3
- Rep Power
- 0
Need a solution to read and store data from a file
Hii everyone...I want to know how to store the input values which are read from a file. The input values are all double values in a line separated by spaces. I want to read these values one after the other and store in a double typed variable.
I actually wrote,
FileReader fr=new FileReader(filename);
BufferedReader in=new BufferedReader(fr);
while(true)
{
String line=in.readLine();//here i cud read the line
Please help me.
Thanks...
- 12-26-2007, 09:46 PM #2
Reading a file
Hello
Your code is correct, but it will result in an infinite loop. To stop this, use a condition to break out of the loop. The following code will create a vector of String objects of each line in the file.
Note that you must always close a file after using it and that the readLine() method will return null if there is no more to read. It is also important to handle any exceptions that may be thrown by the IO classes.Java Code:public static Vector<String> loadFile(String filename){ Vector<String> strings = new Vector<String>(); try{ FileReader file = new FileReader(filename); BufferedReader buffer = new BufferedReader(file); while (true) { String line = buffer.readLine(); if (line == null) break; else { strings.add(line); } } buffer.close(); } catch (Exception e) { System.out.println("Error: " + e.getMessage()); } return strings; }
;)
I do not have a IDE on this PC, so my code has been checked by hand.Last edited by tim; 10-04-2009 at 02:56 PM.
Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 12-26-2007, 10:08 PM #3
Member
- Join Date
- Dec 2007
- Posts
- 3
- Rep Power
- 0
thanks for ur post...
but my question is how to store double values of a line of a text file one by one to variables...
if the first line of input is: 1.0,1.0,90.0
and second line: 1.0,1.0,0.0
and third line: ........... and so on
First I want to read the first line and then I want to store them like
double a=1.0;
double b=1.0;
double c=90.0;
and then go for the second line and read the data and store them
and so on....
thanks again...
- 12-28-2007, 09:35 PM #4
Okay
Hello
I have attached a zip file for you. It contains an executable jar file, a "numbers.txt" file and the source code "Main.java". You can use a text editor to view the source code, like "Notepad" if you are using Windows. It can do everything that you asked and more. I like to use Vector data structures and iterators for fast coding. Just ask if you need more help. The program will increment all the numbers in the data file "numbers.txt". To see this:
- Open "numbers.txt"
- Close "numbers.txt"
- Run the jar executable
- Reopen "numbers.txt"
Just ask if you cannot access the code then I will post it.
Hope this helps.Last edited by tim; 10-04-2009 at 03:05 PM.
Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 12-04-2008, 07:21 PM #5
Member
- Join Date
- Dec 2008
- Posts
- 1
- Rep Power
- 0
java
Hello....
Can any one please help me with this problem
how to store double values of a line of a text file one by one to variables...
if the first line of input is: 1.0,2.0
1.90,2.45....
and second line: 1.0,1.0
and third line: ........... and so on
First I want to read the first line and then I want to store them like
double a=1.0;
double b=2.0;
double c=1.90;
and then go for the second line and read the data and store them
and so on....
- 12-04-2008, 07:28 PM #6
hello... hello... hello....
There seems to be an echo in this posted thread ...
Anushaanni... look in the above posts...
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 03-04-2009, 11:01 AM #7
Member
- Join Date
- Mar 2009
- Posts
- 1
- Rep Power
- 0
cannot open example.zip
can some pls re-upload the example.zip as the winzip is not opening it .
thanks
- 03-04-2009, 12:22 PM #8
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Well, seeing as how it's all null bytes, no wonder. Hopefully it was meant to be a joke on you, if not, then "shame on you, tim". (For attempting to post it at all, that does not help.)
See the API docs for Double (the parse/valueOf methods) and String (the split method), and Character Streams (The Java™ Tutorials > Essential Classes > Basic I/O)
- 10-04-2009, 03:08 PM #9
Last edited by tim; 10-04-2009 at 03:13 PM.
Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 09-29-2010, 08:14 PM #10
Member
- Join Date
- Sep 2010
- Posts
- 1
- Rep Power
- 0
Hi, I have the exact same problem but i cant see see how to get hold of your zip file tim.
thanks
- 09-30-2010, 06:43 AM #11
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Well good for you. Another cheating attempt thwarted.
Exactly what are you having a problem with? Post your code, any and all compiler/exception messages, and a complete (but brief) description of expected results vs. actual results. Then we will help you correct your code.
If you don't figure out how to do this assignment (and copying someone else's code is not solving it yourself) then you'll have an even harder time with your next assignment which will probably build of the principles learned in this assignment. Which, of course, you won't have learned if you simply copy someone else's code.
Similar Threads
-
How do you read from a file, and then store the info in an array?
By szimme101 in forum New To JavaReplies: 5Last Post: 07-30-2008, 09:30 AM -
How would you get information from a file and then store it in an array?
By szimme101 in forum Advanced JavaReplies: 3Last Post: 04-07-2008, 06:02 PM -
How to store property file into key value pair
By Java Tip in forum java.utilReplies: 0Last Post: 04-05-2008, 10:16 AM -
store file
By kazitula in forum Java AppletsReplies: 0Last Post: 02-17-2008, 09:45 PM -
how to use btree to store data
By fred in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks