how to convert a list of numbers in a string to int values - java?

2008-12-11 3:07 am
Given a list of single-digit numbers between 0 and 9, what would be the best way to get the next #. For example, if I have a list of numbers

2, 3, 6, 1, 7, 8, 1, 0, 2, 4

how would I find each number?

I'm currently using the charAt() method, and was just wondering if there's an easier way to do this other than StringTokenizer.
更新1:

The numbers will be separated by commas, or at least something of one character length. So they won't be separated by 2 spaces or 3 commas, etc.

回答 (2)

2008-12-11 3:21 am
✔ 最佳答案
If you're doing it in Java 1.4 or later, I'd suggest you use the split() method available in the String class. Then convert all the delimited portions, which will be in an array of String, with the Integer's parseInt() method. No matter how you're going to implement it, you'll have to decide the delimiter first. i.e. will the numbers be separated by whitespaces or commas?
2016-11-15 2:44 pm
swap(string){ case "n": do something; wreck; case "open": do something; wreck; case "assault": do something; wreck; case "inventory": do something; wreck; default: //blah }


收錄日期: 2021-05-01 01:00:20
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20081210190733AAQHiRt

檢視 Wayback Machine 備份