2013-04-25 47 views
1

我已经找遍了所有关于上述问题的答案。我一直在构建一个计算器,用户可以在文本文件中输入产品名称,程序将在文档中搜索产品的名称,一旦发现它将记录名称旁边的整数用它来计算价格。虽然我已经完成了所有这些工作,但如果第一次找不到名称,我的主要问题是使程序多次遍历文档。就像现在一样,它会遍历整个文档,一旦到达文件末尾就会结束。在java中多次搜索文本文档

这里是文本文件

羊毛15 1

种子3 1

羽毛3 1

字符串2 1

牛肉2 1

鱼2 1

骨1 1

铁1 1

火药11

Glowstone 1 7

InkSack 1 2

肉2 1

鸡蛋2 1

这里是我的代码

公共静态无效的主要(字串[] args)抛出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); 

}

公共静态INT calculateSum(INT productAmount,INT productPrice,弦乐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"); 

}}

我只需要知道怎么做才能让它修复,这样一旦到达文件的末尾,如果它没有找到名称回去文件的开始并再次查看。

感谢您提供任何帮助。我希望我的问题清楚。

一些说明。是的,我希望用户能够使用不同的名称并重新搜索文件,并添加了完整的代码以让人们知道正在发生的事情。

+4

是没有意义的。如果这个名字不是第一次出现,那不会是第二次。你的意思是允许用户输入不同的名称,然后重新搜索文件? – KyleM 2013-04-25 19:35:32

+0

'inFile'是什么类的实例? – Smit 2013-04-25 19:46:17

回答

0

为什么多次搜寻?为什么不把所有的信息存储在地图中并查找?这里有一些关键部分(不是全部工作代码):

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; 
} 
+0

好吧,你的回答可能是最好的,但因为当我读到这些文件中会有两个整数而不是1将things.put(名称,NUMS)进出口关注;允许我做things.put(name,nums,nums); ?或者超出了hashMap的范围。之前从未使用过,所以我想请知道。 – user2321247 2013-04-25 20:36:44

+0

@ user2321247:它只能执行一个对象。由于某些原因,代码消失了,但是num应该是一个Integer []数组。检查Java的文档页面 - http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html – 2013-04-25 20:40:31

+0

谢谢!它似乎工作得很好,但我得到一个错误的构造函数错误的线声明hashmap对象。你有什么想法可能是什么? – user2321247 2013-04-25 20:57:02

0

不知道的许多类型的类的这里 - 他们是输入流等?但是,建议看一看这太问题:

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

的filechannel.position(0)可能是你在找什么,或者RandomAccessFile的退法,或inputstreamreader.mark

哪个更好的取决于如果你想使用流,或者不介意无论哪种方式,你是否介意在内存中缓冲的文件等

顺便说一下,如果你的文件大小是“小”(这意味着什么,这些天)你可以考虑将整个文件加载到内存中。或者每次只重新阅读文件?

0

万一您正在使用扫描仪,请尝试 findWithinHorizo​​n()方法而不是下一个()。它会查找指定的模式,并且不会在失败时提前指针。如果你不使用扫描仪 - 用它;)请参阅Javadocs