-2

I'm trying to debug an error of my code (I'm still very new to Java) for an assignment.

public static void main(String[] args) throws Exception {
    System.out.println("Hi, Welcome!");
    
    Scanner inpt = new Scanner(System.in); 
    System.out.print("What can I call u? : ");
    String user = inpt.nextLine();
    Pelanggan user = new Pelanggan();
    }

"Pelanggan" is the name of the class that I made. I want to assign the value of user into the class Pelanggan but I got this error

Duplicate local variable user

Then I tried to change the code into this

public static void main(String[] args) throws Exception {
    System.out.println("Hi, Welcome!");
    
    Scanner inpt = new Scanner(System.in); 
    System.out.print("What can I call u? : ");
    Pelanggan user = inpt.nextLine();
    user = new Pelanggan();
    }

Then I got this error

Type mismatch: cannot convert from String to Pelanggan

I have tried to look for the solution but still haven't found a solution which is worked out on my code.

How can I fix this?

Any help would be greatly appreciated! Thank you

  • 1
    you have two variables with an identical name. just rename one of them. On your improvement: nextLine reads a String => Pelanggan user = inpt.nextLine(); a Pelanggan is not a String, hence it must be mapped to an instance of Pelanggan, how, that depends on your Pelanggan class – Stultuske Sep 13 '21 at 05:51
  • first you need to understand core java of opps concept and others then you will try to code – Rajesh Patel Sep 13 '21 at 06:07
  • @Joe no, that was the first issue the OP had already corrected – Stultuske Sep 13 '21 at 08:30

3 Answers3

2

Making assumptions on the Pelanggan class, you should create a String field in your Pelanggan class and implement it like this:

public class Pelanggan {
   String name;

   Pelanggan(String name) {
       this.name = name
   }
}

And main part of the code:

public static void main(String[] args) throws Exception {
    System.out.println("Hi, Welcome!");
    
    Scanner inpt = new Scanner(System.in); //variabel simpan inputan nama user 
    System.out.print("What can I call u? : ");
    Pelanggan user = new Pelanggan(inpt.nextLine());
    }

Pelanggan class may be different from what I assumed it to be for the solution. Main idea is this: if you want to store a String in your class, you can store it as a field and give it as a parameter to the constructor of your class.

Muhteva
  • 2,729
  • 2
  • 9
  • 21
  • You are making assumptions of the Pelanggan class you can't make without knowing more specifics. For all we know, Pelanggan is supposed to contain a numeric value based on the input – Stultuske Sep 13 '21 at 05:52
1

There's three parts to a pointer in Java.

Type name = value;
  • The Type is the kind of data the pointer is going to hold.

  • The name is how we can address the pointer in our code.

  • The value is the data the pointer points to.

When you declare a pointer like

String user = "John Doe";

You tell the compiler 2 things.

    1. I have a pointer to a String object.
    1. I'm going to call that String user.

You can now use the value stored in user wherever you might need a String data type. However, you cannot then declare another

MyClass user = new MyClass();

Now the compiler is confused because you already defined a pointer named user, so you get a redeclaration error. (Scope is important here, as you can define it again in different scope, but that's beyond the scope of this answer).

How about your second problem?

You can absolutely change the value of a pointer (if it's not a final pointer), but this

String user = "John Doe";
user = new MyClass();

will not compile because you told the compiler that user is a reference to a String. MyClass is not a String (it could be if it were a subclass of String, but that is again out of the scope of this answer) so it gives you a type mismatch error.

The simple answer is to just use a different name for each of your pointers. If you really want your reference to your Palanggan instance to be user, then name your String pointer something else.

Liftoff
  • 24,717
  • 13
  • 66
  • 119
0

Check the return type : String java.util.Scanner.nextLine()

So its better to have variable in Pelanggan class and set either in method or in constructor

siddu
  • 31
  • 3