0

Hello everyone,

I am currently learning Java. I find it very hard to use string variable. Does string variables need to be declared differently?

Let me explain more:

I have a program(in blueJ) which reads a name and returns it.

public class ReadName
{
    // instance variables - replace the example below with your own
    private String name1;
    
    /**
     * Constructor for objects of class ReadName
     */
    public ReadName(String nameT)
    {
        name1= nameT;
        
    }
public String getName()
{
    return name1;

}
  
}

When I try to input a name, I get "Error: cannot find symbol -variable...."

When I do this with intergers it works perfectly. Please tell me what I am doing wrong?

6
Contributors
8
Replies
9
Views
5 Years
Discussion Span
Last Post by Taywin
Featured Replies
  • [QUOTE=Prateek nandan;1667457]string can never be assigned directly... you can use for loop.. for(int i=0;i<name1.length();i++) name[i]=name1.charAt(i);[/QUOTE] Unless I have completely misunderstood this post... it's complete nonsense. Read More

0

The class itself doesn't seem to be wrong. How do you call or instantiate the class object in your other class/method?

0

I don' t have any other class or methods. Am I supposed to add one?

Thank you

0

Hello,

I know what I am doing wrong. I was suppose to add "" to input. It is working now.

0

Hello everyone,

I am currently learning Java. I find it very hard to use string variable. Does string variables need to be declared differently?

Let me explain more:

I have a program(in blueJ) which reads a name and returns it.

public class ReadName
{
    // instance variables - replace the example below with your own
    private String name1;
    
    /**
     * Constructor for objects of class ReadName
     */
    public ReadName(String nameT)
    {
        name1= nameT;
        
    }
public String getName()
{
    return name1;

}
  
}

When I try to input a name, I get "Error: cannot find symbol -variable...."

When I do this with intergers it works perfectly. Please tell me what I am doing wrong?

string can never be assigned directly...
you can use for loop..
for(int i=0;i<name1.length();i++)
name=name1.charAt(i);

1

string can never be assigned directly...
you can use for loop..
for(int i=0;i<name1.length();i++)
name=name1.charAt(i);

Unless I have completely misunderstood this post... it's complete nonsense.

0

Can you post the code that you are having problem. You need to call the class ReadName - instantiate it.
Can you post the code that you call and gives you the error

0

Hello,

I know what I am doing wrong. I was suppose to add "" to input. It is working now.

I think the OP has already solved the problem. As I thought, the OP instantiated it incorrectly before. I guess this thread should be closed...

Edited by Taywin: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.