Re: more on converting strings to numbers....
From: Bryce (spamtrap_at_berzerker-soft.com)
Date: 08/11/04
- Next message: Eric Sosman: "Re: getting reference to main class"
- Previous message: Madhur Ahuja: "getting reference to main class"
- In reply to: Frances Del Rio: "Re: more on converting strings to numbers...."
- Next in thread: Frances Del Rio: "Re: more on converting strings to numbers...."
- Reply: Frances Del Rio: "Re: more on converting strings to numbers...."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 11 Aug 2004 17:31:50 -0400
On Wed, 11 Aug 2004 14:57:16 -0400, Frances Del Rio <fdr58@yahoo.com>
wrote:
>oh my gosh.... you make it sound so simple.. ("just convert element by
>element...") yes, I do realize the diff. betw. args[i] & args[]..
>
>and I HAVE used loops.. as in this simple little app, similar to
>helloworld..
>
> for (int i=0; i < args.length; i = i+1) {
> System.out.print(" " + args[i]);
> }
This is more commonly written:
for (int i = 0; i < args.length; i++) {
}
Now, for the entirety try something like this:
int sum = 0;
for (int i = 0; i < args.length; i++ {
// This gives you the string out of the array
String tempString = args[i];
// This converts the String to an int
int currentIntValue = Integer.valueOf(tempString).intValue();
// This adds the current value to the sum
sum = sum + currentIntValue;
}
System.out.println("Sum: " + sum);
HTH
>I have to do a little app that adds up all nos. user types in.. like if
>they type in 1 2 3 it returns 6.... ok.... thank you very much for your
>response.. Frances
>
>
>
>Juha Laiho wrote:
>
>> Frances Del Rio <fdr58@yahoo.com> said:
>>
>>>but what I need to do (what the programmer who's teaching me has
>>>assigned me to do) is print out the sum of all the nubmers user inputs..
>>>so I figured what I need to do is convert args[i] to an array of
>>>numbers.. i.e., how to you convert not just a string but AN ARRAY OF
>>>STRINGS (or an array of arguments..) to an ARRAY OF NUMBERS??
>>
>>
>> Well, you convert one element at a time, and go through all the elements
>> in a loop. Note that args[] is the full array, whereas args[i] is
>> a single element within the array.
>>
>> But then, why would you need to convert the full array first?
>> Just convert element by element to a temporary variable; adding
>> the conversion result to the sum just after the conversion
>> (so, before the conversion of the next element).
-- now with more cowbell
- Next message: Eric Sosman: "Re: getting reference to main class"
- Previous message: Madhur Ahuja: "getting reference to main class"
- In reply to: Frances Del Rio: "Re: more on converting strings to numbers...."
- Next in thread: Frances Del Rio: "Re: more on converting strings to numbers...."
- Reply: Frances Del Rio: "Re: more on converting strings to numbers...."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|