Results 1 to 5 of 5
  1. #1
    Valerie10b is offline Member
    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0

    Default Help with reversing the characters of a String!!

    I have to Input a String from the user (use Scanner class method nextLine). Then, write a for loop that prints the characters of the String in reverse. I'm having trouble printing a string of characters to reverse after the user inputs them. when i put hello its supposed to say olleh, and it isnt. I have 2 other problems in my java application and they work perfectly fine. the only thing that isnt printing in the output is problem 3. the only thing that shows in the output is "Enter the String:" for problem 3, and when i do, nothing happens. What am i doing wrong?? Everything in my program is compiling and running. I'm new to programming, can someone help me and explain please!!!

    Java Code:
    //
    // problem # 1 - print odd numbers from 1 to 99
    //
    
    import java.util.Scanner;
    public class Lab6A {
    
    public static int Problem1() {
    
    System.out.println ("This is problem # 1\n");
       int x = 1;
       while(x <= 99) {
          System.out.println(x);
          x = x+2;
       }// end while loop
          return x;
       
      } // end
    
    
      public static void main (String args[]) {
       
       // Test All Problems 
       Problem1();
       System.out.println ("The odd numbers between 1 and 99");
       Problem2();
       Problem3();
      
                
       }//end
       
       
    //
    // problem # 2 - print a triangle of asterisks
    //
    
    public static void Problem2() {
    
    System.out.println ("\nThis is problem # 2\n");
       Scanner scan = new Scanner(System.in);
          int n =0;
          do {
             System.out.print("Enter a number: ");
          n = scan.nextInt();
          if (n<1 || n>50) {
             System.out.println("Error: Invalid input!");
          }
             } while (n<1 || n>50);
    
          TestProblem2(n);
     }//end main
    
       public static void TestProblem2(int base) {
       
          for(int i=1; i <= base; i++) { 
             for(int j=0; j < i; j++) {
                System.out.print("*");
          }//end loop i
                   System.out.println();
          }//end loop j
    }//end main
    
    
    //
    // problem # 3 - print the character of the string in reverse
    //
    
    public static String Problem3() {
    System.out.println ("\nThis is problem # 3\n");
    
       Scanner scan = new Scanner(System.in);
       System.out.println("Enter The String:");
       String s = scan.nextLine();
    
       for (int i = s.length()-1; i >= 0; i--) 
       {
       s += s.charAt(i);
       }
       return s;
    
    }
     
    
    
    }//end class

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,377
    Rep Power
    5

    Default Re: Help with reversing the characters of a String!!

    The problem with problem number 3 is you are assigning to the same string that your source string is in. So you are clobbering s. Use a different string variable.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Valerie10b is offline Member
    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0

    Default Re: Help with reversing the characters of a String!!

    Quote Originally Posted by jim829 View Post
    The problem with problem number 3 is you are assigning to the same string that your source string is in. So you are clobbering s. Use a different string variable.

    Regards,
    Jim
    im sorry, but how would i do that. I'm a bit confused

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,377
    Rep Power
    5

    Default Re: Help with reversing the characters of a String!!

    First you are clobbering (or more specifically appending to your s variable). If you want to return just the characters reversed, then don't assign each character back to s. Use a different variable, like r;

    Java Code:
    String r;
    r = .....
    ...
    
    return r;
    
    // And when you test this routine, use
    
    String v = Problem3();
    System.out.println(v);
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Help with reversing the characters of a String!!

    Method names should start with a lowercase letter.
    Code Conventions for the Java Programming Language: Contents

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Reversing a string?
    By jean28 in forum New To Java
    Replies: 1
    Last Post: 11-29-2012, 02:42 AM
  2. Reversing the characters within substring specified
    By kathyla18 in forum New To Java
    Replies: 18
    Last Post: 04-14-2009, 08:06 AM
  3. Reversing the String
    By Inaam in forum New To Java
    Replies: 1
    Last Post: 03-30-2009, 08:35 PM
  4. Reversing String
    By mew in forum New To Java
    Replies: 4
    Last Post: 12-02-2007, 09:42 PM
  5. reversing a string
    By toad in forum New To Java
    Replies: 5
    Last Post: 11-07-2007, 09:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •