Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## 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:

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.");
num1 = scan.nextInt();

num2 = scan.nextInt();

num3 = scan.nextInt();

num4 = scan.nextInt();

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

2. ## 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.

3. ## Re: Sequence Classification

OK, Let me try that.

4. ## Re: Sequence Classification

It will not accept it, as the variable of num1 etc is int, not String.

5. ## Re: Sequence Classification

Can you post the console's contents from when you execute the program.

6. ## 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.

7. ## Re: Sequence Classification

Did you try as I suggested? enter all 5 numbers BEFORE pressing Enter?

8. ## Re: Sequence Classification

Try this change in your sequence declaration in main:
int[] sequence = new int[5];

9. ## Re: Sequence Classification

@razorsknife You're getting ahead of the OP. He hasn't reported the exception yet.