Results 1 to 5 of 5

Thread: Sum from a file

  1. #1
    mikerousse is offline Member
    Join Date
    Nov 2013
    Posts
    8
    Rep Power
    0

    Default Sum from a file

    Hi guys,

    i have create the following program:
    Java Code:
    import java.io.File;
    import java.util.Scanner;
    
    
    public class ReversePartA {
    
    	private Scanner x;
    	
    		public void openFile(){
    			try{
    				x=new Scanner(new File("myfile.txt"));
    			}
    			catch (Exception e){
    					System.out.println("Could not find file");
    								}
    		}
    public void readFile(){
    			
    int loop=0;
    			do{
    			
    			x.hasNext();
    			String a= x.next();
    			int b=x.nextInt();
    			
    									
    			System.out.println(b);
    			loop++;			
    			}while (loop<4);}
    		
    		
    		
    public void closeFile(){
    	x.close();
    }
    }
    the file "myfile" includes the following:

    434
    44
    3333
    111

    my program provide only the numbers. The "String a= x.next();" includes and other material from the file string mostly. Now I am trying to make sum of this number, there is anyone can help me out?

    Thanks
    Last edited by mikerousse; 01-04-2014 at 07:56 PM.

  2. #2
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    162
    Rep Power
    2

    Default Re: Sum from a file

    What you need is an integer named sum, and every time it loops thorugh, do sum += b. This will add to the sum every time and give you your sum.

  3. #3
    mikerousse is offline Member
    Join Date
    Nov 2013
    Posts
    8
    Rep Power
    0

    Default Re: Sum from a file

    i do something like this:

    Java Code:
    import java.io.File;
    import java.util.Scanner;
    
    
    public class ReversePartA {
    
    	private Scanner x;
    	
    		public void openFile(){
    			try{
    				x=new Scanner(new File("myfile.txt"));
    			}
    			catch (Exception e){
    					System.out.println("Could not find file");
    								}
    		}
    public void readFile(){
    int j=0;
    int sum=0;
    int loop=0;
    			for(j=0; j<=1; j++){ 
    			
    			x.hasNext();
    			String a= x.next();
    			int b=x.nextInt();
    			sum +=b;
    									
    					
    			}System.out.println(sum);}
    		
    		
    		
    public void closeFile(){
    	x.close();
    }
    }
    unfortunately give me some strange sum, any ideas?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,375
    Rep Power
    5

    Default Re: Sum from a file

    First, your for loop only executes twice. And you are not using the hasNext() method of the Scanner class correctly. x.hasNext() by itself doesn't accomplish anything. You should forget the for loop and use the hasNext() in a while loop. Check out the Scanner class for more details.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Sum from a file

    Quote Originally Posted by jim829 View Post
    First, your for loop only executes twice. And you are not using the hasNext() method of the Scanner class correctly. x.hasNext() by itself doesn't accomplish anything. You should forget the for loop and use the hasNext() in a while loop. Check out the Scanner class for more details.

    Regards,
    Jim
    Correct, for this you should only need a scanner and one integer. Each time you go through the while loop, add to the integer. This will be your total.

Similar Threads

  1. Replies: 0
    Last Post: 12-31-2013, 05:46 AM
  2. Replies: 0
    Last Post: 12-15-2012, 01:37 AM
  3. Replies: 1
    Last Post: 08-04-2010, 03:24 PM
  4. Replies: 8
    Last Post: 02-18-2010, 12:54 PM
  5. Replies: 0
    Last Post: 02-11-2009, 09:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •