Solved

Method to append int to a second int

Posted on 2004-10-16
29
353 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
Independent Software Vendors: 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to Alipay 10 71
collection output issue 9 65
program arguments vs VM arguments 4 30
American Express @Work site and Java 4 67
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

733 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