2012-07-21 38 views
2

我的代碼沒有給我正確的輸出。它從文本文件讀取數字,將它們轉換爲整數值並對它們執行一些乘法運算。但方法findProduct()給我的產品是錯誤的,所以我認爲我錯誤地使用了Integer.parseInt()方法。Integer.parseInt()問題

我猜是扔我了是,它是想告訴我的96086x67017 = 2144428166產品,當我的計算器檢查它是圍繞643939395462.

/* 
* 
* Find the greatest product of five consecutive digits in the 1000-digit number. 

73167176531330624919225119674426574742355349194934 
96983520312774506326239578318016984801869478851843 
85861560789112949495459501737958331952853208805511 
12540698747158523863050715693290963295227443043557 
66896648950445244523161731856403098711121722383113 
62229893423380308135336276614282806444486645238749 
30358907296290491560440772390713810515859307960866 
70172427121883998797908792274921901699720888093776 
65727333001053367881220235421809751254540594752243 
52584907711670556013604839586446706324415722155397 
53697817977846174064955149290862569321978468622482 
83972241375657056057490261407972968652414535100474 
82166370484403199890008895243450658541227588666881 
16427171479924442928230863465674813919123162824586 
17866458359124566529476545682848912883142607690042 
24219022671055626321111109370544217506941658960408 
07198403850962455444362981230987879927244284909188 
84580156166097919133875499200524063689912560717606 
05886116467109405077541002256983155200055935729725 
71636269561882670428252483600823257530420752963450 

this text file is in Project_8.txt 
*/ 


import java.io.*; 
// Import the scanner class that is used for reading the text file 
import java.util.*; 


public class Project_8 { 

// Data members================================ 
// Object used to read the text file 
private Scanner scanner; 



// Public Methods============================== 
// Points the scaaner object in the direction of the text file. Outputs an error if file is not found 
public void openFile(){ 

    try{ 
     // Note the whole file's path is required 
     scanner = new Scanner(new File("/Users/nicolematthews/Desktop/Tim/Java WORKSPACE/Project_Euler/src/Project_8.txt")); 

    }catch(Exception e){ 

     System.out.println("Could not find the File. Check file's address"); 

    } 
} 



public String readFile(){ 

    // Instantiating the scanner for user input ruins its application for reading files 
    //scanner = new Scanner(System.in); 

    // Method looks the text file and adds each line of the text to the String sequence. 
    // the Method returns sequence 

     String sequence = ""; 

     // While the scanner has more data to read 
     while(scanner.hasNext()){ 

      sequence = sequence + scanner.next(); 

     } 

    return sequence; 
} 





// This method simply closes the scanner object 
public void closeFile(){ 

    scanner.close(); 
} 









public int findProduct(String sequence){ 


    int highestProduct = 0; 

    for(int i = 0; i < (sequence.length() - 10); i++){ 

     // Extract adjacent 5 digit strings which will be converted to integers 
     String num1 = sequence.substring(i, i + 5); 

     String num2 = sequence.substring(i + 5, i + 10); 

     if(Integer.parseInt(num1)*Integer.parseInt(num2) > highestProduct){ 

      highestProduct = Integer.parseInt(num1)*Integer.parseInt(num2); 

      System.out.println(num1); 
      System.out.println(num2); 
      System.out.println(highestProduct); 
      System.out.println("\n"); 

     } 

    } 

    return highestProduct; 

} 



// Main Method ========================================= 
public static void main(String args[]) throws IOException{ 



    Project_8 object = new Project_8(); 

    object.openFile(); 

    String Sequence = object.readFile(); 

    System.out.println(Sequence); 


    System.out.printf("%d Is the highest product of five consecutive \n" + 
      "digits in the 1000-digit number.", object.findProduct(Sequence)); 



    // Finally, close the file when we are done with it. 
      object.closeFile(); 
    /* Find the greatest product of five consecutive digits in the 1000-digit number 


    System.out.println(Sequence); 

*/  





} 
} 
+2

你可以編輯你的問題來提供你給它的輸入,你實際得到的輸出和你預期的輸出的例子嗎? – 2012-07-21 16:32:15

回答

10

你的序列索引i僅由1每次迭代,i++推進,但你消耗在每次迭代字符。你已經表明這是你的意圖。

你會溢出一個java int許多從您的數據的5位子序列。例如,您的第二個產品將是31671 * 76531 = 2423813301。這比Integer.MAX_INT大:2^31 - 1

更改findProduct方法使用longLong.parseLong()將解決此問題。

+0

我的意圖是隻增加一個,並考慮所有可能的5位數字。但我喜歡增加更多異常處理 – 2012-07-21 16:43:56

+0

謝謝!我會投你一票,但我不能 – 2012-07-21 17:17:05

+0

@ user1542903也許你不能投票,但你可以[標記本文作爲答案](http://meta.stackexchange.com/a/5235/182862)。 – 2012-07-21 17:39:11

1

我認爲這個問題是造成代碼的數字提取器部分,都是長度恰好爲5個字符的數字?如果情況並非如此,那麼您的代碼將無法正常工作,因爲您的代碼期望它的長度爲5個字符。如果裏面有空格,Integer.parseInt()會投訴,除非你打電話給trim,你還沒有完成。

0

你的if語句做太多一下子:

if(Integer.parseInt(num1)*Integer.parseInt(num2) > highestProduct){ 

你應該打破這種由每個int值分配給一個獨立的變量。這樣你可以打印這些值,看看它們是否是正確的。此外,它將允許您在下一行中刪除對Integer.parseInt()的重複調用。