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");
		}
}
}

First of all why create two classes that do the same thing: UserInput1,UserInput2?
Just create only one that prints the message: "Give a number" and create two different instances:

UserInput1 firstNumber = new UserInput1();
int input1 = firstNumber .getUserInput1();

UserInput1 secondNumber = new UserInput1();
int input2 = secondNumber .getUserInput1();

//firstNumber , secondNumber  two different instances

As far you error, the getCalculateNegOrGreater is declared void. It does not return anything, it just prints.
So why you write:
int negGreater = negOrGreater.getCalculateNegOrGreater(Input1, Input2); ?

What is it supposed to return? The getUserInput1 in the UserInput1 class returns something that's why you wrote: int input1 = firstNumber .getUserInput1();
Just write:

negOrGreater.getCalculateNegOrGreater(Input1, Input2);

I appreciate your response. However, I am still confused. So do I just put negOrGreater.getCalculateNegOrGreater(Input1, Input2); in the main??

Do I declare a seperate class or do I leave it under the NumberGames2 class?

And what would I do to have the class print the if values??

Sorry, I appreciate your help if your willing. I am just trying to learn.

You actually do not need classes for the input at all, as they are just making a single call to show your input dialog. A class isn't needed to wrap a single function call. You can put a small helper method in your Calculate class which handles the input part

public static int getInput(){
  int inputValue=0;
  try {
    String input = JOptionPane.showInputDialog("Please enter an integer");
    inputValue = Integer.parseInt(input);
  } catch (NumberFormatException nfe) {
    JOptionPane.showMessageDialog(null, "Entry is not a valid integer.");
  }
  return inputValue;
}

and simply call that method to get your inputs in the main() method. (The method has to be declared static to be used from main() )

int input1 = getInput();
int input2 = getInput();

The class to evaluate the numbers, let's call it Evaluator, should have a separate method for each function that you want it to perform. Each should take the number to evaluate as a parameter and return true or false for the evaluation.

public boolean isGreaterThan100(int value){
  // your code here
  // return true or false if value is greater than 100
}

public boolean isNegative(int value){
  // return true or false if value is negative
}

This separates the functionality of the evaluator to it's individual parts and clearly indicates what it is supposed to do for you. Furthermore, if you wanted the isGreaterThan100() method to be more widely useful, you could let it take two integer inputs : the input value and the value you want to compare it to. That way you have not hard-coded the comparison to 100 in your function. It can compare to any number you want it to.

Granted, these evaluator functions are just doing what a single expression could do for you in your main() method, but the concept of wrapping functionality into classes and methods is really the point here. You are grouping logical operations into functional units that can be used by many parts of your program.

Now you just need to create a single instance of Evaluator and call those functions for each test you need to make against your input integers.

And "Questions??", don't forget to use code tags the next time you post code which is preferably properly indented.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.