How to Read xml file in java?

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

Was this solution helpful to you?

Other answers

Magnifi glass

Related Q & A:

Just Added Q & A:

Find solution

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.