0

I've been trying to wreck my brain around this for days. I need to make a method that is going to ask for the user input via the keybaord. I've written it as

private static String getFromUser(String question){
        String s = " ";
        System.out.print(question);
        while(in.hasNext()){
            s = in.next();
            //return s;
        }
        return s;
    }

or

private static String getFromUser(String question){
        String s;
        System.out.print(question);
        s = in.nextLine();
        return s;
    }

HOWEVER When I "uncomment" a skeleton code:

public static void create() {
    in = new Scanner(System.in);

    String name = "";
    String address = "";

    //ask the user for the name and address of the company receiving the invoice
    //String name = getFromUser("name of company to invoice");              // ******TASK ONE******
    //String address = getAddress();                                        // ******TASK TWO******

It keeps stating : "variable name is already defined in method create()". BUT I'm not allowed to change the codes that has already been written. HENCE, I'm only allowed to remove the "//" comment lines. Do I just return a string or would I require to use method overloading ( I don't fully understand method overloading, tbh). Please Advice or give tips. Thank you very much.

ThomasThiebaud
  • 11,331
  • 6
  • 54
  • 77
  • 6
    Remove the `String name = "";`. – Michael Lloyd Lee mlk Nov 16 '15 at 13:36
  • use "name =" and "address =" instead of "String name =" and "String address =" while calling the method getFromUser and getAddress() – Shivam Nov 16 '15 at 13:37
  • With the code uncommented, you're trying to create name and address twice. When you remove the comments, also remove the lines you added that create those two variables. – Don Branson Nov 16 '15 at 13:38
  • Not allowed to remove that piece of code. That's what makes me confused about it. Any advice, for the method itself? Maybe, I'm writing it wrongly or it can be done another way? – Denice Erycka Aguilon Nov 16 '15 at 13:38
  • You do have to remove that line. Don't believe me, go ask the teacher or a fiend of the same course. – Michael Lloyd Lee mlk Nov 16 '15 at 13:39
  • Well, that can't possibly work. – Don Branson Nov 16 '15 at 13:39
  • @DeniceEryckaAguilon: When you un-comment the commented lines, the two lines before them are unnecessary. Just remove them. – David Nov 16 '15 at 13:40
  • You have to remove the two lines that set name and address to blank. – Don Branson Nov 16 '15 at 13:40
  • @david, see the comment above. She said she can't do that. – Don Branson Nov 16 '15 at 13:40
  • 1
    @DonBranson: That requirement is unclear. Exactly *which* lines is the OP "not allowed to change"? Either way, the OP simply needs to remove *one* of the variable declarations. It literally doesn't matter which one. – David Nov 16 '15 at 13:42
  • The 3rd piece of code. The only alterations that can be done is removing the comment marks "//". – Denice Erycka Aguilon Nov 16 '15 at 13:47
  • I've voted to close this question as it is not about Java but how to interpret badly (understood or written) requirements. @DeniceEryckaAguilon: No one here can read your teachers mind, we can only tell you that as explained you can not complete your homework. You should speak to your teacher and get the requirements confirmed. – Michael Lloyd Lee mlk Nov 16 '15 at 13:47
  • argh...teachers hahaha THANK YOU ALL FOR THE HELP THO :D Really appreciate it ^^ @mlk – Denice Erycka Aguilon Nov 16 '15 at 13:48
  • @David, agreed. The OP states that the uncommented lines can't be changed, then in response to my comment says that her added lines can't be removed. Given these two requirements , the code will not compile. I think she's just confused. – Don Branson Nov 16 '15 at 14:48

4 Answers4

4

You have defined the variable two times in the same method scope, what you need to do is to remove the second declaration of the variables like :

String name = "";
String address = "";

// here don't declare the variable again, just use it like
name = getFromUser("name of company to invoice");
address = getAddress(); 
Salah
  • 8,567
  • 3
  • 26
  • 43
  • NOTE: You are only asked to write methods and add them to the InvoiceCreator class. Do not change any other part of the class, except for removing comment marks and delimiters. Yes, but as I've said... I'm not allowed to "alter" the original code other than removing the "//" – Denice Erycka Aguilon Nov 16 '15 at 13:44
  • this is not possible, you have to declare the variable just only one time, if you can't remove anything, then go back to the one who gave you this example and say to him please provide a valid example, because this is wrong example !!! – Salah Nov 16 '15 at 13:54
  • Noted @Salah This is why I've been wrecking my brain around it. Because I know no matter what, if it's declared twice, it will return an error. Thank you very much for the help :) – Denice Erycka Aguilon Nov 16 '15 at 13:58
0

If you're already declaring the variable here:

String name = getFromUser("name of company to invoice");

Then you don't need to also declare it here:

String name = "";

Just remove the shorter one.


You can declare it on one line and then use it later:

String name = "";
name = getFromUser("name of company to invoice");

or do both on one line (which you already do):

String name = getFromUser("name of company to invoice");

But you can't declare the same variable twice in the same scope.

David
  • 208,112
  • 36
  • 198
  • 279
0

The other two answers are correct in that you cannot define a variable twice within the scope of a single method. Are you permitted comment out lines? eg. you could comment out the initial definition like so.

//String name = "";
String address = "";

//ask the user for the name and address of the company receiving the invoice
String name = getFromUser("name of company to invoice");
//String address = getAddress();   

and then do the same for address once you are ready to use task two.

If this is not permitted then you will have to approach your instructor (assuming this is for a class) and indicate it is not possible.

Wirt
  • 31
  • 1
  • 8
-1

Id rather use the Scanner class instead. Makes it look cleaner:

private static String getAnswerFromUser(String question){
    Scanner input = new Scanner(System.in);
    System.out.println("Question: " + question);
    System.out.print("Answer: ");
    String answer = input.next();
    return answer;
}