简体   繁体   中英

Java - Read lines of file into different variables

I've worked with Java for a few years but during that time I've almost never had to do anything with text files. I need to know how to read lines of a text file into different variables as two-digit integers, along with several lines of said text file into a 2D integer array. Every text file is to be written like this:

5 5
1 2
4 3
2 4 2 1 4
0 1 2 3 5
2 0 4 4 1
2 5 5 3 2
4 3 3 2 1

The first three lines should all be separate integers, but the first line is indicative of the 2D array's dimensions. The last segment needs to go into that integer array. This is what I've got so far in terms of code.

import java.util.*;
import java.io.*;
public class Asst1Main {
    public static void main(String[]args){
        try {
            x = new Scanner(new File("small.txt"));
        } catch (FileNotFoundException e) {
            System.out.println("File not found.");
        }
        while(x.hasNext()){

        }
    }
}

I'm completely at a loss of how to do this.

Here is some psuedoish code

Scanner input = new Scanner(new File("blammo.txt"));

List<String> data = new ArrayList<String>();
String line1;
String line2;
String line3;

line1 = readALine(input);
line2 = readALine(input);
line3 = readALine(input);

... process the lines as you see fit.  perhaps String.split(line1);

while (input.hasNextLine())
{
    String current = input.nextLine();
    data.add(current);
}

private String readALine(final Scanner input)
{
    String returnValue;

    if (input.hasNextLine())
    {
        returnValue = input.nextLine();
    }
    else
    {
        returnValue = null; // maybe throw an exception instead.
    }

    return returnValue;
}

Once you have the data (or perhaps while reading it), you can split it and process it as you see fit.

Start with an ArrayList of integer arrays instead. It'll be easier to do this:

ArrayList<Integer[]> list = new ArrayList<Integer>();

String line = scanner.nextLine();
String[] parts = line.split("[\\s]");
Integer[] pArray = new Integer[parts.length];
for (Integer x = 0; x < parts.length; x++) {
    pArray[x] = Integer.parseInt(parts[x]);
}
list.add(pArray);

Do the bulk of that inside of a loop obviously.

Here is a full version.

import java.util.*;
import java.io.*;
public class Asst1Main {
    public static void main(String[] args) {
        Scanner in;
        try {
            in = new Scanner(new File("small.txt"));
        } catch (FileNotFoundException e) {
            System.out.println("File not found.");
            return;
        }
        int rows = in.nextInt();
        int cols = in.nextInt();
        int startRow = in.nextInt();
        int startCol = in.nextInt();
        int endRow = in.nextInt();
        int endCol = in.nextInt();
        int[][] map = new int[rows][cols];
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                map[row][col] = in.nextInt();
            }
        }
    }
}

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