Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Adding the same integer at the End of the Integer

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends,

back at it again with a new question.

For a project I need to implement a method that adds parts of the value or the whole value to the end of the value.

For example I have my int 11 and want to add 3 digits to 11 from the same int like: 11111.
Second example 10 with 6 more digits: 10101010

So I always need to add the value I have at the end a certain amount of times.
Thought about making an array and entering the numbers but the result needs to be an int to.
I am not able to find a mathematical way at the moment which is smooth? Any ideas to help me out?

Thank you very much!!!
 
Saloon Keeper
Posts: 13283
292
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no good 'mathematical' way, because you must operate on a numeral, not a number. A numeral is the string representation of a number in a specific radix. That means you can solve the problem best using string operators.

First convert the number to a string. Then repeat the string until it has at least the required length. Next, take the substring of the exact required length. Finally, parse the numeral back to an int (or BigInteger).
 
Marshal
Posts: 16597
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to clarify what the exact requirements are, if you had 123 and needed to add 5 digits, would you get 12312312?
 
Michael Grünau
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stephan van Hulst: So I basically transform my Int to a String and repeat the String according to the number of times required?
                            Do I need to put each digit of the Int to a String so I can also cut the value into two parts if I need to split up due to the digit limit?


Junilu Lacar: Thats correct!
 
Michael Grünau
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think i found a relatively easy solution.


So bascially I use my value and transform it in a String and afterward put it in a char array.



So now my numbers are contained in the char array, correct?
I can simply Loop through the array according to the number of digits I can use and add them to a String.
Afterwards we transform the String back to an int?
 
Marshal
Posts: 74069
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That will, I think, meet the case


Quote from the Gondoliers by Gilbert and Sullivan, which I took part in a few weeks ago.
 
Bartender
Posts: 4633
182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using a StringBuilder makes things very easy. And beware: an int goes up to 10 digits.
 
Stephan van Hulst
Saloon Keeper
Posts: 13283
292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Personally I think it's easier not to iterate through char arrays, but simply to append the original string multiple times, and then trim the result down.
 
Michael Grünau
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends,

the problem got a little bit more complicated.

The input of the variables are not normal ints they are written in binary Code for example: 0b011.
Now when I transform the Input to a String and work with my char array it only work with the value 3 not 0b011.

Is there an easy way how I can add the missing parts to the binary Code?
Tried to transform the int to binary with: Integer.toBinary but then I am loosing the first part and the 0 before the 11.

Advice will be appreciated!!1
 
Sheriff
Posts: 26793
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then it looks like your code is converting data to numeric values at some point. But you don't want to do that, it's all supposed to be character manipulation (as far as I can tell). So clear out the code which converts to numeric values.
 
Michael Grünau
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I unfortunately I can't clear out the code that transforms it into numeric values.

Is there a way to access the Input data directly before it gets transformed?
Otherwise I have no idea how I can repeat the Binary Code under the consideration that it might include 011011 (0) that I cannot see when i transform the int to Binary...
 
Stephan van Hulst
Saloon Keeper
Posts: 13283
292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not much we can tell you without knowing how you get the input in the first place.
 
Michael Grünau
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My Main Methods calls the Class with the parameters as below.



in RgbColor I want to extend the Binary Value of 0b011 to 8-Bit so 0b011011.
 
Stephan van Hulst
Saloon Keeper
Posts: 13283
292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm afraid the information is already lost before the constructor is called.

There are two ways you can solve the problem:

1) Let the constructor accept a string that represents an integer value formatted as a binary numeral:

2) Truncate the int value to 3 bits and zero pad it:
 
I don't like that guy. The tiny ad agrees with me.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic