22 Replies - 672 Views - Last Post: 10 October 2011 - 08:43 PM
#1
Finding the sum of the file input
Posted 10 October 2011 - 07:06 PM
Replies To: Finding the sum of the file input
#2
Re: Finding the sum of the file input
Posted 10 October 2011 - 07:13 PM
That being said, I want you to start by telling us where you're starting from.
How do you think this problem gets solved? Break it down into steps, so you can solve it (with a little help, but you're going to be doing the solving)
This post has been edited by jon.kiparsky: 10 October 2011 - 07:14 PM
#3
Re: Finding the sum of the file input
Posted 10 October 2011 - 07:21 PM
#4
Re: Finding the sum of the file input
Posted 10 October 2011 - 07:24 PM
#5
Re: Finding the sum of the file input
Posted 10 October 2011 - 07:24 PM
Lemonade4sale, on 10 October 2011 - 07:06 PM, said:
Here is the program description
Find the sum and average for a set of uncounted set of test scores. The last number in the data will be zero and should not be processed. Print the scores in a column as they are read in. Print the sum, number of scores and average using appropriate labels.
This program requires reading an external file. First you must download the file to the same subdirectory where you are going to save your file. You can also see a sample program on how to read and external file under the button “How do you do this?”.
and..
Here is the code I have so far
import java.util.Scanner;
import java.io.*;
public class SumAvg
{
public static void main(String args[])
{
Scanner inFile = null;
try
{
inFile = new Scanner (new File ("Data Tester.txt"));
}
catch (FileNotFoundException e)
{
System.out.println("File not found!");
System.exit(0);
}
String text[] = new String[100];
System.out.println("Scores");
int numb = inFile.nextInt();
while (numb !=0 )
{
System.out.println(numb);
numb = inFile.nextInt();
}
System.out.println();
inFile.close();
}
}
This post has been edited by macosxnerd101: 10 October 2011 - 07:25 PM
Reason for edit:: Please use code tags
#6
Re: Finding the sum of the file input
Posted 10 October 2011 - 07:26 PM
#7
Re: Finding the sum of the file input
Posted 10 October 2011 - 07:35 PM
Now, what do you want to do next? I see that you declare an array, but I'm not sure what it's for. The loop looks more promising. You've got the loop condition correct, so you're doing quite well so far.
Now what happens in the loop?
Imagine that you're trying to solve this problem on paper: you have a sheet of scratch paper and a list of numbers. You take each number one at a time, and if it's zero, you stop. What do you do if it's not zero?
#8
Re: Finding the sum of the file input
Posted 10 October 2011 - 07:42 PM
jon.kiparsky, on 10 October 2011 - 07:35 PM, said:
Now, what do you want to do next? I see that you declare an array, but I'm not sure what it's for. The loop looks more promising. You've got the loop condition correct, so you're doing quite well so far.
Now what happens in the loop?
Imagine that you're trying to solve this problem on paper: you have a sheet of scratch paper and a list of numbers. You take each number one at a time, and if it's zero, you stop. What do you do if it's not zero?
If the numbers are not zero you continue to loop.
#9
Re: Finding the sum of the file input
Posted 10 October 2011 - 07:43 PM
#10
Re: Finding the sum of the file input
Posted 10 October 2011 - 07:45 PM
int numb = inFile.nextInt();
while (numb !=0 )
{
System.out.println(numb);
numb = inFile.nextInt();
}
assumes that the file ends with a 0 as the last integer in the file
A better approach would be
while (inFile.hasNextInt())
{
int numb = inFile.nextInt();
System.out.println(numb);
}
And you can cumulate the numb into a sum inside the while()loop as macosxnerd101 suggested
#11
Re: Finding the sum of the file input
Posted 10 October 2011 - 07:50 PM
pbl, on 10 October 2011 - 07:45 PM, said:
int numb = inFile.nextInt();
while (numb !=0 )
{
System.out.println(numb);
numb = inFile.nextInt();
}
assumes that the file ends with a 0 as the last integer in the file
A better approach would be
while (inFile.hasNextInt())
{
int numb = inFile.nextInt();
System.out.println(numb);
}
And you can cumulate the numb into a sum inside the while()loop as macosxnerd101 suggested
The file does end with a 0 as the last integer.
#12
Re: Finding the sum of the file input
Posted 10 October 2011 - 07:53 PM
#13
Re: Finding the sum of the file input
Posted 10 October 2011 - 07:54 PM
Quote
If he used your method, he'd then have to remove the zero from his tally, or his numbers would be off...
(ninja'd...)
This post has been edited by jon.kiparsky: 10 October 2011 - 07:54 PM
#14
Re: Finding the sum of the file input
Posted 10 October 2011 - 07:56 PM
jon.kiparsky, on 10 October 2011 - 07:43 PM, said:
I am sorry I just do not understand what you are saying, or what I am suppose to do.
#15
Re: Finding the sum of the file input
Posted 10 October 2011 - 08:00 PM
|
|

New Topic/Question
Reply



MultiQuote







|