# Sequence Classification

• May 31st, 2012, 08:41 PM
SilentNite17
Sequence Classification
Hey guys,

Just made a small new program off one I made earlier, it asks you for a sequence and then classifies it to be either arithmetic or geometric or nothing. Here is the code:

Code :

```import java.util.Scanner;   public class ArithmeticSequenceClassification {   public static void main(String[] args) {   int[] sequence = new int[4]; int num1, num2, num3, num4, num5; int d; int n; boolean isArithmetic, isGeometric, isNothing; String output;   Scanner scan = new Scanner(System.in);   System.out.println("Please enter 5 numbers. Your inputs will be scanned and classified by their sequence attributes."); System.out.print("Enter your first number: "); num1 = scan.nextInt();   System.out.print("Enter your second number: "); num2 = scan.nextInt();   System.out.print("Enter your third number: "); num3 = scan.nextInt();   System.out.print("Enter your fourth number: "); num4 = scan.nextInt();   System.out.print("Enter your last number: "); num5 = scan.nextInt();   sequence[0] = num1; sequence[1] = num2; sequence[2] = num3; sequence[3] = num4; sequence[4] = num5;   d = num2 - num1; n = num2 * num3;   if(num2 - num1 == d) {   if(num3 - num2 == d) {   if(num4 - num3 == d) {   if(num5 - num4 == d) {   isArithmetic = true; isGeometric = false; isNothing = false;   }   }   }   }   else if(num2 / num1 == n && num2 % num1 == 0) {   if(num3 / num2 == n && num3 % num2 == 0) {   if(num4 / num3 == n && num4 % num3 == 0) {   if(num5 / num4 == n && num5 % num4 == 0) {   isGeometric = true; isArithmetic = false; isNothing = false;   }   }   }   }   else {   isArithmetic = false; isGeometric = false; isNothing = true;   }   if(isArithmetic = true) {   output = "Your sequence was: Arithmetic.";   }   else if(isGeometric = true) {   output = "Your sequence was: Geometric.";   }   else {   output = "Your sequence was not a sequence.";   }   System.out.print(output);   }   }```

I cannot see any possible errors within the program, and neither does Eclipse. However, when I run it, it will not accept my number inputs and go on to asking for the next number!

-Silent
• May 31st, 2012, 08:46 PM
Norm
Re: Sequence Classification
Can you post the console's contents from when you execute the program.

I suspect that you are having problems with the Scanner class not emptying its buffer of the line end character. For a test enter all your input on one line before pressing Enter.
To make the Scanner class get rid of the line end character in its buffer, call the nextLine() method.
• May 31st, 2012, 08:46 PM
SilentNite17
Re: Sequence Classification
OK, Let me try that.
• May 31st, 2012, 08:47 PM
SilentNite17
Re: Sequence Classification
It will not accept it, as the variable of num1 etc is int, not String.
• May 31st, 2012, 08:49 PM
Norm
Re: Sequence Classification
Can you post the console's contents from when you execute the program.
• May 31st, 2012, 08:52 PM
SilentNite17
Re: Sequence Classification
OK, here is what happens.

It prints what it it supposed to:

Please enter 5 numbers. Your inputs will be scanned and classified by their sequence attributes.

Then when I enter a number and press enter, the console skips to a new line without typing or printing anything new.
• May 31st, 2012, 08:57 PM
Norm
Re: Sequence Classification
Did you try as I suggested? enter all 5 numbers BEFORE pressing Enter?
• June 1st, 2012, 08:21 AM
razorsknife
Re: Sequence Classification
Try this change in your sequence declaration in main:
int[] sequence = new int[5];
• June 1st, 2012, 08:25 AM
Norm
Re: Sequence Classification
@razorsknife You're getting ahead of the OP. He hasn't reported the exception yet.