Solved

Method to append int to a second int

Posted on 2004-10-16
29
349 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
  • 14
  • 8
  • 4
  • +2
29 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Sounds suspiciously like homework ;-)
0
 
LVL 2

Author Comment

by:Ke11ie
Comment Utility
Yeah... working on an assignment :-(
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
 
LVL 2

Author Comment

by:Ke11ie
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
And what do you mean by "...you'll have to translate between String and int"?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Another clue: Integer.parseInt, Integer.toString
0
 
LVL 13

Assisted Solution

by:Webstorm
Webstorm earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
>>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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>return new StringBuffer(epp).append(lpe).tostirng();

should have been

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

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
Oh but you need to return int so:

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

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
Why will the arithmetic operation make "...the code will fail"?

The number of digits is always 4.
0
 
LVL 86

Expert Comment

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

Author Comment

by:Ke11ie
Comment Utility
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
Comment Utility
>>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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
Comment Utility
:)
0
 
LVL 13

Expert Comment

by:Webstorm
Comment Utility
:-)
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
8-)
0
 
LVL 2

Expert Comment

by:Lance_Frisbee
Comment Utility
ô¿ô
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bigHeights  challenge 13 55
Updating Java 9 85
computer science syllabus 3 52
firstChar challenge 13 81
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now