Java文本文件输入

Java文本文件输入,java,input,Java,Input,我目前正在用java做一个小任务,我对它很陌生,所以请原谅我犯的任何愚蠢的错误。基本上,我试图从一个文本文档中获取2个值,将它们导入到我的java文档中,然后将它们相乘。这两个数字表示时薪和工作小时数,那么输出就是员工的总收入。这就是我目前所拥有的 import java.util.*; import java.io.*; public class WorkProject { Scanner inFile = new Scanner(new FileReader("staff

我目前正在用java做一个小任务,我对它很陌生,所以请原谅我犯的任何愚蠢的错误。基本上,我试图从一个文本文档中获取2个值,将它们导入到我的java文档中,然后将它们相乘。这两个数字表示时薪和工作小时数,那么输出就是员工的总收入。这就是我目前所拥有的

import java.util.*;
import java.io.*;  

public class WorkProject 
{ 

    Scanner inFile = new Scanner(new FileReader("staffnumbers.txt"));

    double Hours;
    double Pay;

    Hours = inFile.nextDouble();
    Pay = inFile.nextDouble();
    double earned = Length * Width;

            System.out.println(earned);
    }

到目前为止,我基本上是在尝试将.txt文档放入java文件中。我不确定这是否正确,然后我也不确定去哪里获取要乘法的值并将其输出。我明白到目前为止我所拥有的可能只是我所需要的一切的开始,但任何帮助都将得到极大的感谢,因为我渴望学习。非常感谢。。。。汉娜

我不知道赚了多少钱。我猜你需要把最后一行改成

double amountEarned = Hours * Pay; //this multiplies the values
System.out.println(amountEarned);  //this outputs the value to the console
编辑: 将代码放入
main
方法中:

public class WorkProject {
    public static void main(String[] args) throws FileNotFoundException {

      Scanner inFile = new Scanner(new FileReader("C:\\staffnumbers.txt"));

      double Hours;
      double Pay;

      Hours = inFile.nextDouble();
      Pay = inFile.nextDouble();
      double amountEarned = Hours * Pay;

      System.out.println(amountEarned);
    }
}

我不确定这是否正确
好吧,你可以检查一下,只需写
System.out.println(挣来的)
看看你做得是否正确。你已经得到了要乘以的值。它们存储在称为小时和工资的变量中。但最后一行是不正确的。您需要声明一个名为
Amount
的double,并将其设置为
Hours*Pay
。顺便说一下,变量名中不允许有空格,通常使用camelCase命名变量。驼峰大小写表示每个变量以小写字母开头,如果它有多个单词,则从第二个单词开始,然后以大写字母开头。示例:如果您想要一个变量来存储赚得的金额,则该变量的名称应为
amounterned
。祝你学习编程时好运您只需键入double Earn=Hours*Pay,您没有声明长度和宽度。非常感谢您的回复,我已将结尾更改为。。双倍收入=工资*小时;系统输出打印项次(挣得);这是否应该给我输出,因为现在我在“双倍支付”之后有一个错误说它期待着一个{挣得的金额意味着未经学习是的,对不起,第一位看起来对从文本文件读取正确吗?@HannahLivestrong是的,它是正确的,尽管您必须处理一个可能的FileNotFoundException…您确定吗?看起来Javadoc说,
扫描仪
文件
作为参数,而不是
FileReader
..@coderofhoron
FileReader
Reader
的一个子类,它的
Scanner
也可以作为构造函数参数。哈,我读错了吗?它看起来像是说它需要一个
可读的
。FileReader实现了可读吗?
// Matt Stillwell
// April 12th 2016
// File must be placed in root of the project folder for this example 

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

public class Input
{

    public static void main(String[] args)
    {

        // declarations
        Scanner ifsInput;
        String sFile;

        // initializations
        ifsInput = null;
        sFile = "";

        // attempts to create scanner for file
        try
        {
            ifsInput = new Scanner(new File("document.txt"));
        }
        catch(FileNotFoundException e)
        {
            System.out.println("File Doesnt Exist");
            return;
        }

        // goes line by line and concatenates the elements of the file into a string
        while(ifsInput.hasNextLine())
            sFile = sFile + ifsInput.nextLine() + "\n";     

        // prints to console
        System.out.println(sFile);

    }
}