Results 1 to 8 of 8
  1. #1
    einstein1234 is offline Member
    Join Date
    Apr 2009
    Posts
    34
    Rep Power
    0

    Default [SOLVED] please help, this assignment is called resreveR

    Write a program (from scratch, if you can -- good practice for a test) named ResreveR. This program asks the User to type in a sentence (words separated by spaces and ending with a period). The sentence cannot be longer than 20 words.

    The program stores the words of the sentence in an array of Strings (a String[ ] ) in correct order (first word is in array element 0). Then the program prints out the words of the sentence backwards. That is, the last word of the sentence is followed by the next-to-last, etc. The letters in the words do not need to be reversed; just reverse the order of the words.


    help

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Hey Einstein, and welcome back. A couple of pointers:

    1) break this problem down into smaller steps
    * prompt user
    * get input and place into String
    * break String into words
    * put words into a String array, backwards order
    * display results

    So start from the top and work on each individual problem, one at a time.

    2) help is a poor question which I'm sure that you know. Remember, the more specific your question is, the more specific your answers are likely to be.

    3) If you get stuck on a particular step, then come back with your code attempt and a specific question.

    Again, best of luck.

  3. #3
    einstein1234 is offline Member
    Join Date
    Apr 2009
    Posts
    34
    Rep Power
    0

    Default

    thanx ok heres what i got so far..

    import java.util.Scanner;
    public class RR{
    public static void main (String[] args) {
    // Declare all variables to be used and request sentence from user.
    int x;
    String [] words;
    String input, reverse = "";
    Scanner scan1 = new Scanner(System.in);
    System.out.println("Type a sentence");
    input = scan1.nextLine();
    input = input.toLowerCase();
    input = input.replace("." , " ");
    Scanner scan2 = new Scanner(input);
    words = new String[20];

    // Store the words of the sentence in a String array.
    for (x = 0; x < 20; x++) {
    if (scan2.hasNext())
    {
    words[x] = scan2.next();
    }}

    is this right so far...and now i think i have to do the calculation

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    It could be right. What I'd do at this point would be to insert code to test if this is correct or not. This code would not be part of the final program. Something like so:

    Java Code:
    import java.util.Scanner;
    
    public class RR
    {
      public static void main(String[] args)
      {
        // Declare all variables to be used and request sentence from user.
        int x;
        String[] words;
        String input, reverse = "";
        Scanner scan1 = new Scanner(System.in);
        System.out.println("Type a sentence");
        input = scan1.nextLine();
        input = input.toLowerCase();
        input = input.replace(".", " ");
        Scanner scan2 = new Scanner(input);
        words = new String[20];
    
        // Store the words of the sentence in a String array.
        for (x = 0; x < 20; x++)
        {
          if (scan2.hasNext())
          {
            words[x] = scan2.next();
          }
        }
        
        // TODO: debug code. remove from final program
        for (int i = 0; i < words.length; i++)
        {
          System.out.println(words[i]);
        }
        // TODO: debug code. remove from final program
      }
    }
    Also, please don't forget to use code tags when posting code.

    edit: if you run this, you'll see a bunch of "null" String results in the output. This is important to know: if the input String is < 20 words (and most will likely be less than 20), the first part of the array will be filled with words, but the rest of the array will be filled with null values. This is important when you try to reverse your String. You could check to make sure that the array item is not null with a statement like so:
    Java Code:
    if word[i] != null
    {
      // this item is OK to use.
    }
    Also, when you get down to the business of reversing your String array, you'll likely have two arrays, your word String array, and another one, perhaps called reverseWord, or whatever you'd like. You'll likely use a for loop to do the placement of Strings from one array to the other, but you will need to be very careful with what indexes you need to use, that is the numbers that go in the square brackets: word[index number goes here]. You should work out on paper how to handle this. It's not hard to do and only requires some simple math.
    Last edited by Fubarable; 04-26-2009 at 03:21 AM.

  5. #5
    einstein1234 is offline Member
    Join Date
    Apr 2009
    Posts
    34
    Rep Power
    0

    Default

    import java.util.Scanner;
    public class ResreveR
    {
    public static void main (String[] args) {
    // Declare all variables to be used and request sentence from user.
    int x;
    String [] words;
    String input, reverse = "";
    Scanner scan1 = new Scanner(System.in);
    System.out.println("Type a sentence");
    input = scan1.nextLine();
    input = input.toLowerCase();
    input = input.replace("." , " ");
    Scanner scan2 = new Scanner(input);
    words = new String[20];

    // Store the words of the sentence in a String array.
    for (x = 0; x < 20; x++) {
    if (scan2.hasNext())
    {
    words[x] = scan2.next();
    }}

    // Print out the sentence in reverse order.
    for (x = 19; x >= 0; x--){
    if (words[x] != null) reverse = " " + words[x];
    System.out.print(reverse);
    }
    } // End of method main.

    } // End of class RR.

    this runs and looks good to me, wow i feel lke a programmer, wat u think?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    It feels good when things start coming together, doesn't it? You're coming along nicely, and it's good to see. Best of luck.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    wat u think?
    BabbleFish: "What do you think?"
    start at space, no!

    Also - please use the tags provided for your convenience.

    Edit: Also, also, this is just a nit pick (and annoyingly fiddly to fix). FB's right the code is an accomplishment.
    Last edited by pbrockway2; 04-26-2009 at 03:54 AM.

  8. #8
    einstein1234 is offline Member
    Join Date
    Apr 2009
    Posts
    34
    Rep Power
    0

    Default

    GREAT.yea it does feel good, and i was lazy back then , but now, i dont know, i feel like its getting somewhere, but anyways thanx for da help and goodnight.

Similar Threads

  1. [SOLVED] Method from one thread called on another thread
    By Ypsilon IV in forum Threads and Synchronization
    Replies: 7
    Last Post: 04-24-2009, 02:07 PM
  2. Return objects called
    By MV1 in forum New To Java
    Replies: 7
    Last Post: 03-11-2009, 07:16 AM
  3. javascipt function is nt getting called
    By pankaj_salwan in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-20-2008, 08:13 AM
  4. Replies: 1
    Last Post: 07-26-2008, 05:26 PM
  5. Replies: 0
    Last Post: 07-03-2008, 08:05 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
  •