aspose file tools*
The moose likes Beginning Java and the fly likes problem with output in array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem with output in array" Watch "problem with output in array" New topic
Author

problem with output in array

mike longs
Greenhorn

Joined: Mar 07, 2013
Posts: 7
i need to output the index for the lowest value and now all i am able to output is [0][1] im not sure why
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Welcome to the ranch Mike. Always use code tags while referring to Java code:



Much more readable now.


~ Mansukh
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Hi, and welcome to the Ranch!

In the future, please UseCodeTags(←click) when posting code so it will retain formatting and be readable. I added them for you here, and that helped a little, but your indentation is a bit wonky. Please google for and follow Sun's (Oracle's) Java coding conventions.

Look at this bit:


That says:
1. Initialize i to 1.
2. As long as i is larger than tmp.length, do the following:
2.1 execute the loop body
2.2 increment i
2.3 go back to 2.

Do you see the problem there?

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Jeff
I am not able to understand what is the logic that Mike is using to find the smallest element in the array of array of double primitives. Can you explain?
mike longs
Greenhorn

Joined: Mar 07, 2013
Posts: 7
would it be the fact that i am looking if i is larger than temp.length when it should be looking for if i is larger than one of indexs in the array ?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3737
    
  16
mikey mike wrote:would it be the fact that i am looking if i is larger than temp.length when it should be looking for if i is larger than one of indexs in the array ?

No.
Your for loop will only execute if the condition (i > temp.length) is true.
What's the initial value of i. Is that greater than temp.length ?

Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
No.
mike longs
Greenhorn

Joined: Mar 07, 2013
Posts: 7
well i is set to 1, and temp is an array so im not exactly sure what temp.length would even be besides and array thats 11 rows and 1 column( if you count 0 )
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11475
    
  16

note that java does not have multi-dimensional arrays. It only has 1-d arrays, but an array can hold...other arrays.

So, your temp array is really an array that holds arrays of doubles. temp holds 12 arrays, so it has a length of 12.

So, the first time you get to your loop, i is 1, and temp.length is 12. So...how many times will your loop run?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
mike longs
Greenhorn

Joined: Mar 07, 2013
Posts: 7
it would only run one time... thats why i was only getting the number in the first index



and that is able to output any number that is the lowest... but i am still having trouble getting the index to output instead of the actual value
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11475
    
  16

mikey mike wrote:it would only run one time... thats why i was only getting the number in the first index


actually, it shouldn't run at all.


The firs time you come to this line of code, i is set to 1. temp.length is 12. so, the very first time you hit this line, the condition is false, so you drop out of this for loop entirely - no line of code in the body was ever executed.
mike longs
Greenhorn

Joined: Mar 07, 2013
Posts: 7
yea your right but with my updated code


it works fine im just having a problem outputting the index instead of the actual value, i can do this on a one dime array but the 2 dime seems to throw me off
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11475
    
  16

what do you mean by " the index instead of the actual value". The index of what?
mike longs
Greenhorn

Joined: Mar 07, 2013
Posts: 7
ok so for the lowest temp in the array is 3 and i keep only outputting the "3" but i am supposed to return the "index of the lowest temp in the array" so it seems to me it would be at [0][1]. or say that it was the six instead of the "3" it would be [1][1]. and im not sure when the index would output what kind of format it would return in
mike longs
Greenhorn

Joined: Mar 07, 2013
Posts: 7
ok so im pretty sure i figured it out for the min. now i am working on the max and ihave this



i can get it to output the 98 as the max, but if i change indexHighTemp = i , it outputs an 11 instead of a 5. im not sure why my loop is running that many times
and both of these min and max are supposed to be set in one program with indexHighTemp and indexLowTemp as 2 seperate methods and i am having a problem setting the methods
Mack Wilmot
Ranch Hand

Joined: Jul 27, 2011
Posts: 88

It would help if you named your variables for what they really are. indexHighTemp is a double and being used for a temperature and not an index in your code. It should be called highTemp.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with output in array