简体   繁体   中英

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

This is for an online judge (Codeforces). Input file is like this

input.txt

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 :

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 .

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

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.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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