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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Sequence Classification

  1. #1
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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.");
    		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!

    Help please?

    -Silent
    Last edited by SilentNite17; May 31st, 2012 at 08:42 PM. Reason: Spelling Error


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Sequence Classification

    OK, Let me try that.

  4. #4
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Sequence Classification

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

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Sequence Classification

    Can you post the console's contents from when you execute the program.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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.
    Enter your first number:

    Then when I enter a number and press enter, the console skips to a new line without typing or printing anything new.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Sequence Classification

    Did you try as I suggested? enter all 5 numbers BEFORE pressing Enter?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Jun 2012
    Location
    INDIA
    Posts
    2
    My Mood
    Inspired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sequence Classification

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

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Sequence Classification

    @razorsknife You're getting ahead of the OP. He hasn't reported the exception yet.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Sequence
    By r_james14 in forum Java Theory & Questions
    Replies: 2
    Last Post: April 8th, 2012, 10:05 PM
  2. The Fibonacci sequence
    By Ryuk_93 in forum Android Development
    Replies: 1
    Last Post: March 26th, 2012, 11:56 AM
  3. BFS Travel not in the sequence
    By keat84 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2012, 09:19 PM
  4. Data sequence
    By street_missile in forum Java ME (Mobile Edition)
    Replies: 10
    Last Post: August 26th, 2011, 11:54 AM
  5. text color changer based on words/ word classification by color
    By knoxy5467 in forum Java Theory & Questions
    Replies: 25
    Last Post: June 15th, 2011, 07:52 AM