Java Help - Sajha Mobile
SAJHA MOBILE
Java Help
Posts 6 · Viewed 10666 · Go to Last Post
cowgirl1996
· Snapshot 0
Like · Likedby · 0
This is the assignment:

Write a Java application titled MyStringTest that creates an instance of the String class and initializes this instance with a String literal. Use a for loop structure to print the string in reverse order. Implement the following two String member methods to complete the assignment.

  • length( )
  • charAt( )

Title your program to test the String class MyStringTest.

 

This is my code thus far, but cannot figure out my errors!

/**

 *

 * 12/15/2013

 * Assignment 9.4

 * Write a Java application that creates an instance of the String class and 

 * initializes this instance with a String literal. 

 * Bellevue University

 */

class MyStringTest


{

    String name;

    public MyStringTest(String temp)//constructer that initializes your object


{

    name=temp;

}


    MyStringTest() {

        throw new UnsupportedOperationException("MyStringTest"); //To change body of generated methods, choose Tools | Templates.

    }

    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);


}


    void reverse(String sajha) {

        throw new UnsupportedOperationException("tseTgnirtSyM"); //To change body of generated methods, choose Tools | Templates.

    }


 


 


}

    public class test

{


    public static void main(String args[])


{

    MyStringTest mst = new MyStringTest("tseTgnirtSyM"); 

    mst.reverse(); 

}


}


    class MyStringTest2


{

    public void reverse(String name)

{


    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 test2

{

public static void main(String args[])

{


MyStringTest mst = new MyStringTest(); 


mst.reverse("MyStringTest"); 


}


}


Can anyone help me, please?

Last edited: 16-Dec-13 03:24 PM
cowgirl1996
· Snapshot 34
Like · Liked by · 0
When I run test2 it gives me the follow error:
run:
java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Uncompilable source code - class test2 is public, should be declared in a file named test2.java
at test2.<clinit>(test.java:79)
Exception in thread "main" Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

This is where it is supposed to reverse the output.
blog
· Snapshot 143
Like · Liked by · 0
Here you go. try to debug this code to see how it actually works.  

public class MyStringTest{

     public static void main(String []args){
       
        String str = "ABCDEFGH";
        for(int i = str.length()-1; i>=0; i--) {
            System.out.print(str.charAt(i));
        }
     }
}
_nepali
· Snapshot 244
Like · Liked by · 0
The file-name should be test2.java (not test.java)
Or you can keep the file-name as test.java and change the class name to "test" from "test2". That is change the code snippet "public class test2" to "public class test".
KaliKoPoi
· Snapshot 343
Like · Liked by · 0
public class MyStringTest {                                  //Class name MyStringTest
   
    public static void main(String args[]){

        String s = new String("TEST STRING");      //initializes String instance with a String literal
        String reverseString = new String();            //temp variable to hold reverse string;

        for(int i = s.length()-1; i>-1; i--){                     //using loop structure and length() of string class.
            reverseString += s.charAt(i);                      //using charAt()
        }

        System.out.println(reverseString);
    }

}
kehiminute
· Snapshot 426
Like · Liked by · 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
Please log in to reply to this post

You can also log in using your Facebook
View in Desktop
What people are reading
You might like these other discussions...
· Posts 1 · Viewed 61
· Posts 1 · Viewed 74
· Posts 1 · Viewed 197
· Posts 16 · Viewed 3425 · Likes 3
· Posts 1 · Viewed 159
· Posts 1 · Viewed 249
· Posts 6 · Viewed 847 · Likes 1
· Posts 1 · Viewed 149
· Posts 1 · Viewed 160
· Posts 4 · Viewed 486



Your Banner Here
Travel Partners
Travel House Nepal