简体   繁体   English

如何从Java中的文本文件获取数组大小和元素的输入?

[英]How to take array size and elements' inputs from a text file in java?

This is for an online judge (Codeforces). 这是给在线法官(Codeforces)的。 Input file is like this 输入文件是这样的

input.txt input.txt

6 6

4 3 2 1 5 6 4 3 2 1 5 6

First line is the array size and second line contains the array elements. 第一行是数组大小,第二行是数组元素。

I've tried it by using this 我已经尝试过使用它

 public static int readFiles(String file){
        try{

        File f = new File(file);
        Scanner Sc = new Scanner(f);
        int n = Sc.nextInt();
        return n;
    }
    catch(Exception e){
        return 0;
    }
}
public static int[] readFiles(String file,int n){
    try{

        File f = new File(file);
        Scanner Sc = new Scanner(f);
        Sc.nextInt();
        int arr [] = new int [n];
        for(int count = 0;count < n; count++){
            arr[count] = Sc.nextInt();
        }
        return arr;
    }
    catch(Exception e){
        return null;
    }
}

public static void main (String args [] ){
        int n = readFiles("input.txt");
        int arr [] = readFiles("input.txt",n);

You could call the method that builds the array without provide n : 您可以调用不提供n构建数组的方法:

public static void main (String args [] ){
    int arr [] = readFiles("input.txt");
    System.out.println(Arrays.toString(arr));
    int n = arr.length;
    System.out.println("n is: " + n);
}

public static int[] readFiles(String file){
    try{
        File f = new File(file);
        Scanner Sc = new Scanner(f);
        int n= Sc.nextInt();
        int arr [] = new int [n];
        for(int count = 0;count < n; count++){
            arr[count] = Sc.nextInt();
        }
        return arr;
    }
    catch(Exception e){
        System.out.println("The exception is: " + e);
        e.printStackTrace();
        return null;
    }
}

if you need n , you can get it by this line arr.length . 如果需要n ,则可以通过arr.length这行arr.length

your code will never work because is not even compiling 您的代码将永远无法工作,因为甚至无法编译

if this method readFiles(String file) return an int then it makes no sense doing this 如果此方法readFiles(String file)返回一个int,则这样做没有任何意义

int arr [] = readFiles("input.txt",n);

hint: 暗示:

  1. read the file line by line, 逐行读取文件,
  2. check if spaces are there 检查是否有空间
  3. get the numbers as string 获取数字作为字符串
  4. parse those into integers 将它们解析为整数
  5. put them in the array 将它们放在数组中
  6. return at the end that array. 返回该数组的末尾。

您为什么不在for循环上尝试在数字之间将空格分开并将其存储在数组中:

int temp[] = Scanner.readLine().split(" ");

You could add a bufferedreader to read the lines one at a time, like this. 您可以添加一个bufferedreader来一次读取一行,就像这样。

public static void main(String[] args) {
    int[] numberArray = readFile();
}

public static int[] readFile(){
    try(BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
        int arraySize = Integer.parseInt(br.readLine());
        String[] arrayContent = br.readLine().split(" ");
        int[] newArray = new int[arraySize];

        for(int i = 0; i<arraySize; i++){
            newArray[i] = Integer.parseInt(arrayContent[i]);
        }
        return newArray;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM