I've looked all over for an answer to the above question. I have been building a calculator that will take a user input of a name of a product in a text file and the program will search through the document for the name the product and once its found it it will record the integers next to the name and use that to calculate the price. While I have gotten all of that to work my main problem is making the program run through the document multiple times if it doesn't find the name the first time. as it is now It will run through the entire document and end once it reaches the end of the file.
here is the text file
Wool 15 1
Seeds 3 1
Feathers 3 1
String 2 1
Beef 2 1
Fish 2 1
Bone 1 1
Iron 1 1
GunPowder 1 1
Glowstone 1 7
InkSack 1 2
Flesh 2 1
Eggs 2 1
And here is my code
public static void main(String[]args)throws FileNotFoundException{
//Declare Variables
String productName, productComp;
int productAmount, productPrice,itemNum, sum = 0;
boolean check = false;
Scanner console = new Scanner(System.in);
Scanner inFile = new Scanner(new FileReader("SelllingPrices.txt"));
System.out.println("Enter the number of items");
for(itemNum = console.nextInt(); itemNum > 0; itemNum--){
System.out.println("Enter the name of the item");
productComp = console.next();
while(check != true){
productName = inFile.next();
System.out.println(productName);
if(productName.compareTo(productComp) == 0){
productAmount = inFile.nextInt();
productPrice = inFile.nextInt();
sum += calculateSum(productAmount, productPrice, productComp);
check = true;
}
else{
check = false;
}
}
check = false;
}
calculateTotalPayout(sum);
}
public static int calculateSum(int productAmount, int productPrice, String productName){
int totalAmount, divisor, sum = 0;
Scanner console = new Scanner(System.in);
System.out.println("Enter the amount of " + productName + " recieved");
totalAmount = console.nextInt();
if(totalAmount > productAmount){
divisor = totalAmount / productAmount;
sum = productPrice * divisor;
return sum;
}
else{
sum = productPrice;
return sum;
}
}
public static void calculateTotalPayout(int sum){ int temp = 0, counter = 0;
if(sum > 64){
temp = sum;
while(temp >= 64){
temp -= 64;
counter++;
}
}
System.out.println("The total comes to " + sum + " emeralds or " + counter + " stacks of emeralds and " + temp + " emeralds left over");
} }
i just need to know what to fix in order to make it so that once it reaches the end of the file, if it hasn't found the name to go back to the beginning of the file and look again.
Thanks for any help you can offer. I hope I was clear with my question.
Some clarification. Yes I want the user to be able to put in a different name and re-search the file and I've added the entirety of the code to let people know whats going on.