Java文件输入 [英] Java file input

查看:75
本文介绍了Java文件输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在我的新展示位置中完成一项任务.基本上,我需要做的是从文件中获取数字并将其相乘.我的文件内容如下

I am currently working through a task in my new placement. Basically what I need to do is get numbers from a file and multiply them. My file read as follows

Dave

7 35

Lucy 

6 19

我需要获取每个人的数字并将它们相乘,以便以后可以使用.到目前为止,我已经有了这段代码,我敢肯定有一种更简单的方法,因为这似乎很耗时.打印到屏幕上只是为了测试它是否有效.

I need to get the numbers for each person and multiply them so I can use them later on, I have this code so far, I'm sure there is a simpler way as this seems very long winded. The print to screen is there simply to test if it works.

import java.util.*; 
import java.io.*; 
import javax.swing.JOptionPane;
public class Task1 
{
  public static void main(String[] args) throws FileNotFoundException
  {
    Scanner inFile = new Scanner(new FileReader("ExternalData.txt")); 
    String name; 
    double rate; 
    double hours; 
    name = inFile.next (); 
    rate = inFile.nextDouble(); 
    hours = inFile.nextDouble(); 
    double weeklypay1 = rate * hours;
    String name2;
    double rate2;
    double hours2;
    name2 = inFile.next (); 
    rate2 = inFile.nextDouble(); 
    hours2 = inFile.nextDouble();
    double weeklypay2 = rate * hours;

    System.out.println("Daves pay:" + weeklypay2);

    }
}

我的问题是,这基本上是一种布局的好方法,还是我完全走错了路,这对Java很新,所以请您多多帮助,谢谢!

My question is basically does this look like a good way to lay it out or am I going completely the wrong way about it, very new to java so would appreciate any help, thanks in advance!

推荐答案

以下是清理此问题的快速尝试:

Here is a quick attempt at cleaning this up:

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Task1 {
  @SuppressWarnings("resource")
  public static void main(String[] args) throws FileNotFoundException {
    Scanner inFile = new Scanner(new FileReader("ExternalData.txt"));
    List<Employee> employees = new ArrayList<>();
    while(inFile.hasNext()) {
      employees.add(new Employee(inFile.next(), inFile.nextDouble(), inFile.nextDouble()));
    }
    for(Employee employee : employees) {
      System.out.println(employee);
    }
  }
}

使用此类(用于保存员工数据并干净地打印它):

With this class (to hold employee data and to cleanly print it):

public class Employee {
  public final String name;
  public final double rate;
  public final double hours;
  public final double weeklypay;

  public Employee(String name, double rate, double hours) {
    this.name = name;
    this.rate = rate;
    this.hours = hours;
    this.weeklypay = this.rate*this.hours;
  }

  public String toString() {
    return name+"'s pay:" + weeklypay;
  }
}

与您的文本文件一起产生以下输出:

With your text file it produces this output:

Dave's pay:245.0
Lucy's pay:114.0

或者您可以将它们存储在 Map (以名称为键)中,然后执行以下操作:

Or you can store them in a Map (with the name as the key) and do stuff like this:

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Task1 {
  @SuppressWarnings("resource")
  public static void main(String[] args) throws FileNotFoundException {
    Scanner inFile = new Scanner(new FileReader("/ExternalData.txt"));
    Map<String, Employee> employees = new HashMap<>();
    while(inFile.hasNext()) {
      Employee employee = new Employee(inFile.next(), inFile.nextDouble(), inFile.nextDouble()); 
      employees.put(employee.name, employee);
    }
    System.out.println(employees.get("Dave"));
  }
}

这篇关于Java文件输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆