9 Replies - 1830 Views - Last Post: 06 March 2010 - 09:53 PM Rate Topic: -----

#1 FUXAS  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-March 10

Reading from file + sum number from that file

Posted 06 March 2010 - 10:39 AM

I've written This code to read from file :

package Javaapplication 9;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main{
  public static void main(String[] args) throws FileNotFoundException {
    Scanner scanner =
      new Scanner(new File("C:/Users/F/Desktop/textfile.txt")).useDelimiter("\\Z"); /* z - failo pabaiga*/
    String skaiciai = scanner.next();
    System.out.print ("skaiciai yra: "); /* isvedam i ekrana uzrasa "skaiciai yra" */
    System.out.println (skaiciai); /* isvedam skaicius, kurie yra faile*/
    scanner.close();
  }
}



Then, I need to sum numbers from that textfile.txt.. And I have no idea how to make a massive to sum them..
For example : in file numbers are 12156, sum of them is : 15..

This post has been edited by FUXAS: 06 March 2010 - 10:54 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Reading from file + sum number from that file

#2 javabie  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 117
  • Joined: 12-February 10

Re: Reading from file + sum number from that file

Posted 06 March 2010 - 12:01 PM

you could use BigInteger class...

have a look at this...
http://java.sun.com/...BigInteger.html
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10561
  • View blog
  • Posts: 39,085
  • Joined: 27-December 08

Re: Reading from file + sum number from that file

Posted 06 March 2010 - 04:10 PM

Assuming the numbers are separated by a delimiter, say whitespace for example, you can read them all in using the Scanner's nextInt() method and sum as you go. Or if they are all as one String, you will have to go through character by character in the String and sum. Note that '1'-'9' as chars are equivelant to 48-57 as ints.
Was This Post Helpful? 0
  • +
  • -

#4 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Reading from file + sum number from that file

Posted 06 March 2010 - 04:49 PM

View Postmacosxnerd101, on 06 March 2010 - 02:10 PM, said:

Assuming the numbers are separated by a delimiter, say whitespace for example, you can read them all in using the Scanner's nextInt() method and sum as you go. Or if they are all as one String, you will have to go through character by character in the String and sum. Note that '1'-'9' as chars are equivelant to 48-57 as ints.

Could he go through character by character and then use the parseInt() method to convert each String number to an int?
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10561
  • View blog
  • Posts: 39,085
  • Joined: 27-December 08

Re: Reading from file + sum number from that file

Posted 06 March 2010 - 05:26 PM

That would work, though knowing the ASCII values for the numerical characters will allow for more memory efficiency, since you would be creating a new String object for each parse.
Was This Post Helpful? 0
  • +
  • -

#6 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Reading from file + sum number from that file

Posted 06 March 2010 - 05:30 PM

View Postmacosxnerd101, on 06 March 2010 - 03:26 PM, said:

That would work, though knowing the ASCII values for the numerical characters will allow for more memory efficiency, since you would be creating a new String object for each parse.

This is true. My way seems like the quicker, easier, less efficient way to go...
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10561
  • View blog
  • Posts: 39,085
  • Joined: 27-December 08

Re: Reading from file + sum number from that file

Posted 06 March 2010 - 09:31 PM

I've got to disagree here on the quicker and easier part. It will still take no fewer lines of code (and probably more depending on implementation) than my solution, and it won't run any faster. It just eats up a little more memory.
Was This Post Helpful? 0
  • +
  • -

#8 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Reading from file + sum number from that file

Posted 06 March 2010 - 09:33 PM

View Postmacosxnerd101, on 06 March 2010 - 07:31 PM, said:

I've got to disagree here on the quicker and easier part. It will still take no fewer lines of code (and probably more depending on implementation) than my solution, and it won't run any faster. It just eats up a little more memory.

All depends on how your mind works I guess...thought process is (nearly) everything in programming.

I just thought mine would be easier because it doesn't involve having to know ASCII values. I'm not saying this is hard to get the ASCII values or anything...

This post has been edited by zim1985: 06 March 2010 - 09:36 PM

Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10561
  • View blog
  • Posts: 39,085
  • Joined: 27-December 08

Re: Reading from file + sum number from that file

Posted 06 March 2010 - 09:41 PM

Algorithmically speaking, you are correct. However, a big part of efficiency analysis is understanding the drawbacks when it comes to implementation.

As for looking up things when programming, get used to it. :) Not a day goes by when I do not look at the API, ASCII table or some other reference when helping other DICs. A big part of programming is understanding your tools, which comes from looking things up a lot of the time.
Was This Post Helpful? 0
  • +
  • -

#10 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Reading from file + sum number from that file

Posted 06 March 2010 - 09:53 PM

View Postmacosxnerd101, on 06 March 2010 - 07:41 PM, said:

Algorithmically speaking, you are correct. However, a big part of efficiency analysis is understanding the drawbacks when it comes to implementation.

As for looking up things when programming, get used to it. :) Not a day goes by when I do not look at the API, ASCII table or some other reference when helping other DICs. A big part of programming is understanding your tools, which comes from looking things up a lot of the time.

I know what you mean about looking stuff up. Today I was helping with some Swing stuff, or maybe it was yesterday, and I had to scroll through the API, find the method and justify. I know what you mean.

You have no idea how much I learned just in the past week or so I have been here. So many new ways of look at problems have come to me now that I have seen so many different ways of solving problems. To more I can absorb, question, and attempt the better I seem to get at seeing what is wrong and figuring stuff out. I would be lying if I said reading your posts, dogstopper's, and eric.price's posts (among a few others whose names have escaped me) haven't helped.

Now I'm not quite sure how I got off-topic, but yeah. I'm always open to other viewpoints and looking stuff up to help and learn...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1