-1

I tried this code but its not working

import java.util.*;
class StringBuffer
{
    public static void main(String[] args)
    {
        StringBuffer Name1=new StringBuffer();
        Scanner in=new Scanner(System.in);
        System.out.println("Enter a string: ");
        Name1.append(in.nextLine());
        System.out.println(Name1);
    }
}

If the StringBuffer is per-defined then its works. But it fails to take input from user.

Parthav
  • 21
  • 1
  • 6

4 Answers4

1

You are using class StringBuffer which is already a built-in class in Java.
Ref: Oracle doc

When you instantiate an object like StringBuffer Name1=new StringBuffer(); within your custom defined class StringBuffer, then it creates an object with reference to your custom defined class StringBuffer.

You need to create an object of Java's inbuilt StringBuffer class.

Change your class name to something else or use StringBuilder which is not thread safe but faster than StringBuffer.

Additionally, using Scanner class to read your input.
How can I read input from the console using the Scanner class in Java?

Community
  • 1
  • 1
Devendra Lattu
  • 2,532
  • 2
  • 15
  • 25
  • I changed the class name to St but there is an error E:\>javac St.java St.java:9: error: cannot find symbol Name1.append(in.nextLine()); ^ symbol: method append(String) location: variable Name1 of type StringBuffer 1 error – Parthav May 04 '17 at 18:43
  • Also do `St Name1=new St();` – Devendra Lattu May 04 '17 at 18:58
0

your code is correct but the class name is wrong. change your class name and run it.

Anshul Sharma
  • 3,078
  • 1
  • 10
  • 17
  • I changed the class name to St but there is an error E:\>javac St.java St.java:9: error: cannot find symbol Name1.append(in.nextLine()); ^ symbol: method append(String) location: variable Name1 of type StringBuffer 1 error – Parthav May 04 '17 at 17:57
0

StringBuffer is a predefined class in java.lang package which you are trying to use but as the name of your class clashes with it, it uses your class definition instead of the predefined one.

Here Name1.append(in.nextLine()); you are calling append method which is not implemented by your class hence will raise an error cannot find symbol. Change your class name to something unique and you are good to go. Nothing wrong with the input.

Rajeev Singh
  • 3,207
  • 2
  • 16
  • 27
  • I changed the class name to St but there is an error E:\>javac St.java St.java:9: error: cannot find symbol Name1.append(in.nextLine()); ^ symbol: method append(String) location: variable Name1 of type StringBuffer 1 error – Parthav May 04 '17 at 18:35
0

no need to change your class name wheather it's StringBuffer or not. But do one change in your class. Surely it'll take input from user side

public class StringBuffer {

    public static void main(String[] args) {
        java.lang.StringBuffer Name1 = new java.lang.StringBuffer();
        Scanner in=new Scanner(System.in);
        System.out.println("Enter a string: ");
        Name1.append(in.nextLine());
        System.out.println(Name1);
    }
}

It will solve your problem.

Vpn_talent
  • 720
  • 7
  • 20
  • @Parthav if your problem has been solved. then accept the answer. https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work#answer-5235 – Vpn_talent May 10 '17 at 08:21