Results 1 to 5 of 5
- 03-12-2013, 01:36 AM #1
Member
- Join Date
- Mar 2013
- Posts
- 18
- Rep Power
- 0
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
- 03-12-2013, 01:47 AM #2
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 703
- Rep Power
- 1
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,
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 03-12-2013, 01:52 AM #3
Member
- Join Date
- Mar 2013
- Posts
- 18
- Rep Power
- 0
- 03-12-2013, 01:59 AM #4
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 703
- Rep Power
- 1
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;
Regards,Java Code:String r; r = ..... ... return r; // And when you test this routine, use String v = Problem3(); System.out.println(v);
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 03-12-2013, 05:09 AM #5
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
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Reversing a string?
By jean28 in forum New To JavaReplies: 1Last Post: 11-29-2012, 02:42 AM -
Reversing the characters within substring specified
By kathyla18 in forum New To JavaReplies: 18Last Post: 04-14-2009, 08:06 AM -
Reversing the String
By Inaam in forum New To JavaReplies: 1Last Post: 03-30-2009, 08:35 PM -
Reversing String
By mew in forum New To JavaReplies: 4Last Post: 12-02-2007, 09:42 PM -
reversing a string
By toad in forum New To JavaReplies: 5Last Post: 11-07-2007, 09:13 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks