String to Integer Java question?
-
I have to take a string, like "2112211241" or something and convert it into an integer array, but have no idea how. I wanted to do for(int j = 0; j < i; j++) { for(int q = 0; q < 10; q++) { char temp = studentAnswers[j].charAt(q); studentIntAnswers[j][q] = Integer.parseInt(temp); // earlier defined integer array. } } But there isn't a method for parseInt(char). Is there an easier way to do that? Thanks!
-
Answer:
You're right: there isn't a method to parse a char into an int but there is a method to parse a string into an in. So, just represent each char as a single char string. This is how you do it: String str = "2112211241"; int[] digit = new int[str.length()]; for( int k=0;k<str.length();k++ ) { ...digit[k] = Integer.parseInt( str.substring(k,k+1) ); } Don't forget to give me my "best answer" rating!!!
Hayden at Yahoo! Answers Visit the source
Other answers
keep it a string, basically, char temp = charAt part should be int temp = Integer.parseInt(studentAnswers[j].subst… EDIT: don't know why it's making it say ... but substring(q,q+1));
David
Related Q & A:
- Is Integer Factorization Harder Than RSA Factorization?Best solution by Theoretical Computer Science
- How to Implement Gateway Service something similar to Oracle API gateway Using Java and Java based Open Source frameworks only?Best solution by Quora
- How to see if one string contains another string?Best solution by Stack Overflow
- Difference between Java 2 and Java 6?Best solution by Yahoo! Answers
- How to search for a particular string in a text file using java?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.