Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: something is wrong with if-statement?

1. ## something is wrong with if-statement?

```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 ?

2. ## Re: something is wrong with if-statement?

-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.

3. ## Re: something is wrong with if-statement?

ohh i hope that the code is cleaner now

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

4. ## Re: something is wrong with if-statement?

it should not contain any zeros
Print out its contents to be sure:
`System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));`

5. ## Re: something is wrong with if-statement?

it gives a error
"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

6. ## 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.

7. ## Re: something is wrong with if-statement?

ah never mind , my bad ... it gave "an ID [0.0, 5.0, 6.0]"

8. ## Re: something is wrong with if-statement?

That shows the first element of the array as 0.0. The array has no negative numbers.

9. ## 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 ?

10. ## 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.

11. ## 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 ?

12. ## 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.

13. ## Re: something is wrong with if-statement?

so i did my best
```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]);

}

}
}}```

14. ## 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.

15. ## Re: something is wrong with if-statement?

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

16. ## Re: something is wrong with if-statement?

i just tested and it worked.

17. ## Re: something is wrong with if-statement?

hehe no i meant that it works as it should except registering the right highest value

18. ## Re: something is wrong with if-statement?

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.

19. ## 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 ?

20. ## 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.

21. ## 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

Good luck.

23. ## 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:
```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.