October 25th, 2013, 10:40 PM
ReverseString Java Homework Help needed
Hey I'm new to java helps forums so I hope this post is in the right spot. I'm having trouble with figure out how to reverse this string for my java homework. I can get it to print the last character the user inputs, but I'm not sure why it's only printing the last character not the whole thing. Any suggestions or tips greatly appreciated. Very new to programming. Below is my code.
Reads a string, reverses the order the characters within the
string, and prints out the result.
Input: the value of s, a string
Output: the string reversed
public class ReverseString
public static void main(String args)
Scanner in = new Scanner(System.in);
int count = 0;
String n = in.nextLine();
//String s = in.nextLine();
for(int i=n.length(); i<=0;i--)
// String s = n.substring(n.length()-1);
//String s = n.substring(n.length()-1);
// System.out.println( n);
String s = n.substring(n.length()-1);
// your work here
Last edited by neekman; October 25th, 2013 at 11:38 PM.
October 25th, 2013, 11:11 PM
a) you need to use [ CODE ] tags in this forum, b) take a close look at that for loop. How many iterations will it have?
Apart from that, I don't really get what you're doing there. "Counts the number of digits with value 7"? What does this have to do with reverting strings? And what's the point of outputting the last character at the end?
I strongly suggest you get clear about the right approach before you fumble with Java code. Take a piece of paper and write down pseudo code or a diagram or whatever. When you know exactly what to do, then implement it with Java. Not before.
October 25th, 2013, 11:36 PM
Ya sorry about that, that was another assignment I was working on. I just copy and paste the intro code to save some time, forgot I had kept the instructions. sorry about that
Originally Posted by Jacques1
October 29th, 2013, 04:42 AM
use charAt() to reverse the string that is easily executable