Posted by: kehiminute December 17, 2013
Java Help
Login in to Rate this Post:     0       ?        
What went wrong on your code.

1.Compilation Error: Your class name must match the file name on which main method is invoked. You have defined your class as "test2" but you might have save it as test.java. Rename it to test2.java

2. Two Constructor and Two Methods : I believe that you have used the template provided by your university. There are two methods named "reverse". one of which throw the unsupported exception and another one which contains your code. Remove the one that throws unsupported exception. Similarly you constructor has been written twice. i.e. you have two MyStringtest() in your code. Remove the one which contains unsupported exception.

3. You haven't passed any string while initializing the 
constructor. Initialize the constructor with a string ie. ("mystring") instead of passing the string on reverse method. 
 
DO THIS

 MyStringTest mst = new MyStringTest("Ishwarkobardan");
  mst.reverse();

Instead of 

 MyStringTest mst = new MyStringTest(); 


mst.reverse("MyStringTest"); 


I have attached the code for you. You must SAVE this file as Main.java to get the output.


 
class MyStringTest
 {
    String name;
    public MyStringTest(String temp)//constructer that initializes  object
    {
        name=temp;
    }
 
    public void reverse()
    {
        int len = name.length();
        String s="";
        for(int i=len-1; i>=0; i--)
 
        {
            char c=name.charAt(i);
            String s1 = Character.toString(c);
            s=s.concat(s1);
        }
        System.out.println (s);
 
    }
 
  }
 
public class Main {
 
    public static void main(String[] args) {
        MyStringTest mst = new MyStringTest("Ishwarkobardan");
        mst.reverse();
 
    }
}
 
 
 
HAPPY CODING :) :) :)




Last edited: 17-Dec-13 06:48 PM
Last edited: 17-Dec-13 06:50 PM
Read Full Discussion Thread for this article