0

I have a stupid question that I'm having trouble finding the solution for. I'm writing a chatroom code and need to assign users usernames. I figured I'd throw a JOptionPane up before they access the chatroom. Here is the little bit of code I threw together for that:

int i = -1;
      while(i < 0)
      {
         String name = JOptionPane.showInputDialog("Enter your username: ");
         if (name.length() > 0)
         {
            i++;
         }
         else {}
      }

When I hit the inner-class it tells me it's got no idea what 'name' is with this error:

Client.java:93: error: cannot find symbol
        writer.append("  " + name + ": " + input.getText() + "\n");
                             ^
symbol: variable name
Client.java:94: error: cannot find symbol
        output.append("  " + name + ": " + input.getText() + "\n");               
                             ^
symbol: variable name

Can someone just give me a quick hand on how to solve that problem? Why can't 'name' carry over to my actionPerformed inner-class?

  • 2
    `name` only has context within the `while` loop` in which is declared. You could make it an instance field, but we'd really need more context to know if that's the right solution to your problem – MadProgrammer Dec 18 '15 at 02:49
  • 1
    What do you mean by inner-class? I don't see any in your code? Also you probably want to read about *scope* and maybe class *fields*. – Pshemo Dec 18 '15 at 02:49

1 Answers1

0

You can make that string variable to which you are saving the input from the JOption dialog box as a public global variable. That should make it accessible from inner classes. This question might be helpful to learn about inheritance. it should show you how to set variables so they can be inherited correctly. Difference between private, public, and protected inheritance

Community
  • 1
  • 1