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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 23 of 23

Thread: something is wrong with if-statement?

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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));
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2013
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: something is wrong with if-statement?

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

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: something is wrong with if-statement?

    That shows the first element of the array as 0.0. The array has no negative numbers.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Nov 2013
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Nov 2013
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Nov 2013
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Nov 2013
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: something is wrong with if-statement?

    Quote Originally Posted by Norm View Post
    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. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: something is wrong with if-statement?

    i just tested and it worked.
    Glad you got it working.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Nov 2013
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: something is wrong with if-statement?

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

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Nov 2013
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Junior Member
    Join Date
    Nov 2013
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: something is wrong with if-statement?

    Good luck.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

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

Similar Threads

  1. what's wrong with this statement
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 30th, 2013, 01:18 AM
  2. [SOLVED] What's Wrong With My Code: If Statement.
    By Java Programmer in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 9th, 2012, 08:31 PM
  3. What is wrong with my if/else statement?
    By CSUTD in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 24th, 2011, 02:34 PM
  4. whats wrong with my if statement
    By semicolon in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 23rd, 2011, 04:28 PM
  5. what is wrong with my return statement??????
    By amr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 13th, 2010, 07:55 PM