日期:2014-05-20  浏览次数:20917 次

如何读入.txt的数字
.txt里有18   20
                12   32
如何把他们放倒数组里?谢谢!

------解决方案--------------------
不会 学习一下
------解决方案--------------------
public static int parseInt(String s)
throws NumberFormatException

Parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '- ' ( '\u002D ') to indicate a negative value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(java.lang.String, int) method.

Parameters:
s - a String containing the int representation to be parsed
Returns:
the integer value represented by the argument in decimal.
Throws:
NumberFormatException - if the string does not contain a parsable integer.

1. 可以先把这些字符读出来
2. 把读出来的字符进行分解,具体的运用最好查看JDK帮助文档
The following is one example of the use of the tokenizer. The code:

StringTokenizer st = new StringTokenizer( "this is a test ");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}


prints the following output:

this
is
a
test

StringTokenizer(String str)
Constructs a string tokenizer for the specified string.
StringTokenizer(String str, String delim)
Constructs a string tokenizer for the specified string.
StringTokenizer(String str, String delim, boolean returnDelims)
Constructs a string tokenizer for the specified string.

3. 把分解出来的字符转化为数字
int a = Integer.parseInt( "123 "); // 结果 a == 123

------解决方案--------------------
1.首先你要用一个字符流输入流对象类读取文本,按照你对于数字的储存方法,你可以行读
2.然后把每行读取的字符串对象进行分解.可以用split方法,当然也可以用楼上讲的StringTokenizer类来处理.
3.接着把分解的各个子字符串用相应的包装器类来解析
4.最后把解析得到的基本类型存储进数组

------解决方案--------------------
split
------解决方案--------------------
使用java.util.* 中的Scanner类。

File f=new File( "*.txt ");
Scanner in=new Scanner(f);
int[] a=new int[10];
i=0;

while(in.hasNextInt() && i <10)
{
a[i++]=in.nextInt();
}