- #1
- 9
- 0
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:
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)
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);
//Expected Answer
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)