Java - Solving a problem, getting an error

  • Comp Sci
  • Thread starter Bimpo
  • Start date
  • Tags
    Error Java
In summary, the code has an error that is preventing the programmer from finding the max or min temperature for a year.
  • #1
Bimpo
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:
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
  • #2
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?
 
  • #3
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:

Related to Java - Solving a problem, getting an error

1. Why is my Java program giving me an error?

There could be many reasons why your Java program is giving you an error. Some common reasons include syntax errors, logical errors, or incorrect data types. It's important to carefully review your code and check for any mistakes that may be causing the error.

2. How can I fix a Java error?

To fix a Java error, you first need to identify the root cause of the error. Once you have identified the problem, you can make the necessary changes to your code to fix the error. It's also helpful to use debugging tools or consult online resources for troubleshooting tips.

3. What is a NullPointerException in Java?

A NullPointerException in Java is an error that occurs when you try to access or use an object that has not been initialized or is set to null. This means that the object does not exist or has not been assigned a value, resulting in a runtime error.

4. Why is my Java program not producing the expected output?

If your Java program is not producing the expected output, it could be due to a variety of reasons. Some common causes include incorrect logic or algorithm, incorrect input data, or a bug in the code. It's important to carefully review your code and test it with different inputs to identify the issue.

5. How can I prevent errors in my Java program?

To prevent errors in your Java program, it's important to follow best coding practices and pay attention to detail. This includes properly commenting your code, using meaningful variable names, and testing your code with different inputs. It's also helpful to use debugging tools and regularly review and refactor your code to catch any potential errors.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top