9 Replies - 670 Views - Last Post: 05 December 2012 - 11:49 AM Rate Topic: -----

#1 sigfreed77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 28-October 12

Adding numbers from txt file and format the output

Posted 05 December 2012 - 08:14 AM

Well, I stumbled upon a little issue on my program. It might sound like too simple to solve, but I tried various possible ways to solve the problem that I'm better off asking someone more experienced on java to give me a hand. This is what I want to achieve:
A nums.txt file is already created, and it looks like this:
45 102 32 139
23 3 4 293 138 312
32 32 32



My result should be like this:
318
763
86



Now, On my code, I'm able to add and sum all the numbers from each row and column, BUT, the output is a single column until the program finishes summing all numbers. This is my current code.

import java.util.*;
import java.io.*;
import java.lang.*;
public class numreader  {
        public static void main (String [] arg){
     try{ 
         Scanner inf = new Scanner(new File("nums.txt"));
         int l = 0; 
         int sum = 0;
             
         while(inf.hasNext()){
          sum = inf.nextInt();
          l += sum;
         System.out.println(l);
         } 

         inf.close();
    
    
     } 
    catch (IOException i){
    }
  }
}



And my current output is like this, which does not follow the format I want to achieve of summing per row and outputting a column with the same number of rows like shown on the example at the start of this topic:

45
147
179
318
341
344
348
641
779
1091
1123
1155
1187


If someone could give me a hand on this one, it'll be great. Note: as you can see the program is done, the output format is the problem I'm facing.

Is This A Good Question/Topic? 0
  • +

Replies To: Adding numbers from txt file and format the output

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 410
  • View blog
  • Posts: 1,449
  • Joined: 14-September 12

Re: Adding numbers from txt file and format the output

Posted 05 December 2012 - 08:18 AM

Well, I would like to correct you here.

Quote

My result should be like this:
318
763
86



Instead it should be

My result should be like this:
1	318
2	773
3	96


regards,
Raghav

This post has been edited by raghav.naganathan: 05 December 2012 - 08:20 AM

Was This Post Helpful? 0
  • +
  • -

#3 sigfreed77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 28-October 12

Re: Adding numbers from txt file and format the output

Posted 05 December 2012 - 08:25 AM

Yes, thanks for that.
Was This Post Helpful? 0
  • +
  • -

#4 sigfreed77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 28-October 12

Re: Adding numbers from txt file and format the output

Posted 05 December 2012 - 09:27 AM

If anyone knows how to solve my issue, let me know. Again, the program is solved, but is not outputting in the right format.
Was This Post Helpful? 0
  • +
  • -

#5 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 410
  • View blog
  • Posts: 1,449
  • Joined: 14-September 12

Re: Adding numbers from txt file and format the output

Posted 05 December 2012 - 09:33 AM

View Postsigfreed77, on 05 December 2012 - 09:57 PM, said:

If anyone knows how to solve my issue, let me know. Again, the program is solved, but is not outputting in the right format.


Well, technically your problem is not solved. It is not the format you need to take into account but the logic basically.
You seem to be adding the digits in succession. But what you need to do is to add the digits based on the lines.

So, you need to write a logic by which the program only adds the digits of individual lines.

regards,
Raghav

This post has been edited by raghav.naganathan: 05 December 2012 - 09:34 AM

Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3488
  • View blog
  • Posts: 15,769
  • Joined: 20-September 08

Re: Adding numbers from txt file and format the output

Posted 05 December 2012 - 09:42 AM

You could use a StreamTokenizer (obviously don't hard-code the file name like i did):

    public static void print() {
        Reader in = null;
        try {
            in = new FileReader("nums.txt");

            StreamTokenizer st = new StreamTokenizer(in);
            st.eolIsSignificant(true);

            int sum = 0;
            int tt = 0;

            while ((tt = st.nextToken()) != StreamTokenizer.TT_EOF) {
                switch (tt) {
                case StreamTokenizer.TT_NUMBER:
                    sum += st.nval;
                    break;
                case StreamTokenizer.TT_EOL:
                    System.out.println(sum);
                    sum = 0;
                }
            }
        } catch (IOException i) {
            i.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (Exception e) { /* ignore */
            }
        }
    }


Was This Post Helpful? 0
  • +
  • -

#7 ipushmycar  Icon User is offline

  • D.I.C Regular

Reputation: 86
  • View blog
  • Posts: 390
  • Joined: 29-August 10

Re: Adding numbers from txt file and format the output

Posted 05 December 2012 - 09:46 AM

If you know the format of your text file why not read in a whole line, split the line on space and then sum up everything that has been split.
Was This Post Helpful? 0
  • +
  • -

#8 sigfreed77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 28-October 12

Re: Adding numbers from txt file and format the output

Posted 05 December 2012 - 10:19 AM

Thanks gOOse. I re-designed my code in a few areas and the output seems to work perfectly. Now I can't make the next two lines to print, just the first line(sum is correct by the way). Here is my code.

import java.util.*;
import java.io.*;
import java.lang.*;
public class numreader  {
        public static void main (String [] arg){
      try{
         Scanner inf = new Scanner(new File("nums.txt"));
         String g = inf.nextLine();
         StringTokenizer s = new StringTokenizer(g);
         int sum =0;

         while(s.hasMoreTokens()){
          int y = Integer.parseInt(s.nextToken());
          sum+=y;
          System.out.println(sum);

         }


        inf.close();

     }
    catch (IOException i){
    }
    catch(NoSuchElementException y){
    }
    catch(NumberFormatException y){
    }
  }
}



Here is the output.
318



I need to get the other two lines(or as many lines) to print out. Any hints would be appreciated.
Was This Post Helpful? 0
  • +
  • -

#9 sigfreed77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 28-October 12

Re: Adding numbers from txt file and format the output

Posted 05 December 2012 - 10:34 AM

Hey there ipushmycar(cool name). The thing is that my .txt file isn't fixed. Meaning, that I have to sum as many lines and columns as I need to, lines and columns filled with numbers and white space of course. I would've finished this program already if that would've been the case, but unfortunately it isn't. Thanks
Was This Post Helpful? 0
  • +
  • -

#10 sigfreed77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 28-October 12

Re: Adding numbers from txt file and format the output

Posted 05 December 2012 - 11:49 AM

Thanks guys. I figured it out.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1