Reputation: 5
I am writing some code to read static stock numbers from a text pad. I ran it to see what was wrong and I cannot seem to fix this cannot find symbol error.
import java.io.*;
import java.util.*;
public class StockMarket
{
public static void main(String[] args) throws IOException
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the filename: ");
String filename = keyboard.nextLine();
File file = new File(filename);
Scanner inputFile = new Scanner(file);
while (inputFile.hasNext())
{
checkValidity();
}
inputFile.close();
}
public static boolean checkValidity()
{
String line = inputFile.nextLine();
double numberOfShares = inputFile.nextDouble();
double buyPricePerShare = inputFile.nextDouble();
double buyComissionRate = inputFile.nextDouble();
double sellPricePerShare = inputFile.nextDouble();
double sellComissionRate = inputFile.nextDouble();
}
}
Upvotes: 0
Views: 1741
Reputation: 22442
You have got few issues with your checkValidity()
method as explained below:
(1) Your Scanner
object scope exists only inside main
method, so pass that object to checkValidity
method
(2) Validate the inputs inside checkValidity
method
(3) Your checkValidity
method has got return
type of boolean
, so you need to return
a boolean
value from it i.e., if validation is successful, return true
or if validation fails, return false
You refer the below code with inline comments:
public static boolean checkValidity(Scanner inputFile) {
String line = inputFile.nextLine();
double numberOfShares = inputFile.nextDouble();
double buyPricePerShare = inputFile.nextDouble();
double buyComissionRate = inputFile.nextDouble();
double sellPricePerShare = inputFile.nextDouble();
double sellComissionRate = inputFile.nextDouble();
boolean validationSuccess = false;
//validate your inputs
// If validations are successful then set validationSuccess = true;
return validationSuccess;//return boolean
}
Also, pass the scanner
object from main()
as shown below:
while (inputFile.hasNext()) {
checkValidity(inputFile);//pass scanner object
}
Upvotes: 2
Reputation: 1171
Your method checkValidity
cannot work. You try to access to variable inputFile
which is not known in the method. You have to pass is as parameter.
public static boolean checkValidity(Scanner inputFile)
{
String line = inputFile.nextLine();
double numberOfShares = inputFile.nextDouble();
double buyPricePerShare = inputFile.nextDouble();
double buyComissionRate = inputFile.nextDouble();
double sellPricePerShare = inputFile.nextDouble();
double sellComissionRate = inputFile.nextDouble();
}
Upvotes: 0