Hello, I am a student. In my Java class we are learning about classes and objects this week. To me this has been very difficult. I have written the following bit of code for the program but I cannot get it to run. The piece of the program is designed to get two different user inputs. For this I have designed two different classes to get this information. This part of the code works with no problem.
However, the second part of the code I am required to create a class to determine whether if either of the two inputted numbers are greater than 100 or a negative number. For this section I beleive I have the majority of the code properly written but I keep getting the following error in the main method under the CalculateNegOrGreater section: cannot convert void to integer.
If anyone would be willing to provide me assistance I would appreciate it.
import javax.swing.JOptionPane;
public class Calculate {
public static void main(String[] args)
{
UserInput1 firstInput = new UserInput1();
int Input1 = firstInput.getUserInput1();
System.out.println("First Input value = " + Input1);//prints the returned entered integer
//calls the UserInput2 class to obtain second integer entered by user
UserInput2 secondInput = new UserInput2();
int Input2 = secondInput.getUserInput2();
System.out.println("Second Input value = " + Input2);// prints the returned entered integer
//I am attempting to call this class but not have it return to the main
CalculateNegOrGreater negOrGreater = new CalculateNegOrGreater();
int negGreater = negOrGreater.getCalculateNegOrGreater(Input1, Input2);//This line shows error cannot covert void to string
}
}
//Class for input number 1
class UserInput1
{//Configuration for user input1
public int getUserInput1()
{//Ask user for first integer
String input = JOptionPane.showInputDialog("Please enter an integer");
return Integer.parseInt(input);//Return first integer to main
}
}
//Class for input number 2
class UserInput2
{//Configuration for user input2
public int getUserInput2()
{//ask user to enter second integer
String input2 = JOptionPane.showInputDialog("Please enter a second integer");
return Integer.parseInt(input2);//return second integer to main
}
}
//Class for CalculateNegOrGreater
class CalculateNegOrGreater
{
public void getCalculateNegOrGreater(int Input1, int Input2){//I am wanting this to output the below given statements if they are true
if(Input1 < 0){
JOptionPane.showMessageDialog(null, "The first integer you entered is negative");
}
else if(Input1 > 100){
JOptionPane.showMessageDialog(null, "The first integer you entered is great than 100");
}
if (Input2 < 0){
JOptionPane.showMessageDialog(null, "The second integer you entered is negative");
}
else if(Input2 > 100){
JOptionPane.showMessageDialog(null, "The second integer you entered is greater than 100");
}
}
}