Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Im so confused! Counting and Summing

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    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.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Im so confused! Counting and Summing

    Quote Originally Posted by captiancd89 View Post
    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.

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default 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:

    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
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. #4
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: Im so confused! Counting and Summing

    Quote Originally Posted by JavaPF View Post
    If your input.txt file contains something like this:

    10
    20
    30
    40
    50
    60
    70
    80
    90
    100

    Then you could do this:

    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?

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Im so confused! Counting and Summing

    This is just an example..
    Try to implement your method
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  6. #6
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default 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++;
    }

  7. The Following User Says Thank You to Zula For This Useful Post:

    JavaPF (October 29th, 2010)

  8. #7
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: Im so confused! Counting and Summing

    Quote Originally Posted by JavaPF View Post
    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!

Similar Threads

  1. Confused about setting up JSP
    By mjpam in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: September 17th, 2010, 05:14 AM
  2. Help with Arrays - Counting elements
    By ShakeyJakey in forum Collections and Generics
    Replies: 7
    Last Post: August 8th, 2010, 04:09 PM
  3. Question: counting
    By miss confused in forum Java Theory & Questions
    Replies: 2
    Last Post: July 30th, 2010, 05:38 PM
  4. Ayudame! :confused:
    By wasaki in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 22nd, 2010, 10:37 AM
  5. Confusion with C/C++
    By Eric in forum Java Applets
    Replies: 0
    Last Post: December 22nd, 2008, 02:18 PM