I'm am reasonably new at coding and am currently studying it at College. While in my break I found Project Euler a neat little site with problems which I have been working through for an extra challenge. This is the question that I am stuck on: http://projecteuler.net/index.php?section=problems&id=8 basically you are given a 1000 digit number and you have to find the greatest product of five consecutive digits in the 1000-digit number.

This here is where I am up to currently:

import java.util.*;
import comp100.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.*;

public class F {
    String number;
    int one = -1;
    int two = -1;
    int three = -1;
    int four = -1;
    int five = -1;
    int product = -1;
    int temp = -1;

    /**
     * Constructor for objects of class F
     */
    public F()
    {
        number = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";

    }
    
    //Reads the numbers and outputs the answer
    private void f() {
        Scanner sc = new Scanner(number);

        while(sc.hasNext()){
            five = four;
            four = three;
            three = two;
            two = one;
            one = sc.nextInteger();

            if(five != -1){
                temp = one*two*three*four*five;

                if(temp > product)
                    product = temp;
            }
        }
        
        System.out.println(product);
    }

    //main run method
    public static void main(String[] args) {
        F f = new F();
        f.f();

    }

}

What I wanted to know is how do I get the scanner class to only read a single digit each time rather than trying to read the whole 1000 digit number at once. Which causes it to throw an error as it's too big to fit into an int.

So is there a way to get the scanner to read a single digit of the 1000 digit number or should I look at a different way to approach this?

Edit: And does anyone know any other sites similar to this which provide some extra coding practise for fun?

Edited 5 Years Ago by FlightOfGrey: n/a

Since you have it as a String, you could get chars as needed with charAt() and convert them to an int with Integer.parseInt. Or you could conceiveably use the toCharArray method to turn it into an array of chars, and walk along that. That's probably a bit faster.
I don't think Scanner is your best bet for this - the String methods are probably better suited to the task.

Since you have it as a String, you could get chars as needed with charAt() and convert them to an int with Integer.parseInt. Or you could conceiveably use the toCharArray method to turn it into an array of chars, and walk along that. That's probably a bit faster.
I don't think Scanner is your best bet for this - the String methods are probably better suited to the task.

Silly I know but I always seem to forget that String and Integer etc have their own methods. Thanks for your help, with my college assignments we have tended to use scanner so that's what I naturally went for when reading data.

Thank you again.

That's part of the point of college assignments - to make sure you use those methods a lot, so you don't forget them later.

This question has already been answered. Start a new discussion instead.