toCharArray in Java works but sum of elements not - java

I am testing the following piece of code:
static int superDigit(String n, int k) {
char[] concatenatedN = n.toCharArray();
int superDigit = 0;
int sumDigits = 0;
char[] totalSum;
if (n.length() > 0) {
sumDigits = 0;
for (int j = 0; j < concatenatedN.length; j++) {
sumDigits = sumDigits + (int)concatenatedN[j];
System.out.println(" sumDigits: " + sumDigits + " ,concatenatedN[j]: " + concatenatedN[j]);
}
totalSum = String.valueOf(sumDigits * k).toCharArray();
superDigit = sumDigitsRecursive(totalSum);
} //end if
return superDigit;
}
For some reason that I don't know sumDigits variable must be the sum of array elements (concatenatedN[j]) but something weird happens and instead of sum, the following output is showed (when I do System.out.println):
sumDigits: 53 ,concatenatedN[j]: 5
sumDigits: 104 ,concatenatedN[j]: 3
sumDigits: 154 ,concatenatedN[j]: 2
sumDigits: 203 ,concatenatedN[j]: 1
Result must be ---> array is 5,3,2,1 ---> result = 5 + 3 + 2 + 1

When you cast a char to an int it gets converted to its underlying int value, which is its acsii value. This is clearly not what you want. Instead you can use Character.getNumericValue() to get the int value:
sumDigits = sumDigits + Character.getNumericValue(concatenatedN[j]);

You are adding the numeric unicode values of the characters of the string. Luckily, these values are consecutive, so you can convert these face values to the numbers they represent by subtracting the values of '0':
for (int j = 0; j < concatenatedN.length; j++) {
sumDigits = sumDigits + (int)(concatenatedN[j] - '0');
}

Related

Sum the digits in a string

I wrote a program that sums the int values in a String. I'm getting the wrong output though. I can't quite figure out the issue. The expected output should be 23 and 29 but I am getting 263 and 269. Any suggestions would be helpful; it seems it is putting a 6 between my outputs for some reason.
public class ParseString
{
String str;
public ParseString(String x)
{
this.str = x;
}
public int sumOfAllDigits()
{
int total = 0;
char[] arr = new char[this.str.length()];
for(int i = 0; i < this.str.length(); i++)
{
arr[i] = this.str.charAt(i);
}
for(int i = 0; i < arr.length; i++)
{
if(arr[i] >= '0' && arr[i] <= '9')
{
total = total + arr[i];
}
}
return total;
}
public class TestParseString
{
public static void main(String[] args)
{
String s1 = "AB458JK2L#4";
ParseString ps1 = new ParseString(s1);
System.out.println("Sum of all digits in \"" + s1 + "\" is: ");
System.out.println(ps1.sumOfAllDigits());
System.out.println();
String s2 = "8927KL3PY";
ParseString ps2 = new ParseString(s2);
System.out.println("Sum of all digits in \"" + s2 + "\" is: ");
System.out.println(ps2.sumOfAllDigits());
}
}
It's not that a 6 is inserted into your sum; it's that your sum is 240 too high. There are 5 digits in each of your test strings. What is missing here is what goes on in the conversion between char and int. A '0' is not 0; when a char is widened to an int for summing, it takes the ASCII value, which for numbers is the represented number plus 48. E.g. '0' -> 48, '1' -> 49, etc.
An extra 48 added 5 times yields an extra 240.
Because the digits are coded in order starting with '0' at 48, you can subtract '0' to take away the unwanted 48.
total = total + arr[i] - '0';
As an aside, as already mentioned in the comments on the question, toCharArray() gets you the char[] for a String more easily than manually copying each character.
The problem lies here:
total = total + arr[i];
arr[i] is a char. When you use the + operator on it, with the other operand being an int, you are actually adding the ASCII value of the character, which is 48 to 57 for 0 to 9.
You think you are doing this:
4 + 5 + 8 + 2 + 4
But actually the program is doing:
52 + 53 + 56 + 50 + 52
So that's why you get such a large number.
You need to parse the characters to get the correct output. One way to do this is to just subtract 48!
total = total + (arr[i] - 48);
Or you can convert it to a string first, then parse it as an int:
total = total + Integer.parseInt(Character.toString(arr[i]));

Having trouble with a for loop evaluating incorrectly

I was having this problem today and I'm not sure what causes it. The following code:
int totWidth = 19;
String header = "Kalle's Numbers";
if (totWidth > header.length()) {
for (int j = 0; j < totWidth / 2 - header.length() / 2; j++) {
header += " ";
}
}
System.out.println(header + "|");
should yield
Kalle's Numbers |
but instead yields
Kalle's Numbers |
I modified the code a little bit by adding a variable "rightSpaces"
int totWidth = 19;
String header = "Kalle's Numbers";
if (totWidth > header.length()) {
int rightSpaces = totWidth / 2 - header.length() / 2;
for (int j = 0; j < rightSpaces; j++) {
header += " ";
}
}
System.out.println(header + "|");
and suddenly it yields the correct output. Why is this?
Because as it iterates through your for loop, you change the length of header by adding a space. So header.length() / 2 in your loop's continuation condition is potentially a different value each time.
What you've done in your second example fixes this problem by evaluating it once.

Why does the for loop output this?

I am just very confused from this homework problem. I do not understand why the values of i and sum come out this way. I just do not understand the concept of the algorithm here, can someone please explain this?
int i = 0;
int sum = 0;
for(i=0; i < 5; i++)
{
sum += i;
}
System.out.println(i + "\n" + sum);
The output is:
5
10
----jGRASP: operation complete.
5 - because there are 5 iterations
10 - because the sum is 10 :)
Sum
Iteration 1: 0 + 0 = 0
Iteration 2: 0 + 1 = 1
Iteration 3: 1 + 2 = 3
Iteration 4: 3 + 3 = 6
Iteration 5: 6 + 4 = 10
Verification code
int i = 0;
int sum = 0;
for (i = 0; i < 5; i++) {
System.out.println(String.format(
"Iteration %s: %s + %s = %s", (i + 1), sum, i, (sum + i)));
sum += i;
}
This code :
int i = 0;
int sum = 0;
for(i=0; i < 5; i++)
{
sum += i;
}
System.out.println(i + "\n" + sum);
output in sum this : 0 + 1 + 2 + 3 + 4 which is equal to 10 and i the number of iterations = 5.
You have created a variable i with value of 0 and then incrementing it 5 times in for-loop. So you got i's value as 5.
Now the value of sum is 0+1+2+3+4 which is 10
Because you iterate through your loop, which makes i == 5, then print it,
Sum goes as below, you are adding i to the previously calculated sum
0 + 1 = 1
1 + 2 = 3
3 + 3 + 6
6 + 4 = 10
Try put your print command inside the loop, they you can see better what's going on.
The only non-obvious thing is (in my opinion): i will be 5, because you used i++, which also incremented i by 1 even though the body did not execute after the last iteration. Inside the body i only can be maximum 4.
int sum = 0; int i = 0;
for (i = 0; i < 5; i++)
{
sum += i;
if (i == 5)
System.out.println("never executed");
};
Other answers tell the other things.

algorithm to create a char array from permutations list in java

I'm sorry for the slightly confusing title, I'm unsure of how to phrase it.
I need to create a char array allowing for every possible permutation of a character set.
If I were to give you:
char[] charSet = {"a", "b", "c"};
BigInteger value = n; //where n is a number >= 0
char[] charArray = createCharArray(value, charSet);
How can I create charArray from value and charSet such that if I ran:
createCharArray(new BigInteger("6"), {"a", "b", "c"});
it would return {"a", "c"}
because
a=1
b=2
c=3
aa=4
ab=5
ac=6
Here's what I have so far:
private char[] createCharArray(BigInteger value, char[] charSet){
List<Character> charArray = new ArrayList<Character>();
if (value.compareTo(this.max) == 0)
System.out.println("");
BigInteger csSize = new BigInteger(String.valueOf(charSet.length));
if(this.powers.isEmpty())
this.powers.add(0, csSize.pow(0));
if(this.sumPowers.isEmpty())
this.sumPowers.add(0, csSize.pow(0));
BigInteger curPow;
int i = 1;
while((curPow = csSize.pow(i)).compareTo(value) <= -1){
if(this.powers.size() <= i)
this.powers.add(i, curPow);
if(this.sumPowers.size() <= i)
this.sumPowers.add(i, this.sumPowers.get(i-1).add(curPow));
i += 1;
}
i -= 1;
while (i >= 0 && value.compareTo(BigInteger.ZERO) >= 0){
if (i <= 1){
int charNum = value.divide(this.sumPowers.get(0)).intValue() - 1;
charArray.add(charSet[charNum]);
}
else{
int charNum = value.divide(this.sumPowers.get(i-1).subtract(BigInteger.ONE)).intValue() - 1;
charArray.add(charSet[charNum]);
}
value = value.subtract(this.powers.get(i));
i -= 1;
}
char[] returnArray = new char[charArray.size()];
int j = 0;
while(j<charArray.size()){
returnArray[j] = charArray.get(j);
j += 1;
}
return returnArray;
}
It certainly could use some help, as a value of 0 fails, values of 1 and 2 succeed, 3-8 fail, 9, 10 succeed, etc.
EDIT: To be clear, the value parameter must be able to be ANY number n > 0. This is why I've chosen BigInteger
Make a class that has two fields:
private char letter;
private int value;
public <classname>(char letter){
this.letter = letter;
value = 0;
}
//Setters and getters
Then in your main when initializing an array (via for loop) set the value to i + 1 (getting rid of 0)
for(int i = 0; i < <yourarray>.length; i ++){
//Assuming you initialized your objects before
<yourarray>[i].<setterforvalue>(i + 1);
}
And then to calculate them together:
for(int i = 0; i < <yourarray>.length; i ++){
for(int j = 0; j < <yourarray>.length; j ++){
if(<yourarray>[i] + <yourarray>[j] == <needednumber>){
//Do what you need to do with the value
}
}
}
Okay after much thinking and eventually breaking it down to using numbers 0-9 instead of characters. Here's the breakdown: Think about how regular base 10 numerals are created.
The number 194 is made up of a 4 in the ones column, a 9 in the tens, and a 1 in the hundreds. The difference between ones, tens, and hundreds is multiplication/division by 10, which is the base.
So I figured that I could mod the 194 by the base (10) to get 4, the ones. then divide by 10 to remove the ones column. mod again to get 9, then divide by 10, mod again to get 1, divide by 10. once the division creates a number that is exactly 0, we are done. This is because we cannot make a number 000194.
For my function, My base is the length of the character set, and the value is like 194 in the example above.
private static void createCharArray(BigInteger value, char[] charSet){
List<Character> charArray = new ArrayList<Character>();
BigInteger csSize = BigInteger.valueOf(charSet.length);
if (value.compareTo(BigInteger.ZERO) == 0)
charArray.add(0, charSet [0]);
else{
BigInteger modded = value.mod(csSize);
BigInteger digit = value.divide(csSize);
while (modded.compareTo(BigInteger.ZERO) != 0 || digit.compareTo(BigInteger.ZERO) != 0){
if(modded.compareTo(BigInteger.ZERO) == 0){
charArray.add(0, charSet[csSize.subtract(BigInteger.ONE).intValue()]);
value = value.subtract(BigInteger.ONE);
}
else
charArray.add(0, charSet[modded.subtract(BigInteger.ONE).intValue()]);
value = value.divide(csSize);
modded = value.mod(csSize);
digit = value.divide(csSize);
}
}
for(char c : charArray)
System.out.print(c);
System.out.println();
}
public static void main(String[] args) {
long start = System.nanoTime();
String characters = "";
characters += "0123456789";
characters += "abcdefghijklmnopqrstuvwxyz";
characters += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
characters += " !\"#$%&'()*+,-./:;<=>?#[\\]^_`{|}~";
char[] cs = characters.toCharArray();
Arrays.sort(cs);
createCharArray(new BigInteger("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"), cs);
long total = System.nanoTime() - start;
System.out.println("Completed in: " + total + " billionths of a second");
System.out.println("Completed in: " + total/1000000 + " thousandth(s) of a second");
}
If you run this, note that that BigInteger 4 lines from the bottom is 100 characters long. On my machine, it takes only 1/1000 th of a second (1 Millisecond).

Check if string is following ISBN-13 in Java

Im trying to check if a string (important that it is a string) that im reading is correct accoring to the rules of ISBN-13. I found a formula
For example, the ISBN-13 check digit of 978-0-306-40615-?
is calculated as follows:
s = 9×1 + 7×3 + 8×1 + 0×3 + 3×1 + 0×3 + 6×1 + 4×3 + 0×1 + 6×3 + 1×1 + 5×3
= 9 + 21 + 8 + 0 + 3 + 0 + 6 + 12 + 0 + 18 + 1 + 15
= 93
93 / 10 = 9 remainder 3
10 – 3 = 7`
My problem is i don't know how to multiply one number with 1 and every other with 3 ? Im guessing a for-loop but i don't know how to start.
You could "simply" use regular expressions:
ISBN(-1(?:(0)|3))?:?\x20+(?(1)(?(2)(?:(?=.{13}$)\d{1,5}([ -])\d{1,7}\3\d{1,6}\3(?:\d|x)$)|(?:(?=.{17}$)97(?:8|9)([ -])\d{1,5}\4\d{1,7}\4\d{1,6}\4\d$))|(?(.{13}$)(?:\d{1,5}([ -])\d{1,7}\5\d{1,6}\5(?:\d|x)$)|(?:(?=.{17}$)97(?:8|9)([ -])\d{1,5}\6\d{1,7}\6\d{1,6}\6\d$)))
You have 6 pairs of (even,odd) numbers, so go through them pairwise.
for (i = 0; i < 6; i++) {
even += array[2*i];
odd += array[2*i+1]*3;
}
checkbit = 10 - (even+odd)%10;
assuming your inputString is ascii:
int odd = 0;
int even = 0;
char[] c = (inputString + "00").replaceAll("[\\-]", "").toCharArray();
for (int i = 0; i < (c.length - 1) / 2; ++i) {
odd += c[2 * i] - 48;
even += c[2 * i + 1] - 48;
}
int result = 10 - (odd + 3 * even) % 10;
This seems to work effectively and is clear.
// Calculates the isbn13 check digit for the 1st 12 digits in the string.
private char isbn13CheckDigit(String str) {
// Sum of the 12 digits.
int sum = 0;
// Digits counted.
int digits = 0;
// Start multiplier at 1. Alternates between 1 and 3.
int multiplier = 1;
// Treat just the 1st 12 digits of the string.
for (int i = 0; i < str.length() && digits < 12; i++) {
// Pull out that character.
char c = str.charAt(i);
// Is it a digit?
if ('0' <= c && c <= '9') {
// Keep the sum.
sum += multiplier * (c - '0');
// Flip multiplier between 1 and 3 by flipping the 2^1 bit.
multiplier ^= 2;
// Count the digits.
digits += 1;
}
}
// What is the check digit?
int checkDigit = (10 - (sum % 10)) % 10;
// Give it back to them in character form.
return (char) (checkDigit + '0');
}
NB: Edited to correctly handle the 0 check digit. See Wikipedia International Standard Book Number for example isbn with check digit of 0.
Paul
Similar, with loop and awful char-to-string-to-int conversions ;]
boolean isISBN13(String s){
String ss = s.replaceAll("[^\\d]", "");
if(ss.length()!=13)
return false;
int sum=0, multi=1;
for(int i=0; i<ss.length()-1; ++i){
sum += multi * Integer.parseInt(String.valueOf(ss.charAt(i)));
multi = (multi+2)%4; //1 or 3
}
return (Integer.parseInt(String.valueOf(ss.charAt(ss.length()))) == (10 - sum%10));
}

Categories