Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By doWhile

Thread: ascending order or not in ascending order

  1. #1
    hawkesey is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default ascending order or not in ascending order

    Hello

    I am fairly new to java and i am currently one of the modules that i am studding at university. I need to create a program that will read in a string and then work out if it is in ascending order or not in ascending order and return true or false.

    Below is the brief




    ascending order or not in ascending order-3.3.png


    Thanks and any help will be much appreciated





  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: ascending order or not in ascending order

    Sierra likes this.

  3. #3
    hawkesey is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: ascending order or not in ascending order

    Quote Originally Posted by doWhile View Post
    I looked at using the charAt method like i did in another exercise to look at the individual characters in the string but i this did not work. I was looking at one of the examples that the lecturer gives and i think that i will need to use an array?

    Thanks

  4. #4
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: ascending order or not in ascending order

    Yes you need probably to use arrays - you won't get more until you actually present your code, so we can see what you actually did and where you are having problems. Then people will help you out! Looking at things will not write your assignment...
    I like likes!

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,010
    Blog Entries
    7
    Rep Power
    20

    Default Re: ascending order or not in ascending order

    You don't need arrays; all you need is a boolean expression for a String s: s.charAt(i) <= s.charAt(i+1) that needs to be true for all applicable values of i to be in ascending order.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    hawkesey is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: ascending order or not in ascending order

    Quote Originally Posted by JosAH View Post
    You don't need arrays; all you need is a boolean expression for a String s: s.charAt(i) <= s.charAt(i+1) that needs to be true for all applicable values of i to be in ascending order.

    kind regards,

    Jos
    ok thank you i will look at this. i have been thinking about using an array but could not get it right. i will try you suggestion

    Thanks

  7. #7
    hawkesey is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: ascending order or not in ascending order

    Quote Originally Posted by JosAH View Post
    You don't need arrays; all you need is a boolean expression for a String s: s.charAt(i) <= s.charAt(i+1) that needs to be true for all applicable values of i to be in ascending order.

    kind regards,

    Jos
    Java Code:
    public class Main 
    {
      public static void Main (String args[])
            
       {
         
                System.out.println("Enter phrase");
                String p = BIO.getString(); 
             
                while (! p.equals("END"))
                
               { 
                    boolean inorder = true;   
    
                    while ( inorder )                 
                    {
                          inorder=false;             
    
                        for (int i=0; i<p.length(); i=i+1)
                            {     
                                
                                if ((p.charAt(i) <= p.charAt(i+1)))
                                {
                               
                                    inorder = true;            
             
                                }   
    
                                  
                              }
                              
                       if (inorder=true)
                    {
                        System.out.printf("%s letters in ascending order,p");
                    }
                              System.out.println("Enter phrase");
                              p = BIO.getString(); 
                              
                    }  
               }    
                    
      }
    }
    This is what i have done so far it is has been done quick to get some of my thoughts down.

    THanks

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,010
    Blog Entries
    7
    Rep Power
    20

    Default Re: ascending order or not in ascending order

    There are at least three mistakes in that code:

    1) check the terminating condition of that for-loop;
    2) if that expression happens to be true once, the characters don't need all to be in ascending order;
    3) check that assignment in line #32.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 8
    Last Post: 11-23-2012, 04:39 PM
  2. search array - ascending order
    By wiola111 in forum New To Java
    Replies: 5
    Last Post: 08-28-2012, 03:09 PM
  3. Sorting in ascending and descending order
    By flpanthers1 in forum New To Java
    Replies: 10
    Last Post: 06-27-2011, 03:48 PM
  4. ascending order using array rush
    By jca in forum New To Java
    Replies: 2
    Last Post: 01-03-2011, 04:24 AM
  5. How to add coins in ascending order in arraylist
    By tribujohn in forum New To Java
    Replies: 2
    Last Post: 01-23-2009, 04:31 AM

Posting Permissions

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