为什么我的文字值会缩回-460?

时间:2017-03-19 07:23:10

标签: java string int

这是我上一篇文章的续篇,但是主题不同。我不知道为什么我的程序读取文件并返回我的错误值-460。我的文本文件中有100个int,其中99个返回为-460,文件中的最后一个数字被正确读取。我不知道发生了什么!请帮忙!谢谢!

import java.io.*;
import java.util.*;
public class projectFour
{
    public static int [] global_numbers;
    public static void main (String[] args)
    {
        read_file();
        print_numbers(global_numbers);
    }
    public static void read_file()
    {
        try
        {
            File file = new File("randomNumbers.txt"); 
            Scanner scan = new Scanner(file);
            int amountOfNumbersInFile = convertingStringToInt(scan.nextLine()); // read the first line which is 100 to set array size
            global_numbers = new int[amountOfNumbersInFile]; // set the array size equal to the first line read which is 100
            for (int index = 0; index < amountOfNumbersInFile; index++) 
            {
            String line = scan.nextLine(); 
            global_numbers [index] = convertingStringToInt(line); 
            }      
        } catch (IOException ex) {
           ex.printStackTrace();
        }
    }
    public static int convertingStringToInt(String numbers) //what does string "number" equal? why/where is it declared? where is its value coming from?!?
    {  
        try {
            return Integer.parseInt(numbers);
        } catch(NumberFormatException n) {
            return -460;
        }         
    }
    public static void print_numbers(int [] numbers) // passing in an array called numbers but how does this array  have values associated  to it!?!
    {
      int max = numbers.length;
      for(int i = 0; i < max; i++)
            System.out.println(numbers[i]); 
    }
}

1 个答案:

答案 0 :(得分:0)

.reportuserinfo label {
    float: left;;
  margin-right: 30px;
  font-weight: bold;
  text-align: right;
  width:30%;
}