• Post Reply Bookmark Topic Watch Topic
  • New Topic

method that reads the max temperature in a given month  RSS feed

 
Graciete Silva
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, I'm new here. I started learning Java on my own 4 months ago, I get stuck sometimes, like now.
I've created this method to read temperatures in the month of March and its supposed to return the highest temperature ... but it doesnt.
Does anyone has time to chcek where I have a mistake? Can you give me a hint about what I'm doing wrong?




 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When posting code please use Code tags. In the editor window, highlight the code segment of your post, then click on the "Code" button just above the editor window. I've fixed it for you this time.
 
Graciete Silva
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, I'm sorry. I will always do it in the future.
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please describe what you expect to be contained in
m[ 0 ][ n ]
m[ 1 ][ n ]
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

A few things first:

That expression on line 25 does not do what the comment on line 19 says it does. Specifically, the maximum value that you can get from that expression is 4, not 8.  So, your temperatures will only range from -3 to 4.  That's because random.nextInt(8) will give you values from 0 to 7.  Adjust the value you pass to nextInt() accordingly.

Next, you're overcomplicating things for yourself by using an int[][]. Why don't you just use an int[] instead and initialize it to have as many elements as there are days in the month? That way, the array will hold just the temperature values and the expression temperatures[n] stands for "the temperature on the Nth day of the month" where N is 0-based. That is N=0 represents the 1st day, N=1 represents the second day, and so on. So temperatures[0] represents the temperature on March 1st.

I have to attend something. Go over the above first and see if you can make the changes to simplify your code a little. Simplifying the code can actually help you get rid of some problems or it can help you find where your problem was.
 
Graciete Silva
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Carey Brown.

// days from 1 to 31
m[ 0 ][ n ]

// random temperatures
m[ 1 ][ n ]

I will make more comments in the future.

Thank you
Graciete
 
Graciete Silva
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Junilu, I will do that.
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Graciete Silva wrote:Hi Carey Brown.

// days from 1 to 31
m[ 0 ][ n ]

// random temperatures
m[ 1 ][ n ]

I will make more comments in the future.

Thank you
Graciete




Given your use of the 'm' array, your above code should have read


You didn't need the nested loop.

That said. I think Junilu's suggestions are better.

Edit: Removed print statements.
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 47 prints a blank line. The next line just calls your tempMax() method but doesn't do anything with the value that it returns.
 
Graciete Silva
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey, I need the nested loop because I'm printing the days in the month and underneath the correspondent temperatures. Unless I'm doing it wrong ... :/ what do you think?
 
Graciete Silva
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"That expression on line 25 does not do what the comment on line 19 says it does. Specifically, the maximum value that you can get from that expression is 4, not 8.  So, your temperatures will only range from -3 to 4.  That's because random.nextInt(8) will give you values from 0 to 7.  Adjust the value you pass to nextInt() accordingly. "


I'm still struggling with this issue, I'm trying to find out how to make my random range start from -3 ...
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Graciete Silva wrote:
"That expression on line 25 does not do what the comment on line 19 says it does. Specifically, the maximum value that you can get from that expression is 4, not 8.  So, your temperatures will only range from -3 to 4.  That's because random.nextInt(8) will give you values from 0 to 7.  Adjust the value you pass to nextInt() accordingly. "


I'm still struggling with this issue, I'm trying to find out how to make my random range start from -3 ...

Well, you've done that.  But how high should the random temperature go?
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Graciete Silva wrote:Carey, I need the nested loop because I'm printing the days in the month and underneath the correspondent temperatures. Unless I'm doing it wrong ... :/ what do you think?

I suggest creating another method to print out the arrays.

Edit: Removed Random.
Now you no longer need the nested loop.
 
Graciete Silva
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should go up to 8 degrees.
 
Graciete Silva
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Carey, I will do that.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Graciete Silva wrote:It should go up to 8 degrees.

So from -3 to 8, how many degrees is that?
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Graciete Silva wrote:Carey, I need the nested loop because I'm printing the days in the month and underneath the correspondent temperatures. Unless I'm doing it wrong ... :/ what do you think?

I suggest creating another method to print out the arrays.

Now you no longer need the nested loop.

If you go with Junilu's suggestion of a single dimensional array, then it would be

Edit: Removed Random.
 
Graciete Silva
Greenhorn
Posts: 26
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ahh thank you Knute, its 12, so it should be:

m [1] [j] = random.nextInt(12) - 3;
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Graciete Silva wrote:Carey, I need the nested loop because I'm printing the days in the month and underneath the correspondent temperatures. Unless I'm doing it wrong ... :/ what do you think?
It's always best to have a method serve only one purpose. As you see, forcing a nested loop into your method so that the method can also serve the purpose of printing out the data has complicated your code.
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general, to get a random number that ranges from (low .. high), inclusive:
 
Graciete Silva
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are right Carey, thank you so much.

Thank you for the code Junilu.

Graciete
 
Graciete Silva
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, one last question, I  didnt know the printf, I had to look it up.
In the expression "%2d", what is the 2 for?

Many thanks
Graciete
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two decimal places.
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Two decimal places.

%d is for int values and the number after the % tells printf() to print the integer value in a field that's at least two characters wide. So, if the value is 1, it will print as " 1", with a leading space. There will be no leading space if the int value has 2 or more digits.

To print with 2 decimal places, you'd use a format string of "%.2f" with a floating point value.
 
Graciete Silva
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for that Junilu.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!