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 2 of 2

Thread: Sum from a file

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Sum from a file

    Hi guys,

    below I make one program to read in one column the strings and the second column the int:
    import java.io.File;
    import java.util.Scanner;
     
     
    public class Main {
     
    	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.printf("%s %s\n",a,b);
    			loop++;			
    			}while (loop<10);}
     
    public void closeFile(){
    	x.close();
    }
    }

    The file includes 10 lines and 2 columns and is like this:

    abc 12
    def 234
    rrt 333

    ..
    ..
    ..

    Now what I need is to sum all int of the 2 column. So I try to make some small changes to :
    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<=10; j++){
     
                x.hasNext();
                String a= x.next();
                int b=x.nextInt();
                sum +=b;
     
     
                }System.out.println(sum);}
     
     
     
    public void closeFile(){
        x.close();
    }
    }

    the results of sum from this program is crazy. Any ideas?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Sum from a file

    Read the whole line as a String, and then parse the String into two parts using the split() method. Please don't store the results in parallel arrays. Create a class instead.

Similar Threads

  1. java rounded sum
    By harvind in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 20th, 2013, 03:07 AM
  2. sum of digits
    By snarayana.murthy86 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 18th, 2013, 02:40 PM
  3. code not returning sum
    By hmcka in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 6th, 2012, 09:17 AM
  4. Sum of intervals
    By cisneros778 in forum Java Theory & Questions
    Replies: 3
    Last Post: February 21st, 2012, 04:08 PM
  5. calculate sum and product
    By swampfox in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 27th, 2011, 03:55 PM