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 7 of 7

Thread: I need help developing an algorithm.

  1. #1
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default I need help developing an algorithm.

    Hello Community,

    I don't know if this some type of algorithm or just a method that I can use. Below is my code.

     
    import java.util.*;
     
    public class algorithmTest
    {
        public static void main(String[] args)
        {
            Scanner scan = new Scanner(System.in);
     
            System.out.print( " Please input a String value: ");
            String inputString = scan.nextLine();
     
            System.out.print( " Please input a int value 1-25: ");
            int inputInt = scan.nextInt();
     
            char[] myChar;
            myChar = inputString.toCharArray();
     
            char myCharJr = myChar[0];
     
            int intCharValue = (int)myCharJr;
     
     
            if( intCharValue >= 65 || intCharValue <= 90)
            {
                int charNew = inputInt + intCharValue; 
     
     
                System.out.println( " New char value: "  +   (char)charNew);
            }
     
            else if( intCharValue >= 97 || intCharValue <= 122)
            {
                int charNew = inputInt + intCharValue; 
                System.out.println( " New char value: "  +   (char)charNew);
            }
     
            else
            {
                System.out.println( "You value is greater than or less than the paramater" );
            }  
     
     
        }
     
     
     
    }

    I'm doing an assignment and testing out a few things from this little test code. What I want to do is add the value of my char value and convert it into a integer. From converting it into a integer I want to shift the value depending on the users input i.e. an integer 1-26. Here is the problem I only want to display the alphabet A-Z and a-z. When I get to z or Z my program does what its supposed to do. Add the value. But I want it to start over i.e. User enters W..... user enters 6.... 6 is added to the ASCII value of 87((W) + 6). I want my ASCII value to start from A once it reaches to 90....and starts back to 65. Is there a way to do this. I hope this clarify my question.


  2. #2
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: I need help developing an algorithm.

    i thought Java used Unicode?

  3. #3
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: I need help developing an algorithm.

    I am guessing the char value for A = 65 and Z = 90 and you are asking how to make it wrap around if it goes past 90 so it goes back to the beginning. I'd put it into it's own method and pass it both the char and places the user wants to shift positions then return the new char it will become.

    In the method you could
    add values
    while (shifted values > 90)
    subtract the difference of 90 and 65
    *end while*
    return new character

    There is probably a cleaner way of doing this but this is just to give you a quick idea of one way to do it.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I need help developing an algorithm.

    I am guessing the char value for A = 65 and Z = 90
    You don't need to use int values. You can do arithmetic with the char values: 'A' and 'Z' directly.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: I need help developing an algorithm.

    Hello everyone,

    Thank you for responding! Unbiquitous.....I'm not quite sure what you mean. I think you want me to subtract the difference? I'm really not quite sure. Could you explain your pseudo code again?...And Norm what do you think I should do as well. Do you have any ideas?

    --- Update ---

    Quote Originally Posted by Ubiquitous View Post
    I'd put it into it's own method and pass it both the char and places the user wants to shift positions then return the new char it will become.

    In the method you could
    add values
    while (shifted values > 90)
    subtract the difference of 90 and 65
    *end while*
    return new character

    There is probably a cleaner way of doing this but this is just to give you a quick idea of one way to do it.

    What do you mean Ubitquitous?.......

  6. #6
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: I need help developing an algorithm.

    @Norm I wasn't saying to convert them to do the int I was simply wondering if the value for 'A' was 65 and 'Z' was 90. I should have been a little clearer on my confusion since I don't exactly know the values for chars.

    @Rain_Maker
    Ok so there is a way to make it a single line with some simple math involved. Anyways for the way initial method I told you about the difference between 90 - 65 = 25 + 1. Why is the difference + 1 important? Well since you want it to wrap around when it hits 91 you want it to go back to 65 so you would actually have to subtract 26 not 25.

    For example
    char w (87) + 6 = 93
    checks: while (93 > 90)
    93 - 26 = 'C' (67)
    checks: while (67 > 95) false
    *exit loop*
    return newChar;

    The while loop allows you to wrap around no matter the shift they provide. As I mentioned this can be done in a single line try to figure it out mathematically. What does it do and why does it work?

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: I need help developing an algorithm.

    A simpler (some would say "more elegant") approach to wrapping back to the beginning is to use the modulo, or remainder, operator.

Similar Threads

  1. Replies: 3
    Last Post: June 10th, 2013, 04:42 AM
  2. Developing a simulator
    By jack_nutt in forum AWT / Java Swing
    Replies: 1
    Last Post: September 5th, 2012, 07:06 AM
  3. problem developing a frame help me...
    By bluestar_me in forum AWT / Java Swing
    Replies: 3
    Last Post: February 18th, 2012, 05:18 AM
  4. Need help in developing a clock application
    By sb.shiv in forum AWT / Java Swing
    Replies: 2
    Last Post: August 24th, 2010, 02:00 AM
  5. How to develop plugins in Netbeans?
    By haygaurav in forum Web Frameworks
    Replies: 1
    Last Post: May 27th, 2009, 04:49 PM