-1

I seem to be having a problem with my code which is to look for the repeating sequence of digits. I have converted(?) double to string because I get the error unreachable statement. (which I guess helps to looking for the reason why I get the error I have now?).

Whenever I run it, it goes fine until I finish entering N and D.

It'll say "Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3"

Here is my code below:

import java.util.*; 
public class RepeatingSequence{
public static void main(String[] args){
    Scanner in = new Scanner(System.in);
    System.out.print("Enter N,D: ");
    int numerator = in.nextInt();
    int denominator = in.nextInt();

    double quotient = numerator / denominator;
    String number = "" + quotient;
    char n = number.charAt(0);
    int j = 1;
    int z = 0;
    String output = "";
    char[] index = number.toCharArray();

    for ( int i = 2; number.charAt(j) != number.charAt(i); i++ ){
        index[z] = number.charAt(z);
        index[j] = number.charAt(j);
        index[i] = number.charAt(i);
        output = output + index[i];

        if ( index[i] != index[z] ){
        System.out.print(index[z] + ".(" + index[j] + output + ")");
        }
    }   
}
}
Rod Galangco
  • 37
  • 1
  • 9

3 Answers3

1

just add i < number.length() to the condition

( int i = 2; i < number.length() && number.charAt(j) != number.charAt(i); i++ )
Sebri Zouhaier
  • 745
  • 7
  • 18
0

For your exception, I think you should write safer code - something on the lines of:

int len = number.length();

for ( int i = 2; (i < len) && (j < len) && 
          number.charAt(j) != number.charAt(i); i++ ){
 ... 
} 

I am not attempting to solve the problem that you are trying to solve but just the problem you are facing. Sorry for that.

Manoj Awasthi
  • 3,460
  • 2
  • 22
  • 26
0

I changed your code a little bit try it out and see what you think:

public static void main(String[] args){
    Scanner in = new Scanner(System.in);
    System.out.print("Enter N,D: ");
    double numerator = in.nextDouble();
    double denominator = in.nextDouble();

    double quotient = numerator / denominator;
    String number = "" + quotient;
    char n = number.charAt(0);
    int j = 1;
    int z = 0;
    String output = "";
    char[] index = number.toCharArray();
    int max = -1;
    int currentNumber = -1;
    int temp = -1;
    int tempMax = -1;
    System.out.println("" + quotient);
    boolean check = true;
    for(int i = (number.indexOf(".") + 1); i < index.length; i++)
    {
        if(max == -1)
        {
            currentNumber = i;
            temp = i;
            max = 1;
            tempMax = 1;
        }
        else
        {
            if(index[i] == index[i-1])
            {
                tempMax++;
            }
            else
            {
                if(tempMax > max)
                {
                    check = false;
                    max = tempMax;
                    currentNumber = temp;
                }
                tempMax = 1;
                temp = i;
            }
        }
    }
    if(check)
    {
        max = tempMax;
    }
    System.out.println(index[currentNumber] + " repeats " + max + " times.");
}

Example of input/output:

Enter N,D: 1
3
0.3333333333333333
3 repeats 16 times.
brso05
  • 13,142
  • 2
  • 21
  • 40
  • Hmmm. I may get an idea from this. But I am looking for the numbers that are repeated in the decimal and will be enclosed in a parenthesis. e.g - 1 / 3 = .(3) 1 / 7 = .(142857) – Rod Galangco Dec 03 '14 at 14:31