6 Replies - 1521 Views - Last Post: 13 January 2011 - 10:11 PM Rate Topic: -----

#1 Guest_dudelove060*


Reputation:

Adding numbers from text file - custom method - HELP!

Posted 13 January 2011 - 09:39 PM

Hello All,

I am using the following method:

//addTwo method
public DollarsAndCents addTwo (DollarsAndCents other)
{
return new DollarsAndCents (this.toCents () + other.toCents ());
}


How would I go about adding numbers from a text file, line by line using this method?

Please use: :code:

This post has been edited by pbl: 13 January 2011 - 09:42 PM
Reason for edit:: code tags added


Is This A Good Question/Topic? 0

Replies To: Adding numbers from text file - custom method - HELP!

#2 NeoTifa  Icon User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4114
  • View blog
  • Posts: 18,216
  • Joined: 24-September 08

Re: Adding numbers from text file - custom method - HELP!

Posted 13 January 2011 - 09:43 PM

Use a while loop to get input from file while fileObjectStreamThing.hasNext() == true and store them in an array or whatnot. Then do what you want with it. It would help if we had a little more context. Also, :code: and http://download.orac...l/essential/io/
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Adding numbers from text file - custom method - HELP!

Posted 13 January 2011 - 09:44 PM

Without seing the class DollarsAndCents there is not much we can do
or this is completly irrelevant and you want to know how to read int or float or int.int from a file ?
Was This Post Helpful? 0
  • +
  • -

#4 Guest_dudelove060*


Reputation:

Re: Adding numbers from text file - custom method - HELP!

Posted 13 January 2011 - 09:50 PM

here is some partial code:

 

public class DollarsAndCents
      {

          private int dollars, cents;

          public DollarsAndCents ()
                {
                    dollars = 0;
                    cents = 0;
                }

          public DollarsAndCents (int pennies)
                {
                   dollars = pennies / 100;
                   cents = pennies % 100;
                }

          public DollarsAndCents (int d, int c)
                {
                   dollars = d;
                   cents = c;
                }

          public DollarsAndCents (double d)
                {
                   int pennies = (int) (d * 100);
                   dollars = pennies / 100;
                   cents = pennies % 100;
                }

          public DollarsAndCents (String s)
                {
                    int positionOfDecimalPoint = s.indexOf('.');
                    dollars = Integer.parseInt (s.substring (0, positionOfDecimalPoint));
                    cents = Integer.parseInt (s.substring (positionOfDecimalPoint + 1));
                }

          public int getDollars ()
                {
                    return this.dollars;
                }

          public static int getDollars (DollarsAndCents B)/>
                {
                    return b.dollars;
                }

          public void setDollars (int d)
                {
                     dollars = d;
                }

          public static void setDollars (DollarsAndCents a, int d)
                {
                     a.dollars = d;
                }

		   public static void main(String args[])
			  {
				  try{
				// Open the file that is the first 
				// command line parameter
				FileInputStream fstream = new FileInputStream("file.txt");
				// Get the object of DataInputStream
				DataInputStream in = new DataInputStream(fstream);
					BufferedReader br = new BufferedReader(new InputStreamReader(in));
				//String strLine;
				String strLine;
				//Read File Line By Line
				while ((strLine = br.readLine()) != null)   {
				  // Print the content on the console
				  System.out.println (strLine);
				}
				//Close the input stream
				in.close();
				}catch (Exception e){//Catch exception if any
				  System.err.println("Error: " + e.getMessage());
				}
			  }



I just wanted to know how I can incorporate that addTwo method using what I have in my main method.
Was This Post Helpful? 0

#5 NeoTifa  Icon User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4114
  • View blog
  • Posts: 18,216
  • Joined: 24-September 08

Re: Adding numbers from text file - custom method - HELP!

Posted 13 January 2011 - 09:53 PM

If file name is a command line parameter, then why are you forcing it a filename? It should be args[0].
Was This Post Helpful? 0
  • +
  • -

#6 Guest_dudelove060*


Reputation:

Re: Adding numbers from text file - custom method - HELP!

Posted 13 January 2011 - 10:06 PM

ah yes, please ignore comments in my code. the numbers have been hard-coded into the text file. the addTwo method sums 2 numbers, and I just wanted to know how I could get the total from the text file, which is line by line.

Text file looks something like this:

12.45
16.78
45.78
1256.45
Was This Post Helpful? 0

#7 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12209
  • View blog
  • Posts: 45,287
  • Joined: 27-December 08

Re: Adding numbers from text file - custom method - HELP!

Posted 13 January 2011 - 10:11 PM

You can obviously read in the Strings. Parse the read in String to a double, then add it to a sum variable.

If you want to use the add() method in your OP, do the following:
-Outside the loop, create a DollarsAndCents variable
-In the loop, if it is null, create a new DollarsAndCents object from the String
-Else, assign the variable the result from invoking add() on itself with the param as a new DollarsAndCents object from the String read in.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1