pass String


Dear Experts!
i m new in java....thats why i have some basic problems in
java programming.my problem is that ....i make a program such as


import java.awt.Graphics;
public class phonebook extends java.applet.Applet
{
String fname;

void name()
{

fname = "toomuch";
}

public void paint(Graphics g){

phonebook pb =new phonebook();
pb.name();
g.drawString(""+fname,1,10);

}}

when i run as
C:jdk1.4in>javac phonebook.java

C:jdk1.4in>appletviewer/phonebook.html
.............then
An java applet appear.... but where is "null" written.
But i want pass the ,string fname from the class to main()

Comments

  • :
    : Dear Experts!
    : i m new in java....thats why i have some basic problems in
    : java programming.my problem is that ....i make a program such as
    :
    :
    : import java.awt.Graphics;
    : public class phonebook extends java.applet.Applet
    : {
    : String fname;
    :
    : void name()
    : {
    :
    : fname = "toomuch";
    : }
    :
    : public void paint(Graphics g){
    :
    : phonebook pb =new phonebook();
    : pb.name();
    : g.drawString(""+fname,1,10);
    :
    : }}
    :
    : when i run as
    : C:jdk1.4in>javac phonebook.java
    :
    : C:jdk1.4in>appletviewer/phonebook.html
    : .............then
    : An java applet appear.... but where is "null" written.
    : But i want pass the ,string fname from the class to main()
    :
    :

    The correct term is you want to return the string value to main. What you need to do is change void in the method header to the appropriate type. Then use the keyword return along with the String being returned.

    in main you will want an assign the value being returned to another String reference. Like so: someString = .name();

    You might also need to explicitly make name() public.

    edit: I missed the part where this is an applet. Applets are different from application is java, there is no explicit main function that starts the execution.


    [italic][blue]Just my 2 bits[/blue][/italic]

  • :
    : Dear Experts!
    : i m new in java....thats why i have some basic problems in
    : java programming.my problem is that ....i make a program such as
    :
    :
    : import java.awt.Graphics;
    : public class phonebook extends java.applet.Applet
    : {
    : String fname;
    :
    : void name()
    : {
    :
    : fname = "toomuch";
    : }
    :
    : public void paint(Graphics g){
    :
    : phonebook pb =new phonebook();
    : pb.name();
    : g.drawString(""+fname,1,10);
    :
    : }}
    :
    : when i run as
    : C:jdk1.4in>javac phonebook.java
    :
    : C:jdk1.4in>appletviewer/phonebook.html
    : .............then
    : An java applet appear.... but where is "null" written.
    : But i want pass the ,string fname from the class to main()
    :
    :

    The problam is you create a object of pb and the you set with pb.name()
    the object varibale fname = tomuch but you access your locat fname variable which is not init....

    So you have to do these.......

    [code]
    import java.awt.Graphics;
    public class phonebook extends java.applet.Applet
    {
    private String fname;

    public void name()
    {

    this.fname = "toomuch";
    }

    public void paint(Graphics g){

    [red]name();[/red][blue]now you call your function of these object[/blue]

    g.drawString(""+fname,1,10);

    }}

    [/code]

    or the second way is to access the variable of the init. pb object

    [code]
    import java.awt.Graphics;
    public class phonebook extends java.applet.Applet
    {
    private String fname;

    public void name()
    {

    this.fname = "toomuch";
    }

    public void paint(Graphics g){

    phonebook pb =new phonebook();

    pb.name();[red]here you create a new object in the existing object so fname exists in pb and in the local applet object[/red]

    g.drawString(""+[red]pb.fname[/red],1,10);

    }}

    [/code]
  • just a note and a question.

    I don't think you need to go

    g.drawString(""+fname,1,10);

    fname is already a string! just pass it normally and save a few instructions, or what you call it on the vm

    Also, to pah, you wrote

    this.fname="something"

    [red]this[/red] is not required, but do you consider this good coding practice, making it more clear? Just curious.
    {2}rIng
  • : just a note and a question.
    :
    : I don't think you need to go
    :
    : g.drawString(""+fname,1,10);
    :
    : fname is already a string! just pass it normally and save a few instructions, or what you call it on the vm
    :
    : Also, to pah, you wrote
    :
    : this.fname="something"
    :
    : [red]this[/red] is not required, but do you consider this good coding practice, making it more clear? Just curious.
    : {2}rIng
    :

    The [red]this[/red] isnt required normaly i use the this
    if the local variable and the passing variable have the same
    name to identife the parameters

    [code]
    import java.awt.Graphics;
    public class phonebook extends java.applet.Applet
    {
    private String fname;

    public void name([red]String fname[/red])
    {

    [blue]this.[/blue]fname = [red]fname[/red];
    }

    public void paint(Graphics g){

    name();now you call your function of these object

    g.drawString(""+fname,1,10);

    }}

    [/code]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories