0

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

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中做到这一点?

4

2 回答 2

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();
        }
    }
}
于 2013-10-09T05:09:26.613 回答
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;
    }
}
于 2013-10-09T04:48:36.677 回答