Hola.
so, im making a simple program right now to work on my java skillz, and just for fun in general (im pretty new to java).
so, i was wondering, whats the fastest way to find the maximum number in an array of STRINGS. because i tried parsing the ints in a for loop like so...

for(int f = 0; f == 20; f++){
  intarray[f] = Integer.parseInt(stringarray[f]);
}

but when i tried to print all of the ints in the intarray, they all came out as zeros.
if you have another way of doing this, please tell me!
Thanks,
Quinnifir

you don't use the for loop like that. the condition must be f<20. it works like a while. The while is the opposite of an if. That's why you're getting zeros, your int array has never been initialized.

string array = Random number();

for(string number : array)
{

if( number < some high number)
{
display number
}
}

Edited 5 Years Ago by Donnovan: n/a

Comments
this piece of code, containing at least 5 errors against Java and it's syntax, makes me believe you should ask questions, instead of trying to answer them.
Your code has no sense.
for(string number : array)

Do you have a class named string?
Or do you mean the java class: String
I guess you intended to use pseudo code, but without explanations on what it is doing,
it looks confusing to me.

Do you not use indentations when writing code?
Unintended code is hard to read. Especially with nesting with multiple }s

string array = Random number();

for(string number : array)

what the hell is this supposed to mean? not taking NormR1's answer into account:

do you know what

for ( [B]S[/B]tring number: array)

does? can you figure out where you messed up there?
if the OP just followed teo236's first answer, he could have gotten a lot further with his task. your answer would just set him back.

waauw you guys are killing me here, maybe i didn't understand the question very wel,
i tought quinnifir was asking for a way to read all the elements in his array.

so maybe my code was a little messy so here is it the java way:

String [] array = {"a","b","c"};
        for(String number: array)
        {
            if(number.compareTo("b") == 0)
            {
                System.out.println(number);
            }
        }

an example of how a for loop work. to be specific this is a foreach loop, was my answer...

Edited 5 Years Ago by Donnovan: n/a

Well what im trying to do is find the largest number in an array of Strings
I create the arrays like so:

String[] stringarray = new String[20]; //make a string array with twenty indexes
int[] intarray = new int[20]; //make a int array with twenty indexes

in my code, i have it do stuff that adds numbers to the string array
and no, its not random, because i would use Random.nextint, not strings people.

so now, i want to parse the strings to ints so i can work with them as numbers

for(int f = 0; f == 19; f++){
	intarray[f] = Integer.parseInt(stringarray[f]);
}

now it loops 20 times, and should take the string in stringarray, parse it, and send it into the same slot in intarray.


teo236:
"you don't use the for loop like that. the condition must be f<20. it works like a while. The while is the opposite of an if. That's why you're getting zeros, your int array has never been initialized."

what does that really mean? should i change the "f == 19" to "f < 19"

Thanks guys, sorry i dident really explain it very well the first time around.
Quinnifir

Edited 5 Years Ago by quinnifir: n/a

if you want to loop it 20 times you have to change "f == 19" to "f < 20" or "f <= 19".

If you put "for(int f=0;f == 19;f++)", it means: create the variable 'f' with a value of 0. while f equals 19(????) run this code and then increment 'f' by one. That's why you have to use < (it means "i is lower than x") or <= (that means "i is lower or equal than x").

thanks teo!
so thats why none of my ints were parsing, because when i do:

int[] i = new int[20];

it sets all of the ints to zero as a default.
thank you guys!

PS:
if i sound like a noob, its because i am one.

This question has already been answered. Start a new discussion instead.