Solved

Method to append int to a second int

Posted on 2004-10-16
29
355 Views
Last Modified: 2010-03-31
I am trying to construct a method (externalPhoneNumber) to append a four digit number (an int) to a second four digit number (also an int).

For example, if externalPhonePrefix and locationPhoneExtension are 1111 and 2222 respectively then 11112222 will reach the extension number from an outside line.

Below are little 'snippets' of my code:

/**Four digit internal extension telephone number of the office responsible for the booking.*/
private int locationPhoneExtension;

/**Four digit prefix that, when appended to locationPhoneExtension, allows the extension number to be dialled externally.*/
private int externalPhonePrefix;

/**Returns the external phone number of the office responsible for the booking. This is produced by appending locationPhoneExtension to the end of
* externalPhonePrefix.*/
public int externalPhoneNumber() {
}

Thanks for your help experts!
0
Comment
Question by:Ke11ie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 8
  • 4
  • +2
29 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12326834
Sounds suspiciously like homework ;-)
0
 
LVL 2

Author Comment

by:Ke11ie
ID: 12326841
Yeah... working on an assignment :-(
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12326850
OK - we're not allowed to give you code, but here's a clue - the append is a String operation, not a numeric one
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:Ke11ie
ID: 12326860
Yeah - I've been trying to figure it out by using StingBuffer.append

such as:

public int externalPhoneNumber() {
  externalPhoneNumber = new StringBuffer().append(externalPhonePrefix).append(
      locationPhoneExtension);
  return externalPhoneNumber();
}

I've tried many different versions of 'StringBuffer.append' but I keep getting error messages such as 'cannot resolve symbol... blah blah blah'

Or when I've tried different combinations of the 'StringBuffer.append' I get error messages such as 'string:int required'.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12326874
a. you want to append a certain number to something else, so you need to make a least one of those entities a parameter to the method
b. phone numbers are not ints, they're Strings

See what you can do with that
0
 
LVL 2

Author Comment

by:Ke11ie
ID: 12326938
I don't think I'm allowed to add parameters to the method - I have a UML diagram I'm working off - and it specifes all the numbers as ints:

-locationPhoneExtension: int
-externalPhonePrefix: int

+externalPhoneNumber(): int

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12327047
Well that's really wrong ;-) If that's what you've got, you'll have to translate between String and int
0
 
LVL 2

Author Comment

by:Ke11ie
ID: 12327055
And what do you mean by "...you'll have to translate between String and int"?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12327085
Another clue: Integer.parseInt, Integer.toString
0
 
LVL 13

Assisted Solution

by:Webstorm
Webstorm earned 50 total points
ID: 12327127
Hi Ke11ie,

You can use arithmetic operation such as this :
    externalPhonePrefix*10000 + locationPhoneExtension
if locationPhoneExtension has always 4 digits.
If you use String, you will not be able to add the firsts '0' of the locationPhoneExtension number
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 12327136
You can also implements int as int if the total number of digits is <= 9  (2^31),
or you can use long,  if the total number of digits is <= 18  (2^63).

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12327176
>>If you use String, you will not be able to add the firsts '0' of the locationPhoneExtension number

If you're using ints it won't *have* any first '0's ;-)
0
 
LVL 2

Author Comment

by:Ke11ie
ID: 12327208
I had already tried using Integer.parseInt and Integer.toString as you mentioned, CEHJ. I had another go though, and my code finally doesn't return any annoying red errors (I'm using Borland JBuilder X, btw). I have know idea if this part of the code actually works though, this is what I have:

/**Returns the external phone number of the office responsible for the
* booking.*/
public int externalPhoneNumber() {
  Integer.toString(externalPhonePrefix);
  Integer.toString(locationPhoneExtension);
  StringBuffer externalPhoneNumber = new StringBuffer().append(externalPhonePrefix).append(locationPhoneExtension);
  return externalPhoneNumber();
}

However, now that I've seen Webstorm's comment - I think I'll change my code to the arithmetic way - that is so clever and so much easier!!! Thankyou!!! It seems so simple now! I knew that when I finally found the answer I would realise it was something simple!

(Personally, I think that all these phone numbers should be Strings instead of ints, but I'm just doing what I have to do...)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12327229
>>Integer.toString(externalPhonePrefix);

You need to do

String epp = Integer.toString(externalPhonePrefix);
String lpe = Integer.toString(locationPhoneExtension);
return new StringBuffer(epp).append(lpe).tostirng();


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12327236
>>return new StringBuffer(epp).append(lpe).tostirng();

should have been

return new StringBuffer(epp).append(lpe).toString();
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12327250
Using arithmetic operations is *not* appropriate. It will make the code unreusable. e.g. if the number of digits is changed, the code will fail
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 50 total points
ID: 12327259
Oh but you need to return int so:

return Integer.parseInt(StringBuffer(epp).append(lpe).toString());
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12327265
btw, the seemingly irrational business of using int instead of String may have an educative purpose: i.e. to get you used to conversion methods
0
 
LVL 2

Author Comment

by:Ke11ie
ID: 12327266
Why will the arithmetic operation make "...the code will fail"?

The number of digits is always 4.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12327285
Well in that case it's OK to multiply by 10000
0
 
LVL 2

Author Comment

by:Ke11ie
ID: 12327289
Thanks for your help experts, It's 11:45pm Saturday night (in Australia), I've got a half an hour drive home and I'm really tired and sick of thinking for the day so I'll come back to this tomorrow.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12327496
>>The number of digits is always 4.

It must also be said that there's never an 'always' in good software design ;-)
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 12328519
>> It must also be said that there's never an 'always' in good software design ;-)

then:

    public static int getMultiplicator(int ndigits)
    // return power(10,ndigits) (use fast algorithm : O(log(ndigits)) )
    {
         int m=1,d=10;
         while (ndigits>0)
         {
              if ((ndigits&1)!=0) m*=d;
              d*=d;
              ndigits>>=1;
         }
         return m;
    }
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 12330370
you were on the right track originally:

public int externalPhoneNumber() {
//  externalPhoneNumber = new StringBuffer().append(externalPhonePrefix).append(locationPhoneExtension);
// need to declare your variable, and you can use the + operator to append the strings
  String externalPhoneNumber = Integer.toString(externalPhonePrefix) + Integer.toString(locationPhoneExtension);
//  return externalPhoneNumber();
// you need to convert that string to an int
   return Integer.parseInt(externalPhoneNumber);
}
0
 
LVL 2

Author Comment

by:Ke11ie
ID: 12331030
Thanks for your help everyone - experts-exchange is more informative and helpful to me then when I'm actually at university supposedly learning this stuff!
0
 
LVL 92

Expert Comment

by:objects
ID: 12335192
:)
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 12337055
:-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12341775
8-)
0
 
LVL 2

Expert Comment

by:Lance_Frisbee
ID: 12359913
ô¿ô
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question