2

i am currently doing a small task in java which i am very new to so please excuse any silly mistakes i have made. Basically i am trying to take 2 values from a text document, import them into my java document and then multiply them together. 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. 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

17
  • I'm not sure if this is right well, you can check, simply write System.out.println(earned) to see if you've done it right. Commented Oct 28, 2013 at 18:01
  • You've already gotten the values to multiply. They are stored in the variables called Hours and Pay. The last line though, is not correct. You need to declare a double called Amount and set it equal to Hours * Pay. Commented Oct 28, 2013 at 18:02
  • By the way, there are no spaces allowed in variable names, and usually variables are named using camelCase. Camel case means every variable starts with a lower case letter and if it has more than one word, from the 2nd word and onward you start with a capital letter. Example: if you wanted a variable to store the amount earned, the name of the variable would be amountEarned. Best of luck to you learning how to program. ;) Commented Oct 28, 2013 at 18:04
  • you can just type double earned = Hours * Pay, you didn´t declare Length and Width. Commented Oct 28, 2013 at 18:07
  • Thank you all so much for you replies, i have changed the end to.. double earned = Pay * Hours; System.out.println(earned); Should this give me output because now i have an error after the ''double pay;'' saying it's expecting a { Commented Oct 28, 2013 at 18:22

2 Answers 2

3

I don't know what Amount earned is. 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:

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);
    }
}
Sign up to request clarification or add additional context in comments.

12 Comments

Amount earned is meant to be amountEarned yes sorry, does the first bit look correct for reading from a text file?
@HannahLivestrong Yes it is correct, although you will have to handle a possible FileNotFoundException...
Are you sure? It looks like the Javadoc says that Scanner takes File as a parameter, not FileReader...
@CoderOfHonor FileReader is a subclass of Reader, which Scanner can also take as a constructor parameter. docs.oracle.com/javase/7/docs/api/java/util/Scanner.html
Huh, am I reading this wrong? It looks like docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html says that it takes a Readable. Does FileReader implement Readable?
|
0
// 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);

    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.