14 Replies - 1290 Views - Last Post: 18 November 2008 - 07:37 PM Rate Topic: -----

#1 carman69  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 19-August 08

cannot get program to sum numbers

Post icon  Posted 16 November 2008 - 12:25 PM

Hi,
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]

Is This A Good Question/Topic? 0
  • +

Replies To: cannot get program to sum numbers

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: cannot get program to sum numbers

Posted 16 November 2008 - 12:49 PM

You read most of the file before starting to sum. What is the format of the file?
Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: cannot get program to sum numbers

Posted 16 November 2008 - 12:51 PM

Please post your code examples using :code:

View Postcarman69, on 16 Nov, 2008 - 11:25 AM, said:

Hi,
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?
Was This Post Helpful? 0
  • +
  • -

#4 carman69  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 19-August 08

Re: cannot get program to sum numbers

Posted 16 November 2008 - 01:04 PM

View Postg00se, on 16 Nov, 2008 - 11:49 AM, said:

You read most of the file before starting to sum. What is the format of the file?



its a text file

View Postn8wxs, on 16 Nov, 2008 - 11:51 AM, said:

Please post your code examples using :code:

View Postcarman69, on 16 Nov, 2008 - 11:25 AM, said:

Hi,
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
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: cannot get program to sum numbers

Posted 16 November 2008 - 01:55 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);



Was This Post Helpful? 0
  • +
  • -

#6 carman69  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 19-August 08

Re: cannot get program to sum numbers

Posted 16 November 2008 - 02:16 PM

[quote name='g00se' date='16 Nov, 2008 - 12:55 PM' post='465870']
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? ]
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: cannot get program to sum numbers

Posted 16 November 2008 - 02:23 PM

With your file and the java book set to 100, it gives me:

math
science
algebra
java
Total 463.40999999999997



Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: cannot get program to sum numbers

Posted 16 November 2008 - 02:28 PM

[quote name='carman69' date='16 Nov, 2008 - 01:16 PM' post='465890']

View Postg00se, on 16 Nov, 2008 - 12:55 PM, said:

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? ]


Won't work with the last line which has "priceless" as the price for Java
Was This Post Helpful? 1
  • +
  • -

#9 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: cannot get program to sum numbers

Posted 16 November 2008 - 02:29 PM

View Postcarman69, on 16 Nov, 2008 - 12:04 PM, said:

Yes the first line is the book and the next the price.
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());

   ...


Was This Post Helpful? 0
  • +
  • -

#10 carman69  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 19-August 08

Re: cannot get program to sum numbers

Posted 16 November 2008 - 02:31 PM

[quote name='g00se' date='16 Nov, 2008 - 01:23 PM' post='465896']
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.]
Was This Post Helpful? 0
  • +
  • -

#11 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: cannot get program to sum numbers

Posted 16 November 2008 - 02:36 PM

IDEs do not have such an effect on the output. (Not counting where it displays its console, etc...)
Was This Post Helpful? 1
  • +
  • -

#12 carman69  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 19-August 08

Re: cannot get program to sum numbers

Posted 16 November 2008 - 05:44 PM

View PostKYA, on 16 Nov, 2008 - 01:36 PM, said:

IDEs do not have such an effect on the output. (Not counting where it displays its console, etc...)

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

Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2687
  • View blog
  • Posts: 11,349
  • Joined: 20-September 08

Re: cannot get program to sum numbers

Posted 17 November 2008 - 03:19 AM

Well make sure you changed 'priceless' into a price. That line only makes sense as a joke and I'm afraid that perhaps you could be disappointed to discover, as a student of computer science, that computers have absolutely no sense of humour
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: cannot get program to sum numbers

Posted 17 November 2008 - 05:43 AM

View Postg00se, on 17 Nov, 2008 - 02:19 AM, said:

Well make sure you changed 'priceless' into a price. That line only makes sense as a joke and I'm afraid that perhaps you could be disappointed to discover, as a student of computer science, that computers have absolutely no sense of humour

lol :)
Was This Post Helpful? 0
  • +
  • -

#15 carman69  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 19-August 08

Re: cannot get program to sum numbers

Posted 18 November 2008 - 07:37 PM

View Postg00se, on 17 Nov, 2008 - 02:19 AM, said:

Well make sure you changed 'priceless' into a price. That line only makes sense as a joke and I'm afraid that perhaps you could be disappointed to discover, as a student of computer science, that computers have absolutely no sense of humour



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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1