take single integers from multiple numbers comprised of 2

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 1023 Views - Last Post: 27 July 2010 - 12:40 PM Rate Topic: -----

Topic Sponsor:

#1 irmoathlete  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 98
  • Joined: 09-July 10

take single integers from multiple numbers comprised of 2

Posted 21 July 2010 - 10:05 PM

the goal is to take single integers and add them together of integers comprised of more than 1 integers.

for example, to take the list 1, 2 ,5 ,12 , 15, 8

and do the following:



1+2+5+(1+2) + (1+5) + 8

the numbers that need to be split have been added to an array list i just need to know how i can split single integers?


Is This A Good Question/Topic? 0
  • +

Replies To: take single integers from multiple numbers comprised of 2

#2 Crunch  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 135
  • View blog
  • Posts: 1,197
  • Joined: 28-July 09

Re: take single integers from multiple numbers comprised of 2

Posted 21 July 2010 - 10:38 PM

Convert the individual integer to a string object

And then do a charAt operation to get the individual digit.
Was This Post Helpful? 1
  • +
  • -

#3 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: take single integers from multiple numbers comprised of 2

Posted 21 July 2010 - 10:45 PM

If you follow Crunch's method, you will be getting the integer value for representing a character, not the actual value of the split numbers.

Why not try using a StringTokenizer for those numbers that are more than 1 digit in length?
Was This Post Helpful? 1
  • +
  • -

#4 irmoathlete  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 98
  • Joined: 09-July 10

Re: take single integers from multiple numbers comprised of 2

Posted 21 July 2010 - 10:47 PM

ok cool, i was on the right track i converted to the string object! =]

so using a string tokenizer i could just parse after i separated right?
Was This Post Helpful? 0
  • +
  • -

#5 Crunch  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 135
  • View blog
  • Posts: 1,197
  • Joined: 28-July 09

Re: take single integers from multiple numbers comprised of 2

Posted 21 July 2010 - 10:53 PM

View Postbcranger, on 22 July 2010 - 04:45 AM, said:

If you follow Crunch's method, you will be getting the integer value for representing a character, not the actual value of the split numbers.

Why not try using a StringTokenizer for those numbers that are more than 1 digit in length?


    int aInt = 9881;
    String aString = Integer.toString(aInt);
    int y = aString.charAt(0);    // <-- You will get the ascii value
    char x = aString.charAt(0);   // So do this
    System.out.println(aString.charAt(0));  // Or this to get the actual digit
    System.out.println(y);
    System.out.println(x);


Was This Post Helpful? 1
  • +
  • -

#6 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: take single integers from multiple numbers comprised of 2

Posted 22 July 2010 - 12:06 AM

View PostCrunch, on 21 July 2010 - 09:53 PM, said:

View Postbcranger, on 22 July 2010 - 04:45 AM, said:

If you follow Crunch's method, you will be getting the integer value for representing a character, not the actual value of the split numbers.

Why not try using a StringTokenizer for those numbers that are more than 1 digit in length?


    int aInt = 9881;
    String aString = Integer.toString(aInt);
    int y = aString.charAt(0);    // <-- You will get the ascii value
    char x = aString.charAt(0);   // So do this
    System.out.println(aString.charAt(0));  // Or this to get the actual digit
    System.out.println(y);
    System.out.println(x);




Your method still does now allow him to add the two digits together, as adding any value returned using the charAt() method will esentially return to you the integer value of that character in ASCII. Adding "char x" to another, let's say "char y", will not give you, for instance "9", it will instead give you the sum of the two ASCII values. It DOES print out the values, but I believe he wanted to add them together.
Was This Post Helpful? 0
  • +
  • -

#7 Crunch  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 135
  • View blog
  • Posts: 1,197
  • Joined: 28-July 09

Re: take single integers from multiple numbers comprised of 2

Posted 22 July 2010 - 12:25 AM

View Postbcranger, on 22 July 2010 - 06:06 AM, said:

View PostCrunch, on 21 July 2010 - 09:53 PM, said:

    int aInt = 9881;
    String aString = Integer.toString(aInt);
    int y = aString.charAt(0);    // <-- You will get the ascii value
    char x = aString.charAt(0);   // So do this
    System.out.println(aString.charAt(0));  // Or this to get the actual digit
    System.out.println(y);
    System.out.println(x);




Your method still does now allow him to add the two digits together, as adding any value returned using the charAt() method will esentially return to you the integer value of that character in ASCII. Adding "char x" to another, let's say "char y", will not give you, for instance "9", it will instead give you the sum of the two ASCII values. It DOES print out the values, but I believe he wanted to add them together.


  System.out.println(aString.charAt(0) -48);



You can ha :bigsmile:

This post has been edited by Crunch: 22 July 2010 - 12:27 AM

Was This Post Helpful? 0
  • +
  • -

#8 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: take single integers from multiple numbers comprised of 2

Posted 22 July 2010 - 12:30 AM

Yes, with that modification it would work :)

And he wanted the total sum, not the sum for every individual number :P
Was This Post Helpful? 0
  • +
  • -

#9 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1100
  • View blog
  • Posts: 2,643
  • Joined: 11-December 07

Re: take single integers from multiple numbers comprised of 2

Posted 22 July 2010 - 01:52 AM

I'm really not a fan of using text processing to work with numbers. Although the text code may look superficially simple, there is a LOT going on behind the scenes to make the compeuter deal with text. It's really not all that difficult with two simple maths operations:

Mod gives the remainder of a division so:

58173 % 10 = 3.

Integer division divides and rounds down

58173 / 10 = 5817

5817 % 10 = 7
5817 / 10 = 581

581 % 10 = 1
581 / 10 = 85

And so on.....

We can put this in a loop:

int number = 58173;
int sumDigits = 0;
while (number > 0) {
  int lastDigit = number % 10;
  sumDigits += lastDigit;
  number /= 10;
}


I'll leave it to you to put that in a method and have another loop to deal with several numbers.
Was This Post Helpful? 2
  • +
  • -

#10 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon


Reputation: 2567
  • View blog
  • Posts: 10,202
  • Joined: 15-July 08

Re: take single integers from multiple numbers comprised of 2

Posted 22 July 2010 - 05:36 AM

I think that method was far more robust than using text anyway...using characters can get really complicated...

@Crunch and bcranger, I'd use String if I were doing it your way, not characters. Something like this:

String stringNum = s.charAt(0) + ""; // Converts to String
int num = Integer.parseInt(stringNum);



That method is far easier than messing with chars
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon


Reputation: 7493
  • View blog
  • Posts: 28,836
  • Joined: 27-December 08

Re: take single integers from multiple numbers comprised of 2

Posted 22 July 2010 - 05:50 AM

I agree with them about using chars, and maybe converting to ints based on the ASCII table. It's more memory efficient than creating a new String object each time you want to parse to an int, which is what happens because of String immutability. Albeit since you aren't using the new keyword to create your Strings, repeated digits will be pulled from the String pool.
Was This Post Helpful? 1
  • +
  • -

#12 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon


Reputation: 2567
  • View blog
  • Posts: 10,202
  • Joined: 15-July 08

Re: take single integers from multiple numbers comprised of 2

Posted 22 July 2010 - 05:55 AM

View Postmacosxnerd101, on 22 July 2010 - 07:50 AM, said:

I agree with them about using chars, and maybe converting to ints based on the ASCII table. It's more memory efficient than creating a new String object each time you want to parse to an int, which is what happens because of String immutability. Albeit since you aren't using the new keyword to create your Strings, repeated digits will be pulled from the String pool.

oooohhhh....right...Geez, how did I miss that one?!
Was This Post Helpful? 0
  • +
  • -

#13 NeoTifa  Icon User is offline

  • ¿Dónde están mis pantalones?
  • member icon


Reputation: 1399
  • View blog
  • Posts: 13,833
  • Joined: 24-September 08

Re: take single integers from multiple numbers comprised of 2

Posted 22 July 2010 - 06:19 AM

Idk if I'm missing something, but from skimming the thread, this is the solution I thought of. Idk if you can dereference a char, but Character has a .toString(), so I don't see why not. I don't have a compiler to test it for you.

String s = ""//list of numbers here
int sum = 0;
char c = '';

for(int i = 0; i < s.length(); i++) {
c = s.charAt(i);
if(c.isDigit()) {
sum += Integer.parseInt(c.toString()); //can't parse chars
} //end if
} //end for

//display sum



This code should loop through every character in the string, test if it's a digit, and if it is, turns it to a string to be parsed as an integer. Then it's added to the previous sum. Hope that helps ^__^

This post has been edited by NeoTifa: 22 July 2010 - 06:20 AM

Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon


Reputation: 7493
  • View blog
  • Posts: 28,836
  • Joined: 27-December 08

Re: take single integers from multiple numbers comprised of 2

Posted 22 July 2010 - 06:25 AM

@NeoTifa: That's more or less what Dogstopper just suggested. Except char doesn't have a toString(), so you'd need to use the Character wrapper class. :)
Was This Post Helpful? 0
  • +
  • -

#15 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon


Reputation: 2567
  • View blog
  • Posts: 10,202
  • Joined: 15-July 08

Re: take single integers from multiple numbers comprised of 2

Posted 22 July 2010 - 06:28 AM

View PostNeoTifa, on 22 July 2010 - 08:19 AM, said:

Idk if I'm missing something, but from skimming the thread, this is the solution I thought of. Idk if you can dereference a char, but Character has a .toString(), so I don't see why not. I don't have a compiler to test it for you.

String s = ""//list of numbers here
int sum = 0;
char c = '';

for(int i = 0; i < s.length(); i++) {
c = s.charAt(i);
if(c.isDigit()) {
sum += Integer.parseInt(c.toString()); //can't parse chars
} //end if
} //end for

//display sum



This code should loop through every character in the string, test if it's a digit, and if it is, turns it to a string to be parsed as an integer. Then it's added to the previous sum. Hope that helps ^__^


You have so many syntax errors it's not even funny. You are trying to dereference a char. You need to use the static methods of the Character class.
Character.isDigit(c)

Character.toString(c)


And macosxnerd101 made a point that said constructing Strings is more time consuming than using chars. However, I think cfoley's solution was best.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2