Hi all, i need help with this code im trying to produce. I've been searching google for days without finding any answers so please youve got to help me understand. The idea is to encrypt a string inputted by the user

I wish to increment the letters at odd positions by 5 in a string (only the letters..the other characters must stay the same)

for example : 2345f57hBFE&&/f3 should result in 2345f57mBKE&&/f3

example : 'h' which occupies position 7 has been incremented by 5 to become m in the new string.

the thing is I have absolutely no idea where to start!
I've been working with the string class a bit on blueJ but i know only how to increment numbers with the ++ and still i can only do a number at a time. How can I do this with a whole string?

please I really need your help.

Use the String class's toCharArray method. Then use a for loop to go through each character in the array. Decide whether or not to increment the character depending on whether or not it is a letter. You can figure out whether or not it is a letter with the Character class's isLetter method.

Use the String class's toCharArray method. Then use a for loop to go through each character in the array. Decide whether or not to increment the character depending on whether or not it is a letter. You can figure out whether or not it is a letter with the Character class's isLetter method.

could you help me get started on some code..like i said im a real begginner.
this is what i did so far

public class increment
{

public static void main (String args[]) {


      String str = "Welcome ";
      System.out.println(str);

      // charAt method returns the corresponding character to the passed index
      // in parentheses
      char letter = str.charAt(0), letter1 = str.charAt(1), letter2 = str
          .charAt(2), letter3 = str.charAt(3), letter4 = str.charAt(4), letter5 = str
          .charAt(5), letter6 = str.charAt(6), letter7 = str.charAt(7);

          letter2++;letter2++;letter2++;letter2++;letter2++;
      System.out.println(letter+""+letter1+""+letter2+""+letter3+""+letter4+""+letter5+""+letter6+""+letter7); 

}
}

this make the 3rd letter change from 'l' to 'q'

but the thing is...the string inputted could be longer than 7 characters...that would make my code useless..also is there a way to increment a letter 5 times
other than doing letter1++;letter1++;letter1++;letter1++;letter1++; .... I need something like a loop dont I?

please share your expertise with me .

char ch = 'a';
ch += 5;
System.out.println(ch);

As for everything else, I already told you everything else you need to know to do this. Like I said, yes you need a loop - you need a for loop. I'll give you one more hint: if you use toCharArray to put your String into an array of chars, then you can use a for loop to loop over each of the chars. So how will you know how long the array is? Use array.length

char ch = 'a';
ch += 5;
System.out.println(ch);

As for everything else, I already told you everything else you need to know to do this. Like I said, yes you need a loop - you need a for loop. I'll give you one more hint: if you use toCharArray to put your String into an array of chars, then you can use a for loop to loop over each of the chars. So how will you know how long the array is? Use array.length

but how will I increment 'w' for example? it gives me the character '/' while I want it to give 'b' ?

Convert the char into an int. Figure out what range of ints represents the alphabet. (Just System.out.println() the int value) Then use if statements to make sure that you wrap around properly.

for (char start = 'a'; start < 'z'; start++){
System.out.println((int)start);
}

Convert the char into an int. Figure out what range of ints represents the alphabet. (Just System.out.println() the int value) Then use if statements to make sure that you wrap around properly.

for (char start = 'a'; start < 'z'; start++){
System.out.println((int)start);
}

i tought this was the begginner's section, what youre telling me to do seems pretty advanced...

please tell me how to get the user to input a string and use the array on that string

Casting a char to an int is not advanced Java, it is basic. And I already gave you the exact code you need to use. In addition to that, all you need to complete your program is a few if statements and a for loop. Use Scanner to read in a String. Look at the example at the top that uses System.in as an argument to the Scanner constructor. You can use either the next() method or the nextLine() method; look at the method docs for those two methods.

P.S.

There is no beginner, intermediate, or expert section. We also don't do people's homework for them. At this point I think I've actually listed everything you need to know to do this problem. If you don't understand specifics of what I said I can help you, but research the topics yourself first.

{

	
	String a="Welcome";
		   
        a=a.replace((a.charAt(5)),'5');
	
	System.out.println(a);
	
}

Here is a simple code.

{

	
	String a="Welcome";
		   
        a=a.replace((a.charAt(5)),'5');
	
	System.out.println(a);
	
}

Here is a simple code.

that only replaces the letter 'm' with '5' ... i need to increment it by 5 so it gives the letter
'r'. your code is not working

Check the String api. There is a method that creates an array of chars. Take the element at the 5th position and increase it by 5. Then use again the String api to find a method (or a constructor) and create a new String out of the char array that you changed.

To increase it is easy:

char old = 'b';
       int i = old+5; // b c d e f g
       char ch = (char)i;
       
       System.out.println(i);
       System.out.println(ch);

Check the String api. There is a method that creates an array of chars. Take the element at the 5th position and increase it by 5. Then use again the String api to find a method (or a constructor) and create a new String out of the char array that you changed.

To increase it is easy:

char old = 'b';
       int i = old+5; // b c d e f g
       char ch = (char)i;
       
       System.out.println(i);
       System.out.println(ch);

I see but...why does it print 103?

Do you understand why it prints 'g' ?.
If you take 'b' and add 5: c,d,e,f,g you will get g

Now as for the 103. Well , what would have it print? It is an int and it must print an int number. Why are you surprised.
Try this:

int a = 40;
System.out.println( (char)a );

I believe, if memory serves me well, that these int numbers are the ASCII code of the character.

thanks alot i get that part now... ok so to get the user to input a string i use the scanner class i figured that part already..but how do i make only odd number in that string get incremented?

it should be something like charAt --- string inputted
increment----- position 1,3,5,9,etc

to see if the char is not a non letter char i would use a if-else statement.

but for the positions thing...
how do I code this part? i need something like a loop that finds odd positions from 1 to n...can you help me out?

You cannot change the value of a specific char ina String. Convert it to a char array and loop the array. Change the elements of the array and convert it back to a String.

Check the API of the String and the Character.

For taking the 1,3,5, ... index, at loop increase the index by 2

You cannot change the value of a specific char ina String. Convert it to a char array and loop the array. Change the elements of the array and convert it back to a String.

Check the API of the String and the Character.

For taking the 1,3,5, ... index, at loop increase the index by 2

how do i convert a string to a char array??

how do i convert a string to a char array??

In the first post of this thread I told you how to do that. Then I repeated myself, re-explaining what to do. Then Java Addict repeated my advice on using the toCharArray method again. Maybe you should pay attention to the advice you are given and actually try it out instead of expecting people to write code for you.

that only replaces the letter 'm' with '5' ... i need to increment it by 5 so it gives the letter
'r'. your code is not working

Yeah, but I already gave you the correct code to do this in post #4. . The same goes for the multiple explanations of toCharArray you were given. Obviously you don't want "help" you want the fully working code so I am done here. Good luck on your assignment.

Yeah, but I already gave you the correct code to do this in post #4. . The same goes for the multiple explanations of toCharArray you were given. Obviously you don't want "help" you want the fully working code so I am done here. Good luck on your assignment.

its not that i want the code...i just want a sample code to get started...one using a chararray with a for loop since ive never used such a thing..im really thankful for the directions but since ive never seen the method in code before i dont know how to get started thats all...all the explanations online show you "the general idea" using thatpiece of code but i dont know how to immplement what youre telling me in the code with a working structure...and no im not asking you to do it for me

how do i convert a string to a char array??

Look at the String API as you have been instructed so many times. You do know how to call methods and loop arrays, don't you? Well search for the method described at the API. Or was the problem that you needed someone to post the link.

okay with all your instructions which im really thankful for sharing with me... and after a bit of browsing the String class... ive managed to get this so far :

public class Main {
    
    /**
     * Converts a String to a character array
     */
    public void convertStringToCharArray() {
        
         String str ;
        
System.out.println("Enter a string to increment");
str = Clavier.lireString();
        

        char[] cArray = str.toCharArray();
        
        for (char c : cArray)
          
            System.out.println(c+=5);
            
    }
    /**
     * Starts the program
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new Main().convertStringToCharArray();
    }
}

this prints out every letter of the string incremented by 5 only vertically..

like

'abc' prints out :

'f'
'g'
'g'

i know it's my loop that is incorrect...how can i print out a new 'normal' string ?

Write the for loop like this:

for (int i=0;i<cArray.length;i++) {
    // take each element of the array and [B]change it[/B]
}

Then after the loop is done, you will have the array with changed values.
Now check the constructors at the String API and see what you can do with that charArray.

Also the reason why you used such loop instead of your own is because the exercise says that only the 1,3,5, ... elements should change.
Well then start the loop at int i=1; and instead of incrementing the 'i' by 1: ;i++ do it by 2

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.