0

I'm building a log in system for a java program which will be working out employees wages for certain stores. Before I go any further I am fully aware this isn't the safest way to make a log in so please don't try explaining because I'm aware. My code so far is below

public class project15 
{
public static void main(String[] args);
{ 
String store = store501;
String stroePin = 1468dty;
String personalPin = abc123;
String inputStore;
String inputPin;
String inputPersonal
inputStore = JOptionPane.showInputDialog("please enter the store number");
inputPin = JOptionPane.showInputDialog("please enter the stores unique ID");
inputPersonal = JOptionPane.showInputDialog("please enter your persoanl ID");
if (inputStore==store && inputPin==stroePin && inputPersonal==personalPin){
System.out.println("correct information");
}
else 
{
System.out.println("incorrect information");
}
}
}

I get an error on the first three String values saying, for example, "store501 can not be resolved to a variable. " Everything else seems to be working okay. Can anyone spot where I've gone wrong and please explain as I am eager to learn from any mistakes I may have made. Thanks in advance and I appreciate any feedback.

  • 1
    You need to put quotes : `String store = "store501";` _"A string literal consists of zero or more characters enclosed in double quotes"_ `StringLiteral: " StringCharacters "` – Alexis C. Jan 07 '14 at 14:21
  • 1
    fyi, you have a typo's in: stroePin->storePin "please enter your persoanl ID"->"please enter your personal ID" – M21B8 Jan 07 '14 at 14:22
  • 1
    also you're comparing strings using `str1==str2` instead of `str1.equals(str2)` – turbo Jan 07 '14 at 14:24

5 Answers5

1

It seems like store501, 1468dty and abc123 are variables that are not defined.

You must define them before using. Something like:

String store501 = "some string";

Or maybe you want them to be Strings. In that case:

String store =  "store501"; // Strings are between ""

Read more about Strings in Java docs, or Java tutorials.

Remember:

To compare String use:

str1.equals(str2)

not

str1 == str2

Read this "How to compare Strings in Java"

Community
  • 1
  • 1
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
  • I've just done this and now I get "incorrect information" printed to the screen if I type the correct or incorrect information – user3169319 Jan 07 '14 at 14:27
  • Strings are compared with `equals` see edit. And read more about that please. – Christian Tapia Jan 07 '14 at 14:29
  • Thank you I've just read through that it's very helpful information, just wondering is there a way to shut down the program if someone types in the wrong information 10 times? – user3169319 Jan 07 '14 at 14:44
  • Try a `while` or a `for` loop. One time more, you can read about them in [Java tutorials](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html) and [here for while](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html) – Christian Tapia Jan 07 '14 at 14:52
1

There's more than one mistake in your program. First, there's what everybody else said about declaring your String constants. But then there's another mistake too.

You are using == to compare Strings. The Java expression a == b compares two variables and it returns true if both variables reference the same Object. It does not compare the objects to see whether or not they have the same value.

Your program could set personalPin="abc123"; and the user could type in abc123 when prompted for inputPersonal, and yet personalPin and inputPersonal are likely to refer to two different String objects that both happen to have the same value. That is to say, personalPin==inputPersonal could be false even though both strings are "abc123".

The way to fix it is to use the String.equals() function to compare the contents of the Strings:

personalPin.equals(inputPersonal)

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
0

When you create a String you need to surround it with ""

String store = "store501";
String stroePin = "1468dty";
String personalPin = "abc123";
String inputStore;
String inputPin;
String inputPersonal
inputStore = JOptionPane.showInputDialog("please enter the store number");
inputPin = JOptionPane.showInputDialog("please enter the stores unique ID");
inputPersonal = JOptionPane.showInputDialog("please enter your persoanl ID");
if (store.equals(inputStore) && stroePin.equals(inputPin) && personalPin.equals(inputPersonal)){
  System.out.println("correct information");
} else {
  System.out.println("incorrect information");
}
M21B8
  • 1,867
  • 10
  • 20
  • I've just done this and now I get "incorrect information" printed to the screen if I type the correct or incorrect information – user3169319 Jan 07 '14 at 14:25
  • never compare strings with == . You should always use .equals(). I'll update the code above for you – M21B8 Jan 07 '14 at 14:31
0

Change your code to :

String store = "store501";
String stroePin = "1468dty";
String personalPin = "abc123";
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0

If they are string variables then you should place them within "".You can either do it this way:

String store = "store501";
String stroePin = "1468dty";
String personalPin = "abc123";

OR:

String store;
String stroePin;
String personalPin;
store="store501";
stroePin="1468dty";
personalPin="abc123";
AnOldSoul
  • 4,017
  • 12
  • 57
  • 118