0

This code isn't working. Can you tell me what's wrong and why?

package exer0403e08;


public class EXER0403E08 {
    public static void main(String[] args) {
        String str= "hello";
        System.out.println(str);
        char[]strchar = str.toCharArray();
        int first;
        int last=5;

        System.out.println("The reversed is: ");
        for (first=1; first<=5; first++){
            strchar[first]=strchar[last];


            last--;

        } 
        str=String.valueOf(strchar);
        str=str.toUpperCase();
        System.out.println(str);


    }

}

The answer is "OLLO" and i want to make it "OLLEH".

6
  • 1
    It is java not JavaScript Commented Mar 12, 2017 at 10:20
  • this i not a jaascript question Commented Mar 12, 2017 at 10:20
  • what is this by the way? Commented Mar 12, 2017 at 10:22
  • @h.harry Java :) I am just wondering that you wrote the program without knowing the language name you used ? Commented Mar 12, 2017 at 10:25
  • 1
    Possible duplicate of Reverse a string in Java Commented Mar 12, 2017 at 10:29

3 Answers 3

1

You loop have several problems. And you misunderstood the array indexes.

You are iterating and modifying the same array. Hence the weird behaviour and indexces will start from zero for arrays.

So the fixed code will be

public static void main(String[] args) {
        String str = "hello";
        System.out.println(str);
        char[] strchar = str.toCharArray();
        int first;
        int last = 4;

        System.out.println("The reversed is: ");
        for (first = 0; first < 5; first++) {
            strchar[first] = str.charAt(last);

            last--;

        }
        str = String.valueOf(strchar);
        str = str.toUpperCase();
        System.out.println(str);

    }

Update :

Demo link http://ideone.com/GfgDZ3

Sign up to request clarification or add additional context in comments.

6 Comments

@h.harry output is correct..see changes in above code
Added a demo link.
thank you so much. i tried it once but no changes. but in my second trial, it's like magic. by the way, what is the language used?
@h.harry Java dude. Java.
@sᴜʀᴇsʜᴀᴛᴛᴀ i am still laughing for this😂😂 java dude java
|
1

you can write your own method like this:

    public static String reverse(String str)
     {
     String reversed = new String();

     for ( int j = str.length()-1; j >= 0; j-- )
         reversed += str.charAt(j);

     return reversed;
     }

and then try reverse("Hello");

2 Comments

that's complicated
@h.harry well for your comments on other answer..s.o. is q&a site but a strict one which is why it has quality. Google for it. All the best for coding..and this ans is not complicated just index is reversed
0

You have better methods in java to reverse string. Please check this answer :

You can use this:

new StringBuilder(hi).reverse().toString()

Or, for versions earlier than JDK 1.5, use java.util.StringBuffer instead of StringBuilder — they have the same API. Thanks commentators for pointing out that StringBuilder is preferred nowadays.

6 Comments

Didn't think that SO is a tutorial site :-) And , the question didn't say he just want to learn loops :-)
ok but our teacher won't accept that. he prefers using for loops only.
what's the purpose of asking?
can you please tell me the language used?
@sᴜʀᴇsʜᴀᴛᴛᴀ what's the language used?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.