Page 2 of 2 FirstFirst 12
Results 21 to 33 of 33
  1. #21
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    CJSL,

    he won't need that loop at all, but you're correct about arrays being zero based.

    Phil

  2. #22
    constden is offline Member
    Join Date
    Apr 2009
    Posts
    18
    Rep Power
    0

    Default

    I still have work to do on my code, but I didn't want to abuse of the kindness of you guys. Here is the code though, you can still help me if you would like :).

    import java.util.Scanner;

    public class ResreveR{

    public static void main (String[] args){


    int x , lgth;
    String [] words , sentence;
    String input , phrase, reverse;
    char period;
    Scanner scan1 = new Scanner(System.in);
    input = scan1.nextLine();
    Scanner scan2 = new Scanner(input);
    words = new String[20];



    System.out.println("Type a sentence");


    input = input.toLowerCase();


    lgth = input.length();
    period = input.charAt(lgth);
    sentence = input.split(".");
    phrase = sentence[0];


    for (x = 1 ; x <= 20 ; x++)
    words[x]= phrase.split("\\s");
    while (scan2.hasNext())

    for (x = 20; x >= 0; x--)
    if (words[x] != null)
    reverse = words[x] + " " + String.valueOf(period);
    reverse = reverse.substring(0,1).toUpperCase() + reverse.substring(1);
    System.out.print(reverse);
    Last edited by constden; 04-23-2009 at 03:00 PM. Reason: I accidentally deleted the top sentence while pasting my code

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

    Default

    OK, I see that you have not yet made changes to your application based on our recommendations. I suggest that you give it a try first, and then repost your code, and let us know how it is working (if any errors, post those as well). Again, please use code tags when posting code to this forum as without them, your code is almost unreadable. My previous post will show you how to use tags. Best of luck.

  4. #24
    constden is offline Member
    Join Date
    Apr 2009
    Posts
    18
    Rep Power
    0

    Default

    The instructions were to make a for loop that will take the words of input one at a time and store them in a String array. the loop should continue until there are 20 words stored, or scan2 does not have any more words to read.
    so how would I write the for loop for that?

  5. #25
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    ah, thnx fubarable... yes the OP doesn't need that loop... no wonder nobody commented on it. I was just looking at the errors... not the logic.
    constden... it appears that you haven't applied the suggested corrections. Until you do, there's not much we can do to further help you.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #26
    constden is offline Member
    Join Date
    Apr 2009
    Posts
    18
    Rep Power
    0

    Default

    I'm pretty sure I have implemented all of the suggestions into my code. This is what I have now:

    Java Code:
     
    import java.util.Scanner;
    
        public class R{
       
           public static void main (String[] args){
          
    
             int x , lgth;
             String [] words , sentence;
             String input , phrase, reverse, reversed, capital;
             char period;
             Scanner scan1 = new Scanner(System.in);
             input = scan1.nextLine();
             Scanner scan2 = new Scanner(input);
          	words = new String[20];
    
          	
    
             System.out.println("Type a sentence");
          
             input = input.toLowerCase();
          	     
             lgth = input.length();
             period = input.charAt(lgth);
             sentence = input.split(".");
             phrase = sentence[0];
          	
    
             for (x = 0 ; x < words.length ; x++) 
             words = phrase.split("\\s");
             while (scan2.hasNext())
             
    
             for (x = words.length ; x >= 0; x--)
             reverse = words[x] + " " + String.valueOf(period);
             capital = reverse.substring(0,1).toUpperCase();
             reversed= capital + reverse.substring(1);
             System.out.print(reversed);
          
              } 
       
       }

  7. #27
    corlettk is offline Member
    Join Date
    Apr 2009
    Location
    Brisbane
    Posts
    86
    Rep Power
    0

    Default

    My advise is to step away from the keyboard... Go sit down somewhere else with a pen and paper and try to define the steps you need to go through to achieve this overall effect.

    To get you started:
    1. Read a sentence from the user.
    2. split the sentence into words.
    3. ...

    Once you what you're trying to do you can proceed to workout exactly how to achieve each of those steps.

    I'd also suggest that you are completely clueless about very basic java syntax... this information is readily available in the java tutorials, and nobody is overly keen on holding your hand whilst you struggle through trying to work-out what java syntax is by trial and error... it's just painful to watch, let alone do. You might also find just looking-at the well-formatted, valid java code in the tutorials a profitable exercise... even if the import of the code is beyond your current level of understanding... So please quit bashing your head against the brick wall and go RTFM:
    java.sun.com/docs/books/tutorial/

    Cheers. Keith.

  8. #28
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    you say that you can't compile your code on your machine for some reason. what is this reason? are you having trouble setting up the jdk, or are you not allowed to install it for whatever reason? it's kinda a nuisance to not be able to check if code can compile, especially when you're starting out and prone to syntax errors

  9. #29
    corlettk is offline Member
    Join Date
    Apr 2009
    Location
    Brisbane
    Posts
    86
    Rep Power
    0

    Default

    Quote Originally Posted by emceenugget View Post
    you say that you can't compile your code ...
    I interpreted that statement as: My code doesn't compile and I'm not going to post the exact compiler error messages, because that would be useful... and we can't have that now can we.

    I'm not just old and cynical, I'm ugly to boot!

  10. #30
    constden is offline Member
    Join Date
    Apr 2009
    Posts
    18
    Rep Power
    0

    Default

    I meant that the computer I'm working with needs an authorization password to allow me to download a program that would let me compile and run my code. :(

  11. #31
    constden is offline Member
    Join Date
    Apr 2009
    Posts
    18
    Rep Power
    0

    Default

    I've been working on my code and asking questions everywhere, and I have almost been able to get it to do what I need it to do. The only problem is that nothing that I've tried has been able to print out exactly what I want.
    I need the printed out sentence to be capitalized and for it to have a period at the end. But the code that I have does not do that. :(
    How can I fix my code?
    This is what I have now:
    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();
                }}
               
             // Print out the sentence in reverse order.
             for (x = 19 ; x >= 0; x--){
                if (words[x] != null) 
                 reverse = " " + words[x];
    	System.out.print(reverse);
             }
             reverse = reverse.substring(0,1).toUpperCase() + reverse.substring(1);
             System.out.print("\n" + reverse + "."); 
    
      } // End of method main.
       
       } // End of class RR.

  12. #32
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Ok... if your trying to make it reverse the letters and make the capitalization correct, you'll also need to change the last letter to lower case using words[words.length - 1] = words[words.length - 1].toLowerCase(). No need for substrings, as the entire word should be lower case.

    Fubarable, how do you manage to put code tags in your messages without them showing up as code boxes? Would come in handy :D

    Edit: Dang, I'm slow too

    Edit number 2: Hmm... you put all the input into lower case at the very beginning... guess this was a somewhat useless post. Still want to know about the code tags though
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  13. #33
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Got it... your last bit of code should be
    Java Code:
    for (x = 19 ; x >= 0; x--){
                 if (words[x] != null) 
                 reverse [b]+= words[x] + " "[/b];//moved space to end to align the text properly
                 }
             reverse = reverse.substring(0,1).toUpperCase() +              
             reverse.substring(1, [b]reverse.length() - 1[/b]);//remove extra space at the end of the line
             System.out.print("\n" + reverse + ".");
    Works like a charm for me. Now try making it capitalize the word I; It currently makes I lower case if you start the sentence with it.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 03:03 AM
  2. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  3. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  4. <core:forEach var="" begin="+<%=j%>+">???
    By freddieMaize in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-27-2008, 01:20 AM
  5. "Jumble" or "Scramble" Program
    By Shadow22202 in forum Java Applets
    Replies: 8
    Last Post: 04-30-2008, 03:42 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
  •