# Java - Solving a problem, getting an error

• Comp Sci
Hey guys, there is this problem from my school in which I'm having troubles..

This code is from past school lab (assignment) assigned a loong time ago
And I figured I would go back to it, to practice my coding skill, since I wasn't able to solve it back then.

So here is the code:
Code:
import java.util.* ;

public class MaxFinder
{
public static void main(String[] args)
{
//Pre given codes
final String TEMPERATURES = "-2 -1 4 11 18 24 27 26 21 14 7 0" ;

Scanner scanner = new Scanner(TEMPERATURES) ;
int hottestTemp = 0 ;
int hottestMonth = 0 ;
int coldestTemp = 0 ;
int coldestMonth = 0 ;

//My code
int temp2 = 0;
int errorDestroy = 0;
int[] currentTemp = new int[12];
for(int i = 0; i<=12; i++)
{
errorDestroy = i;
String temp = scanner.nextLine();
temp2 = Integer.parseInt(temp);
currentTemp[i] = temp2;
if(errorDestroy >= 1)
{
hottestTemp = Math.max(currentTemp[i], currentTemp[i-1]);
coldestTemp = Math.min(currentTemp[i], currentTemp[i-1]);
}
}
System.out.println("Hot:" + hottestTemp);
System.out.println("Cold:" + coldestTemp);

System.out.println("Expected: ") ;
System.out.println("Hottest month is 7 (27 C)") ;
System.out.println("Coldest month is 1 (-2 C)") ;
}
}

My job is to get the hottest month and coldest month, and its temperatures.
So right now, I'm concentrating on getting the temperatures and will worry about the month later.

-The variable temp job is to get the next string from scanner which had the line for TEMPERATURES.

-The variable temp2 is to translate the temp into int.

-errorDestroy is my way to avoid error when I compare two values
and in which one did not exist for Math.max and Math.min.

-currentTemp[] is an array to put in the values for the temperatures.

And then I just print out all the compared values.

I am getting an error from this, I don't know why or how, but this is what is shows:
Exception in thread "main" java.lang.NumberFormatException: For input string: "-2 -1 4 11 18 24 27 26 21 14 7 0"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at MaxFinder.main(MaxFinder.java:31)

## Answers and Replies

Mark44
Mentor
I believe that the problem you describe is caused by using nextLine(). This method advances the scanner past the current line and returns what was skipped. What you should be using is nextInt().

I still don't get what you're trying to do with errorDestroy. I'm pretty sure your code would be better without it. You shouldn't get an error when you compare two integer values. You can make sure that the next value you read is in int by calling hasNextInt(). If this method returns true, you can call nextInt() to get it.

One other thing- why is there 13 months of data if you're trying to find the max or min temperature for a year?

Thanks a lot, really.

For some reason I am blind today, and my brain isnt functioning properly
Thanks for pointing out the errors!

as for error destroy, for Math.min and Math.max
it wont let me compare something that didnt exist
so i used it as a point in which i would let it allow to start comparing.
but since my logic was wrong i didnt need it i nthe first place

Last edited: