# something is wrong with if-statement?

• November 17th, 2013, 09:08 AM
Dixxon
something is wrong with if-statement?
Code :

```double[]   t[week][Measurement] = in.nextDouble (); // input int NumberOfWeeks = in.nextInt (); // input int MeasurementWeekly = in.nextInt (); // input   sum = new double[NumberOfWeeks + 1]; double[] highestValue = new double[NumberOfWeeksr + 1]; double[] lowestValue = new double[NumberOfWeeks + 1];   for (int week = 1; week <= NumberOfWeeks; week++) { for (int Measurement = 1; Measurement <= MeasurementWeekly; Measurement++) { sum[week] = sum[week]+ t[week][Measurement];   if(t[week][Measurement] > highestValue[week] || (Measurement == 1 && week == 1) ) { highestValue[week]= t[week][Measurement]; }     System.out.println("higest value "+ week+": " +highestValue[week]);     }```
is there something wrong with my if-statement?
when enteringen values for each week as:
week 1 : -5 and -9 it definens -5 as highest value which is right but as soon we go forward to week 2 and enter values such as
week 2: -4 and -10 it definens 0 as highest value which is wrong ...

any ideas why the if-statement goes crazy as soon the for-loop runs for the second time ?
• November 17th, 2013, 09:12 AM
Norm
Re: something is wrong with if-statement?
Quote:

-4 and -10 it definens 0 as highest value which is wrong
0 is higher than either of those numbers. Are there any 0s in the array?

The code is very hard to read and understand because there are more than one statement on a line. Can you fix the code to limit the number of statements on a line to one? Also there should NOT be a statement on a line following a {
and a } should be on a line by itself.
• November 17th, 2013, 09:27 AM
Dixxon
Re: something is wrong with if-statement?
ohh i hope that the code is cleaner now :)

Quote:

0 is higher than either of those numbers. Are there any 0s in the array?
it should not contain any zeros as the array contains only users input which in this cause are only negative nummbers.
i noticed that highestValue[week]is defined as zero before defining it as highestValue[week]= t[week][Measurement] in the if-statment
• November 17th, 2013, 09:36 AM
Norm
Re: something is wrong with if-statement?
Quote:

it should not contain any zeros
Print out its contents to be sure:
Code :

`System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));`
• November 17th, 2013, 09:46 AM
Dixxon
Re: something is wrong with if-statement?
it gives a error
Quote:

"Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method toString(long[]) in the type Arrays is not applicable for the arguments (double)"
ps:sorry btw i am very new to java :)
• November 17th, 2013, 09:49 AM
Norm
Re: something is wrong with if-statement?
Post the statement that has the error.

If the array has two dimensions, use the Array class's deepToString() method.
• November 17th, 2013, 09:50 AM
Dixxon
Re: something is wrong with if-statement?
ah never mind , my bad ... it gave "an ID [0.0, 5.0, 6.0]"
• November 17th, 2013, 09:51 AM
Norm
Re: something is wrong with if-statement?
That shows the first element of the array as 0.0. The array has no negative numbers.
• November 17th, 2013, 09:57 AM
Dixxon
Re: something is wrong with if-statement?
ohh yeah i enter "5"and "6" and yet the array had a zero value
also here is the behavior when entering negtive vaules
input:
week 1 :-5.0 -8.0
week 2 : -6.0 -3.0

output:
an ID [0.0, -5.0, 0.0]
highest value week 1: -5.0

an ID [0.0, -5.0, 0.0]
highest value week 2: 0.0

but any idea why it does only work for the first week and not for other weeks? . Is there any other way to register the highest value ?
• November 17th, 2013, 10:04 AM
Norm
Re: something is wrong with if-statement?
Can you make a small, complete program that compiles, executes and shows the problem for testing?
Be sure to add what input the user needs to make to execute the program.
• November 17th, 2013, 10:14 AM
Dixxon
Re: something is wrong with if-statement?
ahh its kinda written in swedish (i translated the the small code above before posting ) so will it be any help to get the program? .... btw exercise is about using arrays so maybe there is a better method to register tne highest value with arrays or something ?
• November 17th, 2013, 10:28 AM
Norm
Re: something is wrong with if-statement?
The normal way to search an array is with a loop with an if statement inside. Without seeing code that compiles and executes, I can't recommend much.
• November 17th, 2013, 10:43 AM
Dixxon
Re: something is wrong with if-statement?
so i did my best
Code :

```import java.util.*; // Scanner, Locale   class tempraturmätningar { public static void main (String[] args) { System.out.println ("values\n");     Scanner in = new Scanner (System.in); in.useLocale (Locale.US);     System.out.print ("number of weeks: "); int NumberOfWeeks = in.nextInt (); System.out.print ("how many Measurement every week: "); int MeasurementWeekly = in.nextInt ();   // plats att lagra temperaturer double[][] t = new double[NumberOfWeeks + 1] [MeasurementWeekly + 1];   // input for (int week = 1; week <= NumberOfWeeks; week++) { System.out.println ("value week " + week + ":"); for (int Measurement = 1; Measurement <= MeasurementWeekly; Measurement++) t[week][Measurement] = in.nextDouble (); } System.out.println ();   // show values double TotalTemp=0; double highestTemp=0; double lowestTemp=0;   System.out.println ("values:");   for (int week = 1; week <= NumberOfWeeks; week++) { for (int Measurement = 1; Measurement <= MeasurementWeekly; Measurement++) { double temp = t[week][Measurement]; System.out.print (temp + " ");   } System.out.println (); }   System.out.println ();     double [] sum = new double[NumberOfWeeks + 1]; double[] highestValue = new double[NumberOfWeeks + 1]; double[] lowestValue = new double[NumberOfWeeks + 1];   for (int week = 1; week <= NumberOfWeeks; week++) { for (int Measurement = 1; Measurement <= MeasurementWeekly; Measurement++) { sum[week] = sum[week]+ t[week][Measurement];   if(t[week][Measurement] > highestValue[week] || (Measurement == 1 && week == 1) ) { highestValue[week]= t[week][Measurement]; }     System.out.println("higest value "+ week+": " +highestValue[week]);     }     } }}```
• November 17th, 2013, 10:56 AM
Norm
Re: something is wrong with if-statement?
Where is the input to the program?
Replace
Scanner in = new Scanner (System.in);
with
Scanner in = new Scanner ("1/n2/n3/n4/n); //put valid user input in Scanner's constructor.

The code is poorly formatted. The indentations are inconsistent and the }s are not where they should be.
• November 17th, 2013, 11:03 AM
Dixxon
Re: something is wrong with if-statement?
Quote:

Originally Posted by Norm
Where is the input to the program?

what do you mean ? i just put the code in eclipse (editing program) and run the program. i just tested and it worked.
and yh i know its poorly formatted but it does not matter right now as long as it works :P
• November 17th, 2013, 11:12 AM
Norm
Re: something is wrong with if-statement?
Quote:

i just tested and it worked.
• November 17th, 2013, 11:14 AM
Dixxon
Re: something is wrong with if-statement?
hehe no i meant that it works as it should except registering the right highest value
• November 17th, 2013, 11:46 AM
Norm
Re: something is wrong with if-statement?
Quote:

it does not matter right now as long as it work
What does that mean? If I'm going to test the code, it matters to me.
• November 17th, 2013, 11:50 AM
Dixxon
Re: something is wrong with if-statement?
i meant that it doesnt really matter for me if the code is poorly written as long it works and are able to run the porgram ... so any idea how to get the highest vaule ?
• November 17th, 2013, 12:06 PM
Norm
Re: something is wrong with if-statement?
Sorry, I don't like working with messed up code.
Fix the formatting and put the user's input into the Scanner class's constructor as shown in post#14.

I bet you could have fixed the formatting in the time it has taken so far to say it doesn't matter.

Of course you can do what you want.
• November 17th, 2013, 12:09 PM
Dixxon
Re: something is wrong with if-statement?
i am very new to java and this was the best i could come up with... so i will just wait for some who can fix the problem
• November 17th, 2013, 12:14 PM
Norm
Re: something is wrong with if-statement?
Good luck.
• November 17th, 2013, 12:44 PM
GregBrannon
Re: something is wrong with if-statement?
Since you're using Eclipse, all you have to do to format your source code is select-all (CTRL-a), then CTRL-i. Learn to use your tools.

Typically, to find the largest value, the largestValue variable is set to a very small number or the first element of the array. Then, each value in the array is compared to the largestValue:
Code java:

```for ( each element of the arrayItems ) { if ( arrayItem[index] > largestValue ) { largestValue = arrayItem[index]; } }```
What you've done with the arrays sum, highestValue, and lowestValue is either confusing (confused?) or just plain wrong. Those should be single values, not arrays of values.