2014-02-18 27 views
0

我已经和Java一起工作了几年,但在那段时间里,我几乎从不需要对文本文件做任何事情。我需要知道如何将文本文件的行作为两位整数读入不同的变量,以及将几行文本文件读入二维整数数组中。每一个文本文件是这样写:Java - 将文件行读入不同的变量

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 

前三行应该都是独立的整数,但第一行表示二维数组的尺寸。最后一个段需要进入该整数数组。这是我迄今为止在代码方面所做的。

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()){ 

     } 
    } 
} 

我完全处于如何做到这一点的损失。

+0

如果你已经使用Java进行了几年,你应该熟悉的Javadoc:docs.oracle.com/javase/7/docs/api/java/util/Scanner.html,知道有一个官方的Java教程解释几乎所有的Java API:http://docs.oracle.com/javase/tutorial/essential/io/charstreams.html –

+0

为什么不写下你将作为一个人试图完成这个步骤任务。我会以第一名开始你:阅读第一行。分开这两个整数。第一个是数字行,第二个是列数。第二排和第三排应该代表什么? – thatidiotguy

+0

第二行和第三行表示数组内的开始和结束位置。它应该代表一张地图。 – user3308219

回答

0

下面是一些psuedoish代码

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

一旦你的数据(或者在阅读它),你可以把它分解和处理它,你认为合适。

0

改为从整数数组的ArrayList开始。它会更容易做到这一点:

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

显然,做一个循环内的大部分。

0

这是一个完整的版本。

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(); 
      } 
     } 
    } 
} 
+0

即使文本文件和Asst1Main文件应位于同一目录中,它仍会抛出异常。它将small.txt放在默认包之外,所以我不知道这是否与这种情况有关。 – user3308219

+0

small.txt文件应位于工作目录中,该目录是您运行类的目录。使用'System.out.println(System.getProperty(“user.dir”))'来打印工作目录。 – tom

+0

完美,现在它可以工作。我认为.txt文件都必须在Eclipse的src下,但似乎需要将它们存储在Java项目根文件夹下。无论如何,感谢您的帮助。 – user3308219

相关问题