Gagan93
Gagan93

Reputation: 1876

Some Error in a simple java code

this is a simple Java code which reverses a String without using any String API function, but in the last line, when it prints the reversed string, there is some problem in output statement (System.out.println())

Here's the code :

class StringReverse
{
    public static void main(String[] args) throws java.io.IOException
    {
        int str[] = new int[100];
        int i=0,j;
        System.out.println("Enter a string");
        while(true)
        {
            str[i]=System.in.read();
            if(str[i++]==13)
                break;
        }
        String reversed="",simple = new String(str,0,i-1);
        System.out.println(simple);

        // now reversing the string
        for(j=i-1;j>=0;j--)
            reversed+=((char)str[j]);
        System.out.println("String is "+reversed);
    }
}

Sample output is enter image description here

Upvotes: 1

Views: 124

Answers (2)

nmargaritis
nmargaritis

Reputation: 879

Alternatively, Instead of having unneeded lines of code, you could simply do that by the use of StringBuilder. For more information about the StringBuilder visit this: Class StringBuilder - Oracle

Below is an implementation with the use of StringBuilder.

import java.lang.StringBuilder;
import java.util.Scanner;

public class ReverseString{
    public static void main(String[] args){
        //String s = getString("Please enter a string");
        //you can either pass the getString directly into the constructor of 
        //StringBuilder or first instanciated and pass that String.
        StringBuilder rev = new StringBuilder(getString("Please enter a String"));
        //A build-in method of the StringBuilder class that reverses a StringBuilder.
        StringBuilder k = rev.reverse();
        //A cmd print
        print("The reverse is: "+k);
    }

    public static String getString(String msg) {
        Scanner in = new Scanner(System.in);
        print(msg);
        String s = in.nextLine();
        return s;
    }

    public static void print(String s) {
        System.out.println(s);
    }
}

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726619

This is because you are running on Windows. When you press Enter, two special characters get transmitted - <CR>, or \r, and <LF>, or \n *. You are trapping the \n, and stop reading. However, \r remains in the buffer, and becomes the first character of the reversed string. That's why the reversed string gets printed on top of your "String is " output.

Here is what's going on at the end, step-by-step:

  • "String is " gets printed; the cursor is at the position number ten (zero-based)
  • The first character of the reversed string "gagan\r" gets printed. The character is invisible, but the position of the cursor becomes zero; the cursor remains on the same line
  • The reversed string "nagag" gets printed over the "Strin" portion of the "String is "
  • Now you see the output "nagagg is"

* <CR> stands for "carriage return"; <LF> stands for "line feed".

Upvotes: 4

Related Questions