Re: more on converting strings to numbers....

From: Bryce (spamtrap_at_berzerker-soft.com)
Date: 08/11/04


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


Relevant Pages

  • Re: more on converting strings to numbers....
    ... >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.. ... Note that argsis the full array, ... the conversion result to the sum just after the conversion ...
    (comp.lang.java.help)
  • Re: more on converting strings to numbers....
    ... I have to do a little app that adds up all nos. ... >>assigned me to do) is print out the sum of all the nubmers user inputs.. ... > a single element within the array. ... > the conversion result to the sum just after the conversion ...
    (comp.lang.java.help)
  • Re: Subset Sum problem (w/ limited scope)
    ... The "Subset Sum" problem (stated various ways depending on what you ... a subset of those integers that sums up to zero. ... Are there going to be duplicate values in your original array? ...
    (comp.lang.fortran)
  • Re: How can I find the greatest possible sum within 12 months? A newbie...
    ... It may be because you have headers, and when I tied it first I omitted ... You may have omitted to enter it as an array formula (see notes on ... the starting sum is in A2 and the end sum is in A123. ... Excel will insert them for you. ...
    (microsoft.public.excel.worksheet.functions)
  • Re: Explanation of SUMPRODUCT
    ... I don't even think you need to enter it as an array ... Yes, we know this, but in this case SUM is an array formula SUMPRODUCT ... > each element is the number of times the corresponding cell value appears ...
    (microsoft.public.excel.programming)