How do i get my java program to read doubles one by one from a txt file?
-
i know how to get stuff from the file, but in strings how do i get numbers from the file in double instead of string because when i change string to double i just get error messages and i need to get the doubles one by one (i am trying to find the average of all the numbers in the file) i know how to find the number of numbers (numbers++; after everytime a number is being taken into the sum) but i need to know how to find the sum so far this is what i have ______________________________________… import java.io.File; import java.util.Scanner; public class Average { public static void Average(String filePath){ Scanner in; try{ in = new Scanner(new File(filePath)); String test = ""; while(in.hasNext()){ test = in.nextLine(); if(!test.equals("")) System.out.println(test); } }catch (Exception ioe){ System.out.println("Error: " + ioe.getMessage()); } } public static void main(String[]args){ Average.Average("C:/Documents and Settings/GRC/Desktop/AP COMP SCI/Average/numbers.txt"); System.out.println(""); } }
-
Answer:
Take a look at this. You can see what you need to do. import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class MainClass { public static void main(String args[]) throws IOException { int i; double d; boolean b; String str; FileWriter fout = new FileWriter("test.txt"); fout.write("Testing Scanner 10 12.2 one true two false"); fout.close(); FileReader fin = new FileReader("Test.txt"); Scanner src = new Scanner(fin); while (src.hasNext()) { if (src.hasNextInt()) { i = src.nextInt(); System.out.println("int: " + i); } else if (src.hasNextDouble()) { d = src.nextDouble(); System.out.println("double: " + d); } else if (src.hasNextBoolean()) { b = src.nextBoolean(); System.out.println("boolean: " + b); } else { str = src.next(); System.out.println("String: " + str); } } fin.close(); } } And Look at this to see which methods the scanner class has. http://leepoint.net/notes-java/summaries/summary-scanner.html
BB at Yahoo! Answers Visit the source
Other answers
Magnifi glass
Related Q & A:
- How do I cancel my account so I can get a new one?Best solution by Yahoo! Answers
- How do I get a read receipt?Best solution by Yahoo! Answers
- How do I get a video file off a sony video camera disk?Best solution by Yahoo! Answers
- How can I get into a graduate program without a stellar GPA?Best solution by answers.yahoo.com
- How can I get into my Yahoo mail that I haven't used for a while?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.