# I need help developing an algorithm.

• October 6th, 2013, 04:54 PM
Rain_Maker
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.

Code Java:

```  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.
• October 6th, 2013, 06:55 PM
camel-man
Re: I need help developing an algorithm.
i thought Java used Unicode?
• October 6th, 2013, 07:09 PM
Ubiquitous
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
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.
• October 6th, 2013, 07:28 PM
Norm
Re: I need help developing an algorithm.
Quote:

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.
• October 7th, 2013, 12:21 PM
Rain_Maker
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
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
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?.......
• October 7th, 2013, 05:35 PM
Ubiquitous
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
Code :

```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?
• October 8th, 2013, 02:26 AM
GregBrannon
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.