Results 1 to 7 of 7
- 06-28-2010, 03:11 PM #1
Member
- Join Date
- May 2010
- Posts
- 7
- Rep Power
- 0
read only double input from text file
hello everyone,
i have some problems regarding reading the double input and ignore the integer input..
i am writing a progran that can read the input fron a file which contains number like this:
1 1 3.0376 0.0273 3.8290
1 2 4.0762 3.7267 4.7282
2 1 5.2373 3.2828 4.2728
...
and i would like to read only the double value and ignore the integer at the first two row in the file.
i currently used inpfile.hasNextDouble() but the reader just read all the number in input file as a double..
anyone got some idea or tips please..??
:confused:
- 06-28-2010, 03:17 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
- 06-28-2010, 03:20 PM #3
Read each line of the file into a String. Split the String/line into words/tokens using String.split() or StringTokenizer then look at each word as JosAH suggested to determine if it is an integer or real number.
- 06-28-2010, 03:26 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
- 06-28-2010, 03:32 PM #5
Member
- Join Date
- May 2010
- Posts
- 7
- Rep Power
- 0
thank you for the reply,
ok, igot the idea, and as i am new in java so could you please give more details.
heres my code:
try {
inpfile = new Scanner(
new BufferedReader(new FileReader("input.dat")));
}
while (inpfile.hasNext()) {
if (inpfile.hasNextDouble() && (firstnum==1)) {
i = inpfile.nextDouble();
}
i am going to used "i" fron the input file which is double number and not the integer in further calculation.
- 06-28-2010, 03:44 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
You have a try block there without a catch clause nor a finally clause; that doesn't even compile.Summarizing: suppose you have read a String s; check if it has a dot in it (the indexOf( ... ) method would do fine for that). If it has convert it to a double (read the API for the Doubke class), otherwise ignore the String s and read a next one.
kind regards,
Jos
- 06-28-2010, 04:06 PM #7
Member
- Join Date
- May 2010
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Read text file
By Sean04 in forum New To JavaReplies: 18Last Post: 06-16-2010, 01:43 AM -
count character in text file as input file
By aNNuur in forum New To JavaReplies: 7Last Post: 03-25-2010, 04:01 PM -
read a specific line in an input file
By sara12345 in forum Advanced JavaReplies: 7Last Post: 01-03-2010, 10:40 PM -
Read from a text file?
By aaronfsimons in forum New To JavaReplies: 3Last Post: 05-01-2009, 04:42 AM -
How to read a text file from a Java Archive File
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:13 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks