你如何在java中的文件中拆分数字?

时间:2013-10-09 04:19:40

标签: java variables file-io java.util.scanner

如果你有一个文件,第一行是数据集的数量,剩下的就是你必须乘以和输出的两个整数,你会怎么做?

The file is
    9
    10 2 
    11 1
    8  2
    7  9
    -1 10
    3  9
    6  3
    18 2
    7  3 
And the output is supposed to be
    20 
    11
    16
    63
    -10
    27
    18
    36
    21

(注意这是在java中,如果输入中的第一个数字发生了变化,输出的数量会随之改变,并且这些数字应该可以与其他数字互换)

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Basic_Attempt {

    public static void main(String[] args) {

        // Location of file to read
        File file = new File("data.txt");

        try {

            Scanner scanner = new Scanner(file);

            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                System.out.println(line);
            }
            scanner.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

我可能使用太多代码甚至只是模仿文件,但我需要知道如何将扫描仪输入实际拆分为数组,以及如何检查每行中的内容。有没有办法在java中做到这一点?

2 个答案:

答案 0 :(得分:1)

检查以下代码:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

class Test{

    public static void main(String args[]){
        Integer[] test = getData("file1.jt");
        for(Integer t: test){
            System.out.println(t);
        }
    }

    public static Integer[] getData(String pathtofile){
        BufferedReader br = null;
        Integer[] ret = null;
        try {
            String sCurrentLine;
            Integer totNumber;
            br = new BufferedReader(new FileReader(pathtofile));
            totNumber = Integer.parseInt(br.readLine());

            if(totNumber > 0){
                ret = new Integer[totNumber];
                for(int i=0;i<totNumber;i++){
                    if(((sCurrentLine = br.readLine()) != null)){
                        String[] pieces = sCurrentLine.split(" ");
                        if(pieces.length==2){
                            ret[i] = Integer.parseInt(pieces[0]) * Integer.parseInt(pieces[1]);
                        }
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NumberFormatException e){
            e.printStackTrace();
        } finally {
            try {
                if (br != null) br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return ret;
    }
}

答案 1 :(得分:1)

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class hello {

    public static void main(String[] args) {

        // Location of file to read
        File file = new File("C:\\Users\\k\\Desktop\\新建文本文档.txt");

        try {

            Scanner scanner = new Scanner(file);

            int lineNum = scanner.nextInt();
            scanner.nextLine();
            while (lineNum > 0) {
                String[] ss = new String[2];
                String line = scanner.nextLine();
                lineNum --;
                ss = line.split("\t");

                int first = Integer.parseInt(ss[0]);
                int second = Integer.parseInt(ss[1]);
                int result = first * second;
                System.out.println(String.valueOf(result));
            }
            scanner.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}