Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8269387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:12:33+00:00 2026-06-08T06:12:33+00:00

My code is not giving me the right output. It reads numbers from a

  • 0

My code is not giving me the right output. It reads numbers from a text file, converts them to an integer value and performs some multiplication on them. But the the products that method findProduct() gives me are wrong, so I think I am using the Integer.parseInt() method incorrectly.

I guess what is throwing me off is that It is trying to tell me the product of 96086×67017=2144428166, When checking on my calculator it is around 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);

*/      





}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T06:12:35+00:00Added an answer on June 8, 2026 at 6:12 am

    Your sequence index i is only advancing by 1 for each iteration, i++, but you consume 10 characters on each iteration. You’ve indicated that this is your intent.

    You’ll overflow a java int with many of the 5-digit sub-sequences from your data. For instance, your second product will be 31671 * 76531 = 2423813301. This is larget than Integer.MAX_INT: 2^31 – 1.

    Changing the findProduct method to use long and Long.parseLong() will fix this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

current code (not working): /^script\s*type=\text\/javascript/i.test(tagName)
I have this code in C++ which is giving weird output: #include<iostream> using namespace
i have looked through other related questions but none of them giving me right
Below code not work, but it's work fine for jsf1.2. Now the framework is
My Yii code not showing error summery before save movie in database.. what's the
my jquery code not run with IE6 but runs all others including IE7. It
Why is the following code not working? if(EventLog.Exists(Foo)) { EventLog.Delete(Foo); } if(EventLog.Exists(Foo) == false)
Why does this code not print an exception stack trace? public class Playground {
Why does the following code not allow foo(ptr) to be called ? #include <boost/scoped_ptr.hpp>
Why is my jQuery code not increasing the font size? <!DOCTYPE html PUBLIC -//W3C//DTD

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.