cannot get program to sum numbers
Page 1 of 114 Replies - 1179 Views - Last Post: 18 November 2008 - 07:37 PM
#1
cannot get program to sum numbers
Posted 16 November 2008 - 12:25 PM
I have a program that gets data from a txt file named books. this file contains books and their cost. my program gets the data from this file then rearranges it into a list. this works fine,but when I try to total the list it just displays the list without totals. I am stumped, any help would be greatly appreciated. here's my code.[copackage Readafile;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
/**
* @author carl carman
*
*/
public 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]
Replies To: cannot get program to sum numbers
#2
Re: cannot get program to sum numbers
Posted 16 November 2008 - 12:49 PM
#3
Re: cannot get program to sum numbers
Posted 16 November 2008 - 12:51 PM
carman69, on 16 Nov, 2008 - 11:25 AM, said:
I have a program that gets data from a txt file named books. this file contains books and their cost. my program gets the data from this file then rearranges it into a list. this works fine,but when I try to total the list it just displays the list without totals. I am stumped, any help would be greatly appreciated. here's my code.[copackage Readafile;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
/**
* @author carl carman
*
*/
public 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);
}
}
You double up the lines to display them. Is the book price on that second line?
#4
Re: cannot get program to sum numbers
Posted 16 November 2008 - 01:04 PM
g00se, on 16 Nov, 2008 - 11:49 AM, said:
its a text file
n8wxs, on 16 Nov, 2008 - 11:51 AM, said:
carman69, on 16 Nov, 2008 - 11:25 AM, said:
I have a program that gets data from a txt file named books. this file contains books and their cost. my program gets the data from this file then rearranges it into a list. this works fine,but when I try to total the list it just displays the list without totals. I am stumped, any help would be greatly appreciated. here's my code.[copackage Readafile;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
/**
* @author carl carman
*
*/
public 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);
}
}
You double up the lines to display them. Is the book price on that second line?
Yes the first line is the book and the next the price.
math
128.98
science
134.77
algebra
99.66
java
priceless
#5
Re: cannot get program to sum numbers
Posted 16 November 2008 - 01:55 PM
double sum = 0.0;
int ix = 0;
while(inputFile.hasNext()){
if (ix++ % 2 != 0) {
sum += inputFile.nextDouble();
}
else {
System.out.println(inputFile.next());
}
}
System.out.println("Total " + sum);
#6
Re: cannot get program to sum numbers
Posted 16 November 2008 - 02:16 PM
Try something like this only:
double sum = 0.0;
int ix = 0;
while(inputFile.hasNext()){
if (ix++ % 2 != 0) {
sum += inputFile.nextDouble();
}
else {
System.out.println(inputFile.next());
}
}
System.out.println("Total " + sum);
[ First thanks for your response. The total this gives me is 0.0 which is better than the nothing I was getting earlier , now I have to try and find a way for it to total the numbers.Any suggestions? ]
#7
Re: cannot get program to sum numbers
Posted 16 November 2008 - 02:23 PM
math science algebra java Total 463.40999999999997
#8
Re: cannot get program to sum numbers
Posted 16 November 2008 - 02:28 PM
g00se, on 16 Nov, 2008 - 12:55 PM, said:
double sum = 0.0;
int ix = 0;
while(inputFile.hasNext()){
if (ix++ % 2 != 0) {
sum += inputFile.nextDouble();
}
else {
System.out.println(inputFile.next());
}
}
System.out.println("Total " + sum);
[ First thanks for your response. The total this gives me is 0.0 which is better than the nothing I was getting earlier , now I have to try and find a way for it to total the numbers.Any suggestions? ]
Won't work with the last line which has "priceless" as the price for Java
#9
Re: cannot get program to sum numbers
Posted 16 November 2008 - 02:29 PM
carman69, on 16 Nov, 2008 - 12:04 PM, said:
math
128.98
science
134.77
algebra
99.66
java
priceless
public static void main(String args[]) throws IOException {
double sum = 0.0;
double temp;
File myFile = new File("books.txt");// Accesses books file
Scanner inputFile = new Scanner(myFile);
String line = inputFile.nextLine();
temp = inputFile.nextDouble);
sum += temp;
System.out.println(line + " " + temp.toString());
...
#10
Re: cannot get program to sum numbers
Posted 16 November 2008 - 02:31 PM
With your file and the java book set to 100, it gives me:
math science algebra java Total 463.40999999999997
[ Really because I copied your code into it and it still gives me zero's. I am using eclipse that shouldnt make a difference should it? Thanks for your help I will try and see what I am doing wrong.]
#11
Re: cannot get program to sum numbers
Posted 16 November 2008 - 02:36 PM
#12
Re: cannot get program to sum numbers
Posted 16 November 2008 - 05:44 PM
KYA, on 16 Nov, 2008 - 01:36 PM, said:
I didnt think the IDE would make a difference either, I just can't understand why I can't get it to work when PBL says he did.
This post has been edited by carman69: 16 November 2008 - 05:45 PM
#13
Re: cannot get program to sum numbers
Posted 17 November 2008 - 03:19 AM
#14
Re: cannot get program to sum numbers
Posted 17 November 2008 - 05:43 AM
#15
Re: cannot get program to sum numbers
Posted 18 November 2008 - 07:37 PM
g00se, on 17 Nov, 2008 - 02:19 AM, said:
Your right it was an attempt at humor. pbl's code does work for summing the books file by itself. I was trying to get it to work in my program so I wouldnt have to do a rewrite. Finally I rewrote it to accomadate a running total and it is working fine.
|
|

New Topic/Question
Reply




MultiQuote



|