Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: Does this work?

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Does this work?

    I'm new to java programming and I have been reading a java how to book. At the end of each chapters there are challenges and one of the challenges was to:
    "Write an application that reads in a user's sentence of unknown, possibly mixed upper and lowercase letters, capitalizes the first letter of the sentence, makes all other letters lowercase, and makes sure there is a period at the end of the sentence."
    Can you tell me if this code fits the description above?


    /*
    * MGramChecker
    */

    import java.io.*;
    public class MGramChecker {
    public static void main(String args[]){
    String userSent = " ";
    String corrSent = " ";


    read = BufferedReader(new InputStreamReader(System));
    System.out.print("What's your sentence you need corrected?");
    try {
    userSent = reader.readLine();
    }
    catch (IOException ioe) {
    System.out.println("I/O Exception Occured");
    }
    corrSent = userSent.toUpperCase(charAt(0)) + userSent.toLowerCase(charAt(1-....));
    //See the line above. Its suppose to take the user's sentence. Then go to the first character and put it
    // in upper case. Then its suppose to take the rest of the sentence and put it in lower case.

    System.out.println("Here is your corrected sentence:" + corrSent + ".");
    System.out.println("Your Welcome");
    }

    }


    Obviously I'm not going to color those two lines red, but that's really what I have questions with. Thanks for the help.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    1,069
    Thanks
    7
    Thanked 215 Times in 191 Posts

    Default Re: Does this work?

    The best - in fact the only - way to see "if it works" is to test the program and see if it gives the correct output.

    Try and cover as many possibilities as you can with your test cases: all upper case letters, all lower case letters, an empty string (if you allow that), strings ending with a period, strings not ending with a period, ...

    -----

    One small point that probably won't be revealed with testing is that you catch an exception, but then carry on as if nothing was wrong. Also you print a fairly "plain" message. That's OK for the end user perhaps (although they're much more impressed with "Error code 4665. If the problem persists please see your system administrator") - but while you develop the program you probably ought to print out the whole stack trace:

    catch(IOException ioe) {
        ioe.printStackTrace();
        System.exit(1);
    }

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Does this work?

    So I found an online compiler and when I tried to run the program it said "illegal start of expression." It says that occurred on line 20 column 76 which is:
    corrSent = userSent.toUpperCase(charAt(0)) + userSent.toLowerCase(charAt(1-....));
    I put that in there so it would go from the second character through "...." or the rest of the sentence, but is "...." even code? If not what could I put there that would tell the machine to skip the first character and make every other letter lower case?
    Last edited by marmanq; February 18th, 2012 at 07:47 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,299
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Does this work?

    Can you get the full text of the error message? If so, post it.
    Does it have a line number or show the source line with the problem?

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Does this work?

    Yeah, I just added that to my response above. Post #3. It was line 20 and column 76. Thanks for the help.
    Last edited by marmanq; February 18th, 2012 at 07:52 PM.

  6. #6
    Junior Member
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Does this work?

    So I changed my code to:

    /*
    * MGramChecker
    */

    import java.io.*;
    public class MGramChecker {
    public static void main(String args[]){
    String userSent = " ";
    String corrSent = " ";
    int numLetters;

    read = BufferedReader(new InputStreamReader(System));
    System.out.print("What's your sentence you need corrected?");
    try {
    userSent = reader.readLine();
    }
    catch (IOException ioe) {
    System.out.println("I/O Exception Occured");
    }
    corrSent = userSent.toUpperCase(charAt(0)) + userSent.toLowerCase(charAt(1-int numLetters));
    //See the line above. Its suppose to take the user's sentence. Then go to the first character and put it
    // in upper case. Then its suppose to take the rest of the sentence and put it in lower case.
    System.out.println("Here is your corrected sentence:" + corrSent + ".");
    System.out.println("Your Welcome");
    }

    }


    But now I get 2 errors:
    Program.java LINE: 20 COL: 80 '.class' expected
    Error Program.java LINE: 20 COL: 91 ';' expected

    How do I get rid of those?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,299
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Does this work?

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting
    Your unformatted code is very hard to read.

    Where is line 20?

  8. #8
    Junior Member
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Does this work?

    /*
    * MGramChecker
    */
     
    import java.io.*;
    public class MGramChecker {
    public static void main(String args[]){
    String userSent = " ";
    String corrSent = " ";
    int numLetters;
     
    read = BufferedReader(new InputStreamReader(System));
    System.out.print("What's your sentence you need corrected?");
    try {
    userSent = reader.readLine();
    }
    catch (IOException ioe) {
    System.out.println("I/O Exception Occured");
    }
    corrSent = userSent.toUpperCase(charAt(0)) + userSent.toLowerCase(charAt(1-int numLetters));
    //Line 20 is the line above. See the line above. Its suppose to take the user's sentence. 
    //Then go to the first character and put it in upper case. Then its suppose to take 
    //the rest of the sentence and put it in lower case.
    System.out.println("Here is your corrected sentence:" + corrSent + ".");
    System.out.println("Your Welcome");
    }
     
    }

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,299
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Does this work?

    int is a keyword and it looks out of place on line 20.

  10. #10
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Does this work?

    And is this the whole code? Or there is something else? If this is the all, then you are missing a lot of things and compiler must generate bunch of errors.

  11. #11
    Junior Member
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Does this work?

    so can I just remove int and leave numLetters or do I have to add something else? Also this is all of my code, but I only get two errors when compiling:
    Program.java LINE: 20 COL: 80 '.class' expected
    Error Program.java LINE: 20 COL: 91 ';' expected

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,299
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Does this work?

    What value do you want to pass to the charAt() method here?
    userSent.toLowerCase(charAt(1-int numLetters));

  13. #13
    Junior Member
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Does this work?

    I would like it to skip the first character and make every other character in the sentence lowercase. That's why I put 1-int numLetters. I was hoping that would go from 1- then insert the number of characters in the sentence, so the compiler would know how many to make lowercase.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,299
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Does this work?

    What value do you want in the charAt() method call?
    It must be a value from 0 to the length of the String -1.
    The expression: (1 - XXX) will be 0 when XXX is 1 and it will be less than zero when XXX is greater than 1.
    The charAt() method requires a value >= 0.
    so the compiler would know
    You must specify exactly the value for the charAt() method. The compiler does NOT know what you want to do.
    You must tell it exactly what you want to do.

  15. #15
    Junior Member
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Does this work?

    I'm sorry, i'm having trouble understanding. The program is suppose to allow the user to type in a random sentence of any length and then its suppose to make all the letters (not including the first) lower case. that's why I put int numletters.
    So are you saying that if I put charAt (1-XXX) it will make all the letters, except the first, lower case?

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,299
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Does this work?

    charAt (1-XXX) was an example of code that I used to explain how to use the method. It is not for you to use in your code.

    You need to look at the letters one at a time (in a loop??) and test/change them as you want the program to work.

    Look at using the substring method to get the parts of the String that you need to handle.
    How many parts of the String do you need to work one? What do you want to do to each part?

  17. #17
    Junior Member
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Does this work?

    So I just came up with a new idea/question. Could I make a new string called String userSentLength and tell the compiler for that string to read the user's random sentence for the length. Then I could put charAt(1-userSentLength) ?

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,299
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Does this work?

    charAt(1-userSentLength)
    What do you expect that method call to do?
    You need to read the API doc for that method to see what it does!!!
    Java Platform SE 6

Similar Threads

  1. I cant work it out!!!
    By tuts73 in forum What's Wrong With My Code?
    Replies: 25
    Last Post: January 18th, 2012, 10:16 AM
  2. Why doesn't this work?
    By mailman in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 9th, 2012, 11:19 PM
  3. Do you work out?
    By thesolo in forum Totally Off Topic
    Replies: 4
    Last Post: October 24th, 2011, 09:03 AM
  4. Why does this not work?
    By Spidey1980 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 12th, 2011, 10:47 AM
  5. My lines won't work!!!
    By The Mewzytion in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 2nd, 2010, 11:24 AM