| | |
need help getting program to sum numbers
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 20
Reputation:
Solved Threads: 0
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]
[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]
Here is something, that can help you:
http://www.java2s.com/Code/JavaAPI/j...ingpattern.htm
http://www.java2s.com/Code/JavaAPI/j...ingpattern.htm
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
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".
Did I answered on your question?
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".
java Syntax (Toggle Plain Text)
//we have cycle until we'll reach the symbol, //what hasn't next element while(src.hasNext()) { //if next value is double //(it is just checking next value, not goes to it) if(src.hasNextDouble()) { //we take this value and add to sum sum += src.nextDouble(); } //if the next value is not double //in your case you should pass through this block else { //we take this element as string String str = src.next(); //if this string equals("done"), we are //coercive breaking the cycle if(str.equals("done")) break; else { //if this is not "done" we have troubles :) System.out.println("File format error."); return; } } }
Last edited by Antenka; Nov 16th, 2008 at 9:49 pm.
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
![]() |
Similar Threads
- add even (or odd) numbers from input (C++)
- Sum of numbers will not display (Assembly)
- need help on pep/8 machine language program (Assembly)
- need help, need to generate 10 random numbers, such that their sum is less than 1 (C)
- what is C++ builder code for sum (C++)
- C++ program help please........ (C++)
- Help with a problem with C++ program (C++)
- Factorial program (Java)
Other Threads in the Java Forum
- Previous Thread: need working code in java of SMTP and pop3
- Next Thread: Question about Arrays
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation bi binary blackberry block bluetooth character class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp game gameprogramming givemetehcodez graphics gui health html ide image integer j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux list login loops mac main map method methods mobile netbeans notdisplaying number online printf problem program project properties qt recursion researchinmotion rotatetext rsa scanner screen server set singleton sms sort sql string swing system textfields threads time title tree tutorial-sample update variablebinding windows working xor





