Jump to content

Please check my coding error

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
41 replies to this topic

#1
farhanyun91

farhanyun91

    Learning Programmer

  • Members
  • PipPipPip
  • 53 posts
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);
}

#2
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
Please use code tags.

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
farhanyun91

farhanyun91

    Learning Programmer

  • Members
  • PipPipPip
  • 53 posts
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?

#4
Roman Y

Roman Y

    Programmer

  • Members
  • PipPipPipPip
  • 189 posts
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:

Quote

nameM(nameSaya);
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".

#5
farhanyun91

farhanyun91

    Learning Programmer

  • Members
  • PipPipPip
  • 53 posts
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
farhanyun91

farhanyun91

    Learning Programmer

  • Members
  • PipPipPip
  • 53 posts
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;
}
}

#7
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
Oh, you learned nothing from my previous post, so i'm not going to help you ..

#8
farhanyun91

farhanyun91

    Learning Programmer

  • Members
  • PipPipPip
  • 53 posts
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
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
The 'void' is not a keyword for method parameters. It defines what the method returns.

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
farhanyun91

farhanyun91

    Learning Programmer

  • Members
  • PipPipPip
  • 53 posts
alright.it is vivider .would i say that void would not produce any return value but can be inserted arguments?

#11
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
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.

#12
farhanyun91

farhanyun91

    Learning Programmer

  • Members
  • PipPipPip
  • 53 posts
i got it!!!thank you so much.i really appreciate it,mind to scrutinize the second problem??