Im so confused! Counting and Summing
I am trying to make a program opens a file called input.txt and sums all of the numbers in it along with counting the number of numbers.
So far i have
Code Java:
import java.util.Scanner;
import java.io.*;
import java.io.File;
import java.io.IOException;
public class test1 {
public static void main(String[] args) throws IOException {
String fileName = "input.txt";
Scanner fileScan = new Scanner(new File(fileName));
int sum = 0;
int count = 0;
File f = new File(fileName);
Scanner scanFile = new Scanner (fileName);
String line;
while(scanFile.hasNext()) {
line = scanFile.next();
count++;
String integer;
while (scanFile.hasNext()){
integer = scanFile.next();
Im really confused on how to add up integers from each line and save it to sum.
Re: Im so confused! Counting and Summing
Quote:
Originally Posted by
captiancd89
I am trying to make a program opens a file called input.txt and sums all of the numbers in it along with counting the number of numbers.
So far i have
import java.util.Scanner;
import java.io.*;
import java.io.File;
import java.io.IOException;
public class test1 {
public static void main(String[] args) throws IOException {
String fileName = "input.txt";
Scanner fileScan = new Scanner(new File(fileName));
int sum = 0;
int count = 0;
File f = new File(fileName);
Scanner scanFile = new Scanner (fileName);
String line;
while(scanFile.hasNext()) {
line = scanFile.next();
count++;
String integer;
while (scanFile.hasNext()){
integer = scanFile.next();
Im really confused on how to add up integers from each line and save it to sum.
My brain is kinda fried from fiddling with an evil UNIX program, but I think that if you want to sum,
you do this:
int x = scanFile.nextInt();
although if that won't work, try scanFile.nextInteger(), though I think it's nextInt().
However, you'll have to do this many times I think. Not quite sure on the specifics.
Re: Im so confused! Counting and Summing
If your input.txt file contains something like this:
10
20
30
40
50
60
70
80
90
100
Then you could do this:
Code Java:
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class test1 {
public static void main(String[] args) throws IOException {
int sum = 0;
int count = 0;
int size = 0;
String fileName = "input.txt";
Scanner fileScan = new Scanner(new File(fileName));
String line;
// Get amount of lines for setting up array
while (fileScan.hasNext()) {
line = fileScan.nextLine();
size++;
}
fileScan.close();
//System.out.println("Number of lines: " + size);
fileScan = new Scanner(new File(fileName));
// Setup array to store numbers
String[] array = new String[size];
while (fileScan.hasNext()) {
line = fileScan.nextLine();
// Add line to array
array[count] = line;
count++;
}
// Print contents of Array
for (int a = 0; a < array.length; a++){
System.out.println(array[a]);
// Math stuff here to add numbers..
}
}
}
It will read the file line by line and add each number to an array.
size will be the amount of numbers in the file.
You need to add the sum code to the loop which iterates through the array.
This should also help you:
http://www.javaprogrammingforums.com...tring-int.html
Re: Im so confused! Counting and Summing
Quote:
Originally Posted by
JavaPF
If your input.txt file contains something like this:
10
20
30
40
50
60
70
80
90
100
Then you could do this:
Code Java:
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class test1 {
public static void main(String[] args) throws IOException {
int sum = 0;
int count = 0;
int size = 0;
String fileName = "input.txt";
Scanner fileScan = new Scanner(new File(fileName));
String line;
// Get amount of lines for setting up array
while (fileScan.hasNext()) {
line = fileScan.nextLine();
size++;
}
fileScan.close();
//System.out.println("Number of lines: " + size);
fileScan = new Scanner(new File(fileName));
// Setup array to store numbers
String[] array = new String[size];
while (fileScan.hasNext()) {
line = fileScan.nextLine();
// Add line to array
array[count] = line;
count++;
}
// Print contents of Array
for (int a = 0; a < array.length; a++){
System.out.println(array[a]);
// Math stuff here to add numbers..
}
}
}
It will read the file line by line and add each number to an array.
size will be the amount of numbers in the file.
You need to add the
sum code to the loop which iterates through the array.
This should also help you:
http://www.javaprogrammingforums.com...tring-int.html
Why read the file twice? Why not just read it once, sum the numbers you read, and keep a counter of how many numbers you have read?
Re: Im so confused! Counting and Summing
This is just an example..
Try to implement your method :)
Re: Im so confused! Counting and Summing
Why would you need an array just have
int count = 0;
int sum = 0;
while(file.hasNext()) {
sum+= (int)(Integer.parseInt(fileScan.nextLine());
count++;
}
Re: Im so confused! Counting and Summing
Quote:
Originally Posted by
JavaPF
This is just an example.. I read the file first so I could declare the array size. Then the second time it adds the numbers to the array.
Try to implement your method :)
Ok... I just won't store the numbers I read in, in an array because that isn't a requirement.
Hey look Zula implemented it. I'm sure it was really hard!