Skip to main content
gamedev.net
🔒 Locked

Extract int from string in Java

Started by kevin_06s Sep 6, 2010 at 7:28 AM 5 replies 1.4k views
Original Post
kevin_06s
kevin_06s
Having troubles with type conversions in Java.

I have a string containing some number, say, 12345, and I need to loop through the string, extracting a single digit at a time to perform operations on.

Here's what I have so far.

for(int i = 0; i < 10000; ++i) {
String temp = Integer.toString(i);
int sum = 0;
for(int n = 0; n < temp.length(); ++n) {
sum += Math.pow(Integer.parseInt(temp.charAt(n)), 4);
}
}

I tried doing this:
sum += Math.pow(Integer.parseInt(temp.charAt(n).toString()), 4);
but it didn't work, and as I have it right now, it won't let me parse a char, so I need to convert it to string somehow.

Thanks
ApochPiQ
ApochPiQ
My Java is a bit rusty, but this ought to work:

String digit = temp.charAt(n);int digitvalue = Integer.parseInt(digit);// etc.
Sneftel
Sneftel
My Java is also a bit rusty, but I think charAt returns a char. So that toString is necessary.

Incidentally, kevin, "it didn't work" is not as useful a diagnostic description as you might assume.
kevin_06s
kevin_06s
I figured it out. The problem was that I had to do:

sum += Math.pow(Integer.parseInt(Character.toString(temp.charAt(n))), 4);

It seems like this would be so much easier in other languages. I think C++ would just be
sum = pow(temp[n] - '0', 4);
phresnel
phresnel
Why?

Integer division and modulo come to help and will let you avoid strings. There is no need for strings in numerical operations. Composing strings in inner loops is slow. If at all, use a string builder, but that is not good either. It is wrong practice. As soon as you discover a string and char-extractions in numerical operations you know you are making it wrong. String is evil. Strings are evil. They are the tip of the iceberg why nowadays applications waste so much memory and still run as slow as 20 years ago. Use algorithms, not strings. Algorithms are not old-school. They are what makes up a programmer. Strings are the adhesive tape that might make your application work, but they are not the solution. In corner cases and contrarius situations of high lateral acceleration where you want not to display 100 rows but 500, they make your application fail utterly. Because you were to lazy to build up a basic programmer's mindset. They are what seperates your slow-like-ass-application from top-notch software like VMWare, VirtualBox or the linux kernel. The linux kernel is not proper C, but it is properly programmatic and does not use strings in its process scheduler and memory allocator. Integer division and modulo. Avoid strings and learn at least a handful of simple algorithms. Please.
Oysterman
Oysterman
Quote:
Original post by kevin_06s
I figured it out. The problem was that I had to do:

sum += Math.pow(Integer.parseInt(Character.toString(temp.charAt(n))), 4);

It seems like this would be so much easier in other languages. I think C++ would just be
sum = pow(temp[n] - '0', 4);

Java sucks, but maybe not as much as you may think. :) You can do basically the same thing in Java too, as:

import static java.lang.Math.*;// ...sum += pow(temp.charAt(n) - '0', 4);


And the loop could be made more readable (though not necessarily more efficient) with:

import static java.lang.Math.*;// ...for (char c : temp.toCharArray()) {    sum += pow(c - '0', 4);}


Phresnel does have a point, though.
phresnel
phresnel
Tbh, I am even slightly surprised that the mods did not see this string abuse (sorry, mods).

If you have an integer, then the most basic way to get all digits is as simple as

#include <iostream>void dissect (int value, int base) {        std::cout << value << " in digits of base " << base << " (low to hi): ";                const bool negative = value < 0;        if (negative) value = -value;    // strip sign                for (; value!=0; value /= base) {                const int valmod = value % base;                                std::cout << '[' << valmod << ']';        }        std::cout << '\n';}int main () {        int x = -123789;        dissect (x, 10);}



--> no memory waste
--> less heat waste
--> way, way, way better performance
--> no memory waste
--> works for all bases
--> no memory waste and way, way, way better performance and less heat waste
--> additional plus for C++: base can be made a compile time constant so that not even a modulus is harmed

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.