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?

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

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

Thank you

Hello,

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

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);

string can never be assigned directly...

String str="It is possible";

Can't you assign directly?

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.

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

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...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.