![]() |
| ||
| need help getting program to sum numbers I wrote this program that reads data from a txt file named books,and then arranges the data by name and price. that part works fine ,my problem is that I want to sum up the total price of the books and display it. I've tried a couple of ways to do this but cannot get any to work.I dont seek help with my code untill I have tried for at least 2 hrs. After that I get frustrated. Any help will be greatly apprecited..Carl [ICOpublic class Readafile { public static void main(String args[]) throws IOException { File myFile = new File("books.txt");// Accesses books file Scanner inputFile = new Scanner(myFile); String line = inputFile.nextLine(); String line1 = inputFile.nextLine();// puts line & line1 on the same // line System.out.println(line + " " + line1); String line2 = inputFile.nextLine(); String line3 = inputFile.nextLine();// puts line2 & line3 on the same // line System.out.println(line2 + " " + line3); String line4 = inputFile.nextLine(); String line5 = inputFile.nextLine();// puts line4 & line5 on the same // line System.out.println(line4 + " " + line5); String line6 = inputFile.nextLine(); String line7 = inputFile.nextLine();// puts line6 & line7 on the same // page System.out.println(line6 + " " + line7); System.out.println("--------------------------"); double sum; sum = 0.0; while(inputFile.hasNext()){ double number = inputFile.nextDouble(); sum = sum + number; System.out.println("Total " + sum); } } } DE][/ICODE] |
| ||
| Re: need help getting program to sum numbers Here is something, that can help you: http://www.java2s.com/Code/JavaAPI/j...ingpattern.htm |
| ||
| Re: need help getting program to sum numbers Thanks I will check it out |
| ||
| Re: need help getting program to sum numbers [QUOTE=carlcarman;737385]Thanks I will check it out[/ That would work fine as a stand alone application but I am trying to get it to sum the numbers in my program. I know that I am very new to this but I dont see how I can make this work in my program. I am very gratefull for your help.] |
| ||
| Re: need help getting program to sum numbers As I understood, you have combined information in your file "books.txt" (name - string and price - double). To split information, that was read from your file you should use patterns (in example there is pattern to delimit using comma and different number of whitespaces " *" ). You also can use control symbols, such as "\n". //we have cycle until we'll reach the symbol,Did I answered on your question? |
| ||
| Re: need help getting program to sum numbers Both of your replys were very helpful thanks. |
| ||
| Re: need help getting program to sum numbers Quote:
|
| ||
| Re: need help getting program to sum numbers Interesting, I included it there because thats where the example in my text book had it. We are using "Starting out with JAVA .. Early Objects". Some of the guys in class say there are better texts to begin with but this is what our instructor wanted. |
| All times are GMT -4. The time now is 7:34 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC