2

So I have this variable long hashvalue and I want to add digits to it if something is true.

I have this array int arr[3][3] with the numbers -1, 1, 0, 0, -1, 1, 0, 0, -1 in it

I need something like this:

if(array[i][j] == -1){ //add digit 2 to hashvalue } else if(array[i][j] == 1){ //add digit 1 to hashvalue } else if(array[i][j] == 0){ //add digit 0 to hashvalue } 

hashvalue = 210021002

2
  • 2
    Pure code-writing requests are off-topic on Stack Overflow -- we expect questions here to relate to specific programming problems -- but we will happily help you write it yourself! Tell us what you've tried, and where you are stuck. This will also help us answer your question better. Commented Jul 12, 2015 at 20:35
  • 1
    Hint . Make hashvalue String and then concat Commented Jul 12, 2015 at 20:38

2 Answers 2

3

You can simply make a string for the hashvalue and adding the digits to the string via the + (or +=) operator. To receive a long object from the string, you can use the Long.parseLong() function.

String hashvalue = ""; // Add your digits to the String here hashvalue += "1"; // Convert the String to a long long finalHashvalue = Long.parseLong(hashvalue); 
Sign up to request clarification or add additional context in comments.

Comments

1

you can just multiply hashvalue by 10 and then add the wanted digit. Like:

if(array[i][j] == -1){ hashValue = hashValue * 10 + 2; } else if(array[i][j] == 1){ hashValue = hashValue * 10 + 1; } else if(array[i][j] == 0){ hashValue = hashValue * 10 + 0; } 

Imagine your hashValue starts with 3, if you multiply it by 10 it becomes 30, if you add the digit, let's say 2, it becomes 32. The digit has been added. I hope I helped!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.