Java - Solving a problem, getting an error

  • Context: Comp Sci 
  • Thread starter Thread starter Bimpo
  • Start date Start date
  • Tags Tags
    Error Java
Click For Summary
SUMMARY

The Java code provided in the discussion attempts to find the hottest and coldest temperatures from a predefined string of monthly temperatures. The main issue arises from using nextLine() instead of nextInt(), leading to a NumberFormatException when parsing the temperature values. Additionally, the loop iterates one extra time, causing an ArrayIndexOutOfBoundsException. The suggested solution is to replace nextLine() with nextInt() and to ensure the loop iterates correctly over the 12 months of data.

PREREQUISITES
  • Java programming fundamentals
  • Understanding of exception handling in Java
  • Familiarity with the Scanner class for input
  • Basic knowledge of arrays in Java
NEXT STEPS
  • Learn how to use Scanner.nextInt() for reading integers in Java
  • Research Java exception handling techniques to manage NumberFormatException
  • Study array bounds and iteration techniques in Java
  • Explore best practices for input validation in Java applications
USEFUL FOR

Java developers, computer science students, and anyone looking to improve their coding skills by debugging and optimizing Java programs.

Bimpo
Messages
9
Reaction score
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:
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)
 
Physics news on Phys.org
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 isn't functioning properly
Thanks for pointing out the errors!

as for error destroy, for Math.min and Math.max
it won't 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:

Similar threads

  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K