Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

if condition doesn't work  RSS feed

 
sahar sa
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear friends,
I have a problem with a simpl eprogram, I am trying to write a program which choose the maximum number in an array, and after that, according to the indicator value returns an string!. But it seems that in "if(t[i] > maximum)" command if condition does't act because it doesn't go inside the if loop... do you know why? and how can I fix it?
here is the code:



thank you in advance!
Sahar.
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Sahar,

First thing - Follow naming convention while doing code
You ought start method with lower case like getMax() or something - Not the Max()

Second i can very well see in the output that it is going inside the if loop. What's the problem? When i ran the program it shows me following output -
inside MAX method
inside for
i: 0
inside for
i: 1
inside if // it is inside the if condition.
indicator: 1, 4.5
inside for
i: 2
inside for
i: 3
inside if
indicator: 3, 7.5


Please let me know if there is some problem in understanding.

Cheers
Patricia
 
sahar sa
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear,
But its not the same for me! here is the output:


I was thinking the problem is from array which has been passed but I cheked and its ok!
I passed the array by the following command:

any Idea what is wrong?
thank you so much
sahar.

thnx, sahar
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, what's in the array that you're passing in as an argument? There are a number of cases where that "if" will not be entered -- for example, if t[0] is the largest element in the array, or if all the elements are equal, or if the array is of length 1.
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes exactly...If will be executed based on the data provided. Program seems ok to me. Can you please specify what the data is and what do you expect from this program as an output?

Cheers
Patricia
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as an extension to what EFH says, the 'if' checks the indicator variable, but what is the default value?
 
sahar sa
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ooooooh god!
thankkkk you soooooo much! I could never find it !
yes the problem was that the first element was the biggest so indicator did't get any value and finally it return null to me!
thank you guys again!
Sahar.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!