Jump to content

Java: Error "void cannot be converted to String"

Hip
Go to solution Solved by MotionFlex,
public static void showMessageDialog(Component parentComponent,
                                     Object message)

https://docs.oracle.com/javase/8/docs/api/javax/swing/JOptionPane.html#showMessageDialog-java.awt.Component-java.lang.Object-

 

showMessageDialog doesn't return anything. So change:

a = JOptionPane.showMessageDialog(null, "b is "+ b);

to:

JOptionPane.showMessageDialog(null, "b is "+ b);

 

Hello there,

 

I would like to know why I can't run this code:

 

import javax.swing.*;

public class 2
{

	public static void main(String[] args)
	{

		boolean b;
		String a;
	
		b = false;

		

		a = JOptionPane.showMessageDialog(null, "b is "+ b);
		System.exit(0);
	
		}
}

 

I always get the error message "void cannot be converted to String".

 

Thanks in advance!

 

 

 

I managed to run it like this:

 

		boolean b;
		
		b = false;
		

		JOptionPane.showMessageDialog(null, "b is "+ b);
		System.exit(0);

 

But I still wonder why I get the error message with the first code.

Link to comment
Share on other sites

Link to post
Share on other sites

public static void showMessageDialog(Component parentComponent,
                                     Object message)

https://docs.oracle.com/javase/8/docs/api/javax/swing/JOptionPane.html#showMessageDialog-java.awt.Component-java.lang.Object-

 

showMessageDialog doesn't return anything. So change:

a = JOptionPane.showMessageDialog(null, "b is "+ b);

to:

JOptionPane.showMessageDialog(null, "b is "+ b);

 

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, MotionFlex said:

showMessageDialog doesn't return anything. So change:

 

 

What do you mean by that? I thought it is meant to return something I set in the () ?

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, Hip said:

What do you mean by that? I thought it is meant to return something I set in the () ?

I'm not entirely sure what you mean by that.

 

The showMessageDialog function creates a dialog based on the arguments passed to it ( The stuff in the () ). It doesn't return any values.

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, MotionFlex said:

 

 

The showMessageDialog function creates a dialog based on the arguments passed to it ( The stuff in the () ). It doesn't return any values.

Ok now I understand. What could be a function that returns a value? Thanks for your help.

Link to comment
Share on other sites

Link to post
Share on other sites

15 minutes ago, Hip said:

Ok now I understand. What could be a function that returns a value? Thanks for your help.

Any method whos return type is not void, will return a value.

For example:

int myVar = Math.round(1.5);

Math.round is an example of a method which returns a value. In this case, it will take 1 argument (a float/double) and return that value, rounded to the nearest whole number. In this case, myVar will have a value of 2.

 

If you check the java docs, you can see the parameters for each method, and what it returns.

 

https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#round-double-

Link to comment
Share on other sites

Link to post
Share on other sites

20 minutes ago, Hip said:

Ok now I understand. What could be a function that returns a value? Thanks for your help.

A return function can be a ton of different things, some examples:

public static void main(String[] args)
{
	// Get a greeting in your program with a customized name in it (could base it of user input).
	string name = "John Doe";
	string greeting = getGreeting(name);
    // I know Java uses something else, you can do sout -> Tab and it does a similar line to Console.WriteLine.. This is just for showcase porpuses..
    Console.WriteLine(greeting);
    
    // Get a bool (true/false) from a function to see if someone is of a legal driking age:
    int age = 17;
    bool legalDrinkingAge = getLegalAge(age);
    
  	// Do something with that boolean.
    if(legalDrinkingAge)
    {
    	Console.Writeline("You are of legal drinking age, at age: " + age;
    }
    else
    {
    	Console.WriteLine("No alchohol for you, you still have to wait " + (18 - age) + " years before you can drink alcohol";
    }
    
    // Calculate the area of something based on userinput.
    double width = 100.0 // or userinput
    double height = 200.0 // or userinput
    double areaOfLand = calculateArea(width, height);
    Console.WriteLine("The area of this land is: " + areaOfLand + " meters squared.");
}

// Receive a name (for example via user input) and place it in a greeting
// returns: greeting (a string).
string getGreeting(string receivedName)
{
	string retval = "Hello " + receivedName + ", welcome to the program!";
    
    return retval;
}

// Checks whether the age this function got as a parameter is above or below the legal drinking age. True = above, false = below.
bool getLegalAge(int receivedAge)
{
	bool retval = false;
    
	if(receivedAge > 18)
    {
    	retval = true;
    }
    
    return retval;
}

// Calculate the area of a land, when given a width and a height.
// Returns: the area of said land, a double.
double calculateArea(double width, double height)
{
	double retval = width * height; 
    
    return retval;
}

 

The way you call and setup a return function:

// How to call a return function:
VariableType VariableName = FunctionName(PossibleParameters);

//How to set up a return function:

VariableType FunctionName (PossibleParameterVarType PossibleParameter)
{
	VariableType returnValueVar;
    
    // Do whatever you need to do with the PossibleParameter and returnValueVar here
    
    return returnValueVar;
}

 

The examples I listed above are just three of a million different implementations of return functions. These functions are typically a good way to write code, since you can much more easily re-use code and generally you get much cleaner code too.

 

EDIT:

Oh, I should mention void too. Void function basically are the opposite of return functions, they don't return anything. They just do something on their own.

You will often have pieces of code that work independent of other pieces of code, meaning you don't need to return anything. I can't think of a good example at the moment, but there are tons of uses for it (for example the main function in Java is a void).

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×