Reputation: 23
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
Upvotes: 2
Views: 55939
Reputation: 132
// 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);
}
}
Upvotes: 0
Reputation: 7383
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);
}
}
Upvotes: 3