i dont know what mistake i have done here i could run the prog but i am not getting the o/p so guys pls help me and this is the coding if any corrections is need pls let me know and if possible change and send me the correct prog

coding:

import java.util.*;

import org.apache.commons.lang.StringUtils;


public class reverse 
{
	public static void main(String args[])
	   {
	      String original="hi how are you", reverse ="",nonrecurse=" ",delimitedReverse="";
	      Scanner in = new Scanner(System.in);
	 
	     // System.out.println("Enter a string to reverse");
	     // original = in.nextLine();
	 
	      //int length = original.length();
	      
	   //  reverse=reverseMe_recursion(original);
	   //=reverseMe_nonrecursion(original);
	     
	      rev(original);
	 
	     // for ( int i = length - 1 ; i >= 0 ; i-- )
	     //    reverse = reverse + original.charAt(i);
	      
	      
	    //delimitedReverse = StringUtils.reverseDelimited(original, ' ');
   	      System.out.println("Reverse of string using recursion is:  "+reverse);
   	   System.out.println("Reverse of string using nonrecursion is:  "+nonrecurse);
	      System.out.println("The delimited Reverse string: " +delimitedReverse);
   	      
   	      
		  }
	
	 static String reverseMe_recursion(String s) {
	        if(s.length() == 0)
	          return "";
	        return s.charAt(s.length() - 1) + reverseMe_recursion(s.substring(0,s.length()-1));
	      }
	 
	 static String reverseMe_nonrecursion(String s) {
		   StringBuilder sb = new StringBuilder();
		   for(int i = s.length() - 1; i >= 0; --i)
		     sb.append(s.charAt(i));
		   return sb.toString();
		 }
	 
	 public static void rev(String original)
	 {
		 try
			{
				for(int i=0;i<original.length();i++)
				  {
				   if(original.charAt(i)==i)
				   {
				   
				    int count = 0;
					count++;
				    	
				    }
				   
				  }
				 }
				 catch(Exception e)
				 {
					 
				 }
	 }
		 
		}

Edited 4 Years Ago by peter_budo: Keep It Clear - Do use code tags when posting any code

well, if it doesn't do what it 's supposed to do, probably corrections are needed.

do you get wrong output, does your application crash?

be a bit more specific.
also, don't do:

try{
// your code
}
catch(Exception e)
{

}

you are just hiding the error message, that doesn't mean there isn't one. replace previous code by:

try{
// your code
}
catch(Exception e)
{
e.printStackTrace();
}

it's also bad design to use the Exception superclass. be as specific as you can, even if that means you need to add five catch clauses there. you'll get a bit more information as to what goes wrong, and in some cases, not all exceptions should be dealt with the same way.

fixed it!

import java.util.Scanner;

public class reverse {
	public static void main(String args[]) {
		String original = "hi how are you", reverse = "", nonrecurse = " ", delimitedReverse = "";
		Scanner in = new Scanner(System.in);

		// System.out.println("Enter a string to reverse");
		// original = in.nextLine();

		int length = original.length();

	[B]	reverse = reverseMe_recursion(original);
		nonrecurse = reverseMe_nonrecursion(original);[/B]

		rev(original);

		// for ( int i = length - 1 ; i >= 0 ; i-- )
		// reverse = reverse + original.charAt(i);

		// delimitedReverse = StringUtils.reverseDelimited(original, ' ');
		System.out.println("Reverse of string using recursion is: " + reverse);
		System.out.println("Reverse of string using nonrecursion is: "
				+ nonrecurse);
		System.out.println("The delimited Reverse string: " + delimitedReverse);

	}

	static String reverseMe_recursion(String s) {
		if (s.length() == 0)
			return "";
		return s.charAt(s.length() - 1)
				+ reverseMe_recursion(s.substring(0, s.length() - 1));
	}

	static String reverseMe_nonrecursion(String s) {
		StringBuilder sb = new StringBuilder();
		for (int i = s.length() - 1; i >= 0; --i)
			sb.append(s.charAt(i));
		return sb.toString();
	}

	public static void rev(String original) {
		try {
			for (int i = 0; i < original.length(); i++) {
				if (original.charAt(i) == i) {

					int count = 0;
					count++;

				}

			}
		} catch (Exception e) {

		}
	}

}

the problem was line 13 and 14 were commented out

also note

import org.apache.commons.lang.StringUtils;

is not needed

Edited 4 Years Ago by Jaggs: n/a

hi i need to get the output for recursive and non-recursive and also delimited Reverse string in which i couldnt find out what and where i have done wrong and i m not so good in java so anybody can help me out pls with the proper coding or any ideas where i could correct myself.....

I meant did you try the suggestions posted by stultuske and Jaggs?

If it's yes and there's new error then could you post the current code and it's error so we may help

After trying the suggestions posted by stultuske and Jaggs I am getting the output for recursive and non recursive but i am not getting the o/p for the delimted.
delimted means
if i am giving i/p as "HI HOW ARE YOU"
I should get the o/p as "YOU ARE HOW HI"

use the split(" "); method of the String class, to get an array of String elements based on the words in your original String, then, print the words in that array starting with the last word, .. until the first word, each time followed by a " "

String myString = "This is my sentence";
String[] rowString = myString.split(" ");

run that, and check the contents of rowString, that should give you at least an idea of how to do it.

actually, I told you step by step what to do, any more precise would be giving the code.

i m getting output as null for delimited

Edited 4 Years Ago by jayadan: n/a

and ... do you think that could be caused by you having the line of code that sets that value commented out?

nope i have chked all the needed lines for delimited and took the slashes off

well, have you tried what I suggested and wrote your own method to perform that task? can you show your code how it is now?

import java.util.*;

import org.apache.commons.lang.StringUtils;


public class reverse 
{
	public static void main(String args[]) {
		String original = "hi how are you", reverse = "", nonrecurse = " ", delimitedReverse = "";
		Scanner in = new Scanner(System.in);

		int length = original.length();

		reverse = reverseMe_recursion(original);
		nonrecurse = reverseMe_nonrecursion(original);

		rev(original);

		

		delimitedReverse = StringUtils.reverseDelimited(original, ' ');
		System.out.println("Reverse of string using recursion is: " + reverse);
		System.out.println("Reverse of string using nonrecursion is: "
				+ nonrecurse);
		System.out.println("The delimited Reverse string: " + delimitedReverse);

	}

	static String reverseMe_recursion(String s) {
		if (s.length() == 0)
			return "";
		return s.charAt(s.length() - 1)
				+ reverseMe_recursion(s.substring(0, s.length() - 1));
	}

	static String reverseMe_nonrecursion(String s) {
		StringBuilder sb = new StringBuilder();
		for (int i = s.length() - 1; i >= 0; --i)
			sb.append(s.charAt(i));
		return sb.toString();
	}
	
	static String delimited (String s)
	{
		String myString = "hi how are you";
		String[] original = myString.split(" ");
		return "";
		
	}
	public static void rev(String original) {
		try {
			for (int i = 0; i < original.length(); i++) {
				if (original.charAt(i) == i) {

					int count = 0;
					count++;

				}

			}
		} catch (Exception e) {

		}
	}

		}

well, I see you tried this:

static String delimited (String s)
	{
		String myString = "hi how are you";
		String[] original = myString.split(" ");
		return "";
		
	}

at this point, you have an array original, with:

original[0] = "hi";
original[1] = "how";
original[2] = "are";
original[3] = "you";

in order to print the original sentence, based on this array, you could say:

String s = "";
for ( int i = 0; i < original.length; i++)
  s+= original[i] + " ";
System.out.println(s);

you need something similar, you just need to reverse your loop, so you start your iteration at the last element of your array and work your way back to the first element.

well thnks a lot stultuske ur great and ur helpful but i m being dumb and purely new to java i m getting errors again so anyways thnks for spending ur time for me..:):):)

saying you get errors doesn't help us solve them. what errors do you get?
a tip for future reference:

} catch (Exception e) {

		}

that's very bad coding, especially if errors still occur.
1. be as specific possible with what Exception you catch, even if that means you have to add ten catch clauses for each try, you'll be able to make the error message a lot more specific and appropriate to that exception
2. add an error message, don't just hide the error
so, in your case:

} catch (Exception e) {
e.printStackTrace(); // not sure what exception you're catching, so can't 
// be more specific about it :)
		}

the error says that it is "The type of expression must be an array type but it is resolved to string "

where in your code is that exception thrown? doesn't it say more, like which method or line throws the exception?

all the line other than the for statement line
String original = "";
original[0] = "hi";
original[1] = "how";
original[2] = "are";
original[3] = "you";
for ( int i = 0; i < s.length(); i++)
s+= original + " ";

well, you've declared original as a normal string, not as an array of string objects.
so, let's say your original input is stored in the String 'abc'

you're not supposed to put the lines

original[0] = "hi";
original[1] = "how";
original[2] = "are";
original[3] = "you";

I just added them to show you what the result of

String[] original = abc.split(" ");

would be.

so, original is your original text, but it is not your original String object, it's an array of Strings, containing the same text as your original String, where a space is used to show where the next element starts. in my example here, abc is your original String, which you should pass as a parameter to the method

Sadiaali: why did you post this in this thread, while the question is not related to the topic?
here you'll find all you need to write, compile and run java applications, but that's something you could 've found in the sticky thread on top of the java forum too.

This article has been dead for over six months. Start a new discussion instead.