1

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.

  • 4
    makes no sense. if the name isn't there the first time it won't be the second time. do you mean to allow the user to enter a different name, then re-search the file? – KyleM Apr 25 '13 at 19:35
  • `inFile` is instance of what class? – Smit Apr 25 '13 at 19:46

3 Answers3

0

Not sure of the types of many of your classes here- are they input streams etc? But suggest take a look at this SO question:

java file input with rewind()/reset() capability

The filechannel.position(0) could be what you are looking for, or the RandomAccessFile rewind method, or inputstreamreader.mark

Which is better depends if you want to use streams, or don't mind either way, whether you mind the file being buffered in memory, etc.

Incidentally, if your file size is "small" (whatever that means these days) you could consider just loading the whole file into memory. Or just read the file afresh every time?

Community
  • 1
  • 1
GrahamMc
  • 3,034
  • 2
  • 24
  • 29
0

in case you are using Scanner, try the findWithinHorizon() method instead of next(). It looks for a specified pattern and does not advance the pointer on failure. And if you are not using Scanner - use it;) See javadocs

makasprzak
  • 5,082
  • 3
  • 30
  • 49
0

Why search multiple times? Why not just store all of the information in a map and look it up? Here's some key parts (NOT full working code):

HashMap<String,Integer[]> things = new HashMap<String,Integer[]>();

// Read each line and add to HashMap using:
Name = productName;
nums[0] = inFile.nextInt();
nums[1] = inFile.nextInt();
things.put(Name,nums);

// When you're done, ask the user what they're looking for and look it up.
productComp = console.next();
Integer[] nums = things.get(productComp);
if(nums == null){ // or try/catch for NullPointerException
   // Doesn't exist! Try again!
}else{
      productAmount = nums[0];
      productPrice = nums[1];
      sum += calculateSum(productAmount, productPrice, productComp);
      check = true;
}
David K
  • 1,296
  • 18
  • 39
  • Alright, your answer is probably the best but im concerned since when i read the files in there will be two integer numbers instead of 1. will things.put(Name,nums); allow me to do things.put(name, nums, nums); ? or is that out of hashMap's scope. ive never used that before so i would like to know please. – user2321247 Apr 25 '13 at 20:36
  • @user2321247: It can only do a single object. For some reason the code disappeared, but nums should be an Integer[] array. Check Java's doc page - http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html – David K Apr 25 '13 at 20:40
  • Thanks! it seems to work all well and good, but im getting a misplaced constructor error on the line that declares the hashmap object. do you have any idea what that could be? – user2321247 Apr 25 '13 at 20:57
  • @user2321247: I think it's because I forgot the parentheses at the end (updated above) – David K Apr 25 '13 at 21:04