简体   繁体   English

Java文本文件输入

[英]Java text file input

i am currently doing a small task in java which i am very new to so please excuse any silly mistakes i have made. 我目前正在用Java做一个很小的任务,这对我来说是新手,所以请原谅我犯的任何愚蠢错误。 Basically i am trying to take 2 values from a text document, import them into my java document and then multiply them together. 基本上我想从文本文档中获取2个值,将它们导入到我的Java文档中,然后将它们相乘。 These 2 numbers are meant to represent the hourly pay and amount of hours worked, then the output is the total amount the member of staff has earned. 这两个数字代表小时工资和工作小时数,然后输出是工作人员已赚取的总金额。 This what i have so far ... 这是我到目前为止所拥有的...

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

What i have so far is basically me trying to get the .txt document into my java file. 到目前为止,基本上我只是想将.txt文档放入我的Java文件中。 I'm not sure if this is right and then i'm not sure where to go to get the values to multiply and have it outputted. 我不确定这是否正确,然后不确定要乘以哪个值并将其输出。 I understand what i have so far is probably just the very start of what i need but any help will be massively appreciated as i am keen to learn. 我知道到目前为止我所拥有的可能仅仅是我所需要的一开始,但是当我热衷于学习时,任何帮助都会得到极大的赞赏。 Thanks so much .... Hannah 非常感谢....汉娜

I don't know what Amount earned is. 我不知道Amount earned So my guess is you need to change the last line to 所以我的猜测是您需要将最后一行更改为

double amountEarned = Hours * Pay; //this multiplies the values
System.out.println(amountEarned);  //this outputs the value to the console

EDIT: Putting code inside a main method: 编辑:将代码放入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);
    }
}
// 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);

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM