Results 1 to 5 of 5
  1. #1
    Yoana is offline Member
    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0

    Default consecutive numbers!

    im trying to do a program to find if numbers are consecutive or not! if they are consecutive i need a true as a return and a false when they are not... i did this program and im sure i did something wrong because i keep only true returns .. can anyone please see my code and let me know what i did wrong!
    Example: (3,5,7) (1,2,2) (7,7,9) should return a false!
    Java Code:
    import java.util.*;
    public class Consecutive{
        public static void main (String [] args){
            Scanner console= new Scanner(System.in);
            System.out.println("Enter three numbers");
            String numbers = console.nextLine();
            System.out.println( "The numbers (" + numbers + ") is '" + consecutive( numbers ) + "'" );
        }//end of main
    
    
        private static boolean consecutive(String str) {
            String[] numbers = str.split(",");
            Arrays.sort(numbers);
            for (int index = 0; index < numbers.length-1; index++){
                if (Integer.parseInt(numbers[index]) > Integer.parseInt(numbers[index+1])){
                    return false;
                }
            }
            return true;
        }//end of consecutive method
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default Re: consecutive numbers!

    First, why did you repost this again and not just continue with your first thread --> consecutive numbers!?

    In any event, your logic is working fine for the sets you provided. You are checking if the first is greater than the second and if the second is greater than the third. Perhaps you should reverse your notion of what is false and true.

    BTW, what is your definition of consecutive? Are either of these sets consecutive?

    {1,3,4}
    {4,3,1}

    Because no matter where you return true or false in the code above, one of these will return true and one will return false.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: consecutive numbers!

    let me know what i did wrong
    Can you describe the algorithm the code uses to solve the problem?
    Are there other algorithms that work better?

    How many passes can be made over the array?
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Yoana is offline Member
    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0

    Default Re: consecutive numbers!

    someone mentioned that it may be easier using an array so that is why i stop working in the other program and started a new one. !
    (1,3,4) as well as (4,3,1)

  5. #5
    Yoana is offline Member
    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0

    Default Re: consecutive numbers!

    someone mentioned that it may be easier using an array so that is why i stop working in the other program and started a new one. !
    (1,3,4) as well as (4,3,1)

Similar Threads

  1. consecutive numbers!
    By Yoana in forum New To Java
    Replies: 1
    Last Post: 02-22-2014, 02:13 AM
  2. JPopupMenu Consecutive Clicks
    By PrinceSendai in forum AWT / Swing
    Replies: 2
    Last Post: 08-15-2012, 06:20 PM
  3. four consecutive characters
    By aortell24 in forum New To Java
    Replies: 3
    Last Post: 06-19-2012, 03:32 AM
  4. Consecutive null valors identifier - Suggestions
    By Bonfy in forum New To Java
    Replies: 4
    Last Post: 01-23-2012, 06:50 PM
  5. Replies: 23
    Last Post: 07-04-2011, 03:26 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •