1
    Scanner input = new Scanner(System.in);
    System.out.print("\tEnter student no.: \t");
    long studNo = input.nextLong();
    System.out.println(studNo);

If I enter 0914325 it only prints 914325. I know because the data type is long. I could use a String data type but what if the user accidentally inputs a letter? Is there any way I could print a number starting at 0?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Ciara
  • 197
  • 1
  • 3
  • 11

3 Answers3

3

There is no such thing as a number that starts with 0.

Numbers are stored as numbers, not text.
Since 004 and 4 are the same number, they are stored identically.

If you care about leading zeroes, you should be using a String, not a Long, since you obviously aren't using it as a number.
The same goes for zip codes or phone numbers.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Funny how you start with "no such thing as a number that starts with `0`", and then end with the example of a phone**number** of something that might start with a zero. The problem here is maybe domain-language :) .. Maybe it would be better to just say that in order to 'save' something with a leading `0`, you can't use the normal "number" types, so you'd need a `String` type. – Nanne Jan 21 '13 at 14:33
  • @Nanne: the way phone numbers are *usually* handled makes them not-numbers: You usually format them in a special way and they can't even be automagically translated into each other. For example vanity number such as "1-800-SOMETHING" are **not** stored as their numeric equivalent. – Joachim Sauer Jan 21 '13 at 14:35
  • 1
    @Nanne: No; I'm saying that phone numbers are also **not** numbers. – SLaks Jan 21 '13 at 14:37
  • obviously I understand that, I'm trying to show how it could be confusing for someone not using the word `number` in the same domain as you are, and I was trying to "prove" the fact by your own 'double' usage of the word number. So it might be clearer to specify something like "in most programming languages, there is no..." – Nanne Jan 21 '13 at 14:44
  • Ftr, only Italy and the Ivory Coast allow phone numbers that start with zero. – djechlin Jan 21 '13 at 15:02
  • @djechlin Here in the Netherlands area codes for phone numbers start with zero, for example 010 = Rotterdam, 020 = Amsterdam etc. You have to dial the leading 0. – Jesper Jan 21 '13 at 15:17
  • @Jesper 0 is the trunk prefix (http://en.wikipedia.org/wiki/Trunk_prefix) - last time I dealt with this problem was in a domain in which we wanted to exclude that (so international calls could be automated.) – djechlin Jan 21 '13 at 15:40
0

As SLaks said, it will always cut off leading zeroes. So you should definitely store it as a String if you want to preserve the original formatting of user input. The best way to check if it is a valid number is to use regex.

String s = input.getLine();
if(!s.matches("[0-9]+")){
    //prompt for input again
}

Basically how this works is it matches the inputted string against the pattern provided.

So I put [0-9]+ as the string to match against, the brackets mean any number in the set 0-9. And the plus sign means the previous pattern repeated one or more times. So this will check every character in the string to make sure they are within the set 0-9. Keep in mind, if you ever convert this to a numeric type again the leading zero will be lost.

Chad Campbell
  • 321
  • 1
  • 9
0

Use

scanner.next("[0-9]+");

This does all the validation you need, throwing a NoSuchElementException on mismatch.

djechlin
  • 59,258
  • 35
  • 162
  • 290