Java Help

Archived from the original Sajha.com — preserved as posted, replies can no longer be added here.
Start a New Discussion
Archived Post

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 · Dec 16, 2013 3:13 PM · 21,056 views

5 Replies

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.

cowgirl1996 · Dec 16, 2013 3:56 PM

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

blog · Dec 16, 2013 6:13 PM

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".

_nepali · Dec 16, 2013 10:58 PM

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

kalikopoi · Dec 17, 2013 10:40 AM

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

kehiminute · Dec 17, 2013 6:48 PM

This conversation is preserved exactly as it was on the original Sajha.com and can't accept new replies.

Start a New Discussion

You might be interested in...

Recent Classifieds View all
Upcoming Events View all
Service Providers View all