i have a number of errors which are i don't know how to correct them.
i just learn how to create a method as well as call the method.so i took a try on what i have learn by doing this code which required the users to key in their name.
import javax.swing.JOptionpane;
public class nameInputMethod
{
public static void main(String [] args)
{
String str;
String nameSaya;
nameSaya=JOptionPane.showInputDialog("Type down your name");
nameM(nameSaya);
}
public static void nameM()
{
JOptionPane.showMessageDialog(null,"My name is:"+nameM);
}
Please check my coding error
Started by farhanyun91, Aug 12 2010 02:51 AM
41 replies to this topic
#1
Posted 12 August 2010 - 02:51 AM
|
|
|
#2
Posted 12 August 2010 - 04:12 AM
Please use code tags.
Anyway, i fixed the code. I hope you learn something from it:
Anyway, i fixed the code. I hope you learn something from it:
import javax.swing.JOptionPane;
public class NameInput{
public static void main(String[] args){
String nameSaya;
nameSaya=JOptionPane.showInputDialog("Type down your name");
nameM(nameSaya);
}
public static void nameM(String name){
JOptionPane.showMessageDialog(null,"My name is:"+name);
}
}
#3
Posted 12 August 2010 - 04:39 AM
thank you so much for helping me out.
okay i have something that is puzzling me here.
public static void nameM(String name)
as far as i concern,there will be no parameter if the return type is void,isn't it??
but why did you put parameter in it and it is running without error detected?
okay i have something that is puzzling me here.
public static void nameM(String name)
as far as i concern,there will be no parameter if the return type is void,isn't it??
but why did you put parameter in it and it is running without error detected?
#4
Posted 12 August 2010 - 04:59 AM
yeah except fo misspelling the import line which was a minor misstake easy noticable, when you want to to send something as a parameter in a method as in: you need to write argument(s) to the method, that is what kind of and how many parameter it should expect. What this argument tells the method is like: "You will be provided with a String as the parameter and you will store it at the variable 'name' of the type String".
Quote
nameM(nameSaya);
#5
Posted 12 August 2010 - 05:13 AM
am i rite by saying that i have to declare the variable name as String in void return type?is that what you think i should do
#6
Posted 12 August 2010 - 05:18 AM
what would be the error in this coding?
import javax.swing.JOptionPane;
public class temperetureMethodInput
{
public static void main(String [] args)
{
String str;
double minimum_tempereture;
double maximum_tempereture;
double average_tempereture;
str=JOptionPane.showInputDialog("Key in minimum tempereture");
minimum_tempereture=Double.parseDouble(str);
str=JOptionPabe.showInputDialog("Key in maximum tempereture");
maximum_tempereture=Double.parseDouble(str);
average_tempereture=tempereture(minimum_tempereture,maximum_tempereture);
}
public static double tempereture(double min_temp)
{
double max_temp;
double ave_temp;
ave_temp=(min_temp+max_temp)/2;
return ave_temp;
}
}
import javax.swing.JOptionPane;
public class temperetureMethodInput
{
public static void main(String [] args)
{
String str;
double minimum_tempereture;
double maximum_tempereture;
double average_tempereture;
str=JOptionPane.showInputDialog("Key in minimum tempereture");
minimum_tempereture=Double.parseDouble(str);
str=JOptionPabe.showInputDialog("Key in maximum tempereture");
maximum_tempereture=Double.parseDouble(str);
average_tempereture=tempereture(minimum_tempereture,maximum_tempereture);
}
public static double tempereture(double min_temp)
{
double max_temp;
double ave_temp;
ave_temp=(min_temp+max_temp)/2;
return ave_temp;
}
}
#7
Posted 12 August 2010 - 05:20 AM
Oh, you learned nothing from my previous post, so i'm not going to help you ..
#8
Posted 12 August 2010 - 05:25 AM
i tried to understand how can void return type can accept an argument.is it just accepts argument for certain cases where we need the users to key in something??please correct me if im wrong
#9
Posted 12 August 2010 - 05:31 AM
The 'void' is not a keyword for method parameters. It defines what the method returns.
For example:
Another example:
As you can see, the return type and input parameters aren't connected at all.
For example:
public int length(String str){
// some code here
return theLengthOfString;
}
This method takes String as an input, but returns an int.Another example:
public void printString(String str){
System.out.println(str);
}
This method takes String as an input, but returns nothing (void).As you can see, the return type and input parameters aren't connected at all.
#10
Posted 12 August 2010 - 05:35 AM
alright.it is vivider .would i say that void would not produce any return value but can be inserted arguments?
#11
Posted 12 August 2010 - 05:43 AM
void does not return any value. You can define a method with any type of parameters you wish, it doesn't matter what it's return type is.
You used a method before, which is declared like that:
public void showMessageDialog(Component, Object);
As you can see, this method also returns void, but produces a nice little window with text inside.
You used a method before, which is declared like that:
public void showMessageDialog(Component, Object);
As you can see, this method also returns void, but produces a nice little window with text inside.
#12
Posted 12 August 2010 - 05:47 AM
i got it!!!thank you so much.i really appreciate it,mind to scrutinize the second problem??


Sign In
Create Account


Back to top









