#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    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.


    Code:
    import java.util.Scanner;
    
    /**
        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;
          System.out.println("  ");
          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);
              count++;
          
             
            
        }
          
        
        String s = n.substring(n.length()-1);
        System.out.println( s);
        
        // your work here
         //System.out.println(i);
         // System.out.println(count);
       }
    }
    Last edited by neekman; October 25th, 2013 at 10:38 PM. Reason: [CODE]
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1045
    Hi,

    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.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0
    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
    Hi,

    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.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    5
    Rep Power
    0

    reversestring


    use charAt() to reverse the string that is easily executable

IMN logo majestic logo threadwatch logo seochat tools logo