1

Just like what the title says i have been trying to do this for hours.

System.out.print ("Please input the first name: ");
name1 = in.next();

if (name1.substring(0,1).equals("[a-zA-Z]"))
System.out.println("\t" + name1.substring(0,1).toUpperCase()+name1.substring(1).toLowerCase() + "  is in the name list");

else if (name1.substring(0,1).equals("[0-9]"));
System.out.println("error");

This is part of the code that is not working out for me. I dont really know where i went wrong im a beginner at this. With this code whatever I write i get the second System.out.println back which is error. I want the program to display an error message if the string doesnt start with a number. Also, please use the if statement to do that because I couldn't use something I have not covered otherwise I would have searched it.

Abz
  • 13
  • 3
  • 4
    What do you think `equals("a-zA-Z")` does and why do you think so? – Sotirios Delimanolis Feb 13 '15 at 04:43
  • Indeed. `equals` is not a regex match. – aruisdante Feb 13 '15 at 04:43
  • @SotiriosDelimanolis that is the last thing I used after using "contains" thinking it is the same as contains. – Abz Feb 13 '15 at 04:45
  • possible duplicate of [How to check if a string starts with one of several prefixes?](http://stackoverflow.com/questions/9790584/how-to-check-if-a-string-starts-with-one-of-several-prefixes) – Tarik Feb 13 '15 at 04:47
  • @Tarik: Going to have to disagree on that dupe there. This is only checking a single letter after all; there are better ways to do it. – Makoto Feb 13 '15 at 04:48
  • @Makoto in that case it's duplicate to that: http://stackoverflow.com/questions/8540015/determine-if-string-starts-with-letters-a-through-i – Tarik Feb 13 '15 at 04:50
  • @Tarik - That's not a duplicate. They are looking for specific letters. The OP is asking for any letter – Ascalonian Feb 13 '15 at 04:51
  • 1
    If trying to decide between `Character.isAlphabetic` versus `Character.isLetter`, check out the post about [the difference between them](http://stackoverflow.com/questions/18304804/what-is-the-difference-between-character-isalphabetic-and-character-isletter-in) for TL;DR, if it's always English, it doesn't matter :-) – Ascalonian Feb 13 '15 at 04:52
  • 1
    @Tarik: It's *slightly* stronger, but I'm not seeing it as a dupe to this one at all for reasons already elaborated on by Ascalonian. – Makoto Feb 13 '15 at 04:53
  • Yes you have absolutly right Mokoto and Ascalonian on that, and my apologizes to the OP, I can't see how to remove that flag – Tarik Feb 13 '15 at 04:55

4 Answers4

4

This should do it:

if(!Character.isAlphabetic(name1.charAt(0)) {
    //it's not a letter
}
Makoto
  • 104,088
  • 27
  • 192
  • 230
Cat Snacks
  • 96
  • 4
2

I suggest you use Character.isLetter(char), name your variables and define them when you initialize them. Something like

Scanner in = new Scanner(System.in);
System.out.print ("Please input the first name: ");
String firstName = in.next();
if (Character.isLetter(firstName.charAt(0))) {
    // It is a letter.
} else {
    // It is not a letter.
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

Use matches if you want to use REGEX:

if (name1.substring(0,1).matches("[a-zA-Z]"))

equals checks for exact match. It will only be true if your first letter is "[a-zA-Z]" which will never happen.

user3437460
  • 17,253
  • 15
  • 58
  • 106
  • Thankyou for the reply I also forgot to ask if it was possible to ignore the input if it did not start with a letter. I would appreciate it if you helped me in that. – Abz Feb 13 '15 at 05:08
  • @Abz Just saw your message. You can do anything you want to the input including "ignoring" it. But you still need to receive the input with a variable first, else there is no way to check whether the first letter is a letter. – user3437460 Feb 13 '15 at 14:19
0

The reason why you get the second print statement all the time is because you've added a ';' after the else if:

Change

else if (name1.substring(0,1).equals("[0-9]"));

To

else if (name1.substring(0,1).equals("[0-9]"))

Remove that semi colon first!

wO_o
  • 181
  • 3
  • 13
  • 1
    This is syntax suggestion, you could have added it in comments rather then an answer. This does not solve the problem even though. – Naman Gala Feb 13 '15 at 04:55
  • True. But I think my answer does explain why he was getting "error" printed regardless of whatever he gave as an input, which is stated as part of the question. – wO_o Feb 13 '15 at 05:03