Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

substring ints.  RSS feed

 
Theresa Marlin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to define a 10digit phone number as an int, then extract the area code, the middle three numbers, and the last 4 numbers. I'm not entirely sure how to do this, because I can't use substring, as it is obviously for strings. Is there such a command as subint? If so, should I use it?
This is what I have so far:
class Telephone
{
public static void main(String args[ ])
{
int telephone = 1234567890;
System.out.println (telephone);
int t1 = telephone.subint(7);
System.out.println (t1);
}
}
And this is the error message I get:
Error: int cannot be dereferenced
Thanks so much!!!
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll give you a hint:

12345 % 10 = 5
12345 % 100 = 45
12345 % 1000 = 345
12345 / 10 = 1234
12345 / 100 = 123
12345 / 1000 = 12
 
Theresa Marlin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THANK YOU!!!
 
Campbell Ritchie
Marshal
Posts: 55687
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Theresa Marlin wrote:I need to define a 10digit phone number as an int . . .
Welcome to JavaRanch

Well done picking up Garrett's hint.

But have they told you about the limits of int? Have you tried a ten-digit telephone number beginning with 3? Or any telephone number beginning with 0? Have you found out why you get different sorts of errors for those?
 
Theresa Marlin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, it's pretty awesome!
Thanks, I felt like a real idiot after he gave me that hint. I had just read two chapters, one on doing math, and one on substrings. I had received my assignments in chronological order according to the chapters, but at the end she threw in one, and I assumed it was for the last chapter, but it wasn't. Fortunately, I have ranch hands are bartenders to help me out!
As far as ints, I now they can't be decimals, and to use a double for decimals. I have only done the preliminary stages of the assignment, I think next week I might get variations with the telephone number starting with 3 or 0. I'm pretty sure why it would error with a 0, but unsure what the problem is with a 3. Hopefully as I go along, I will figure it out.
 
Campbell Ritchie
Marshal
Posts: 55687
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome

Everybody forgets things like that. Think nothing of it.

You will only get errors with 10 digits beginning with 3. Not with 9 digits. See whether that helps. And please confirm you know why you will get errors with numbers beginning with 0.
 
Theresa Marlin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that helped greatly with the whole division by 3 thing. I now understand why I was getting errors. I also (think) I know why I get errors with 0, because you can't divide something into 0, right?
Thanks!
 
Rob Spoor
Sheriff
Posts: 21089
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you dividing by 0?

What Campbell meant was numbers that start with 0: That is not assigning the integer value 123456789 to telephone, because if a number starts with 0, 0x or 0X that number will be treated differently. I'll let you find out what these mean yourself
 
Campbell Ritchie
Marshal
Posts: 55687
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob is right. I meant numbers like 0123456789. And when I said a number starting with 3 I meant one like this 345678910. You will have no problems with that number because it has 9 digits in, but you will have problems if you try 3456789100 which has 10 digits in.
 
Rob Spoor
Sheriff
Posts: 21089
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In fact, any number larger than 2,147,483,647 (Integer.MAX_VALUE) will cause problems.
 
Theresa Marlin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know understand why you can't start with a 3. You would have to use a double for that, right?
As for 0 bringing an error message, I'm not sure, the only thing I could think of is it is impossible to divide something into 0 (0/8) is that right?
Thanks!
 
Rob Spoor
Sheriff
Posts: 21089
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
0/8 is simply 0.

Read section 3.10.1 of http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html and you'll find out why any int starting with 0 is treated differently.
 
Theresa Marlin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, thanks, I understand it now!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!