0

Eclipse gives me this error message: The literal [number] of type int is out of range. I know that's impossible to define an int with a number which is higher than 2147483647 or lower than -2147483647. The numbers which I want to set as an int are mobile phone numbers (in Switzerland they can have maximum the value of 0999999999 - Isn't this already enough low to define it as an int??). I changed the variable type of the attributes date and number in the class Person to long. Now I receive the error message which I mentionned above. Why? How can I fix this?

Thank you.

Here is my code:

public class PhoneBook {

    public static void main(String[] args) {

            Person sister = new Person ();
            Person brother = new Person();
            Person mother = new Person();
            Person father = new Person();

            mother.name = "Gertrud";
            mother.date = 12041969;
            mother.number = 0792467286;

            sister.name = "Theresa";
            sister.date = 02112002;
            sister.number = 0794567843;

            brother.name = "Megnitz";
            brother.date = 01041999;
            brother.number = 0775685421;

            father.name = "Randolph";
            father.date = 21071966;
            father.number = 0764532178;


    }

}

___________________________________________________________________________________________________



public class Person {

    String name;
    long date;
    long number;

    public void print() {
        System.out.println(name + ", " + "born on " + date + " / " + number);
    }

}
Bakeaynado
  • 25
  • 6
  • Phone numbers need to be strings, not numbers. A long literal should end in L. Never put an extra 0 at the start of an integral literal, because that indicates the number is in octal. – khelwood Jan 02 '20 at 15:31
  • If you need any convincing of why you shouldn't record a number as an int (with a leading zero), try printing `mother.number`. – Andy Turner Jan 02 '20 at 15:32
  • Phone numbers, like *so many* simple, everyday things (e.g. dates and times, addresses, names...), are *stupidly* complicated. Do yourself a favour by using a library: [here is Google's](https://github.com/google/libphonenumber), but others are available too. – Andy Turner Jan 02 '20 at 15:34
  • Thank you all for the answers. The thing with the *octal* is clear now. This is an assignment for school, so I have to use int for the phone numbers. But it makes sense to use String. – Bakeaynado Jan 02 '20 at 15:37
  • Don't save phone numbers in number format (long, int, ect) because you will lose phone numbers when the first digit is zero. Change the telephone number type to String. – koding Jan 02 '20 at 15:37

0 Answers0