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 9 of 9

Thread: New to Java having problem debugging my code.

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default New to Java having problem debugging my code.

    The code is supposed to capitalize the first letter of the word typed and reverse the sentence.

    I believe i messed up here "String firstchar = sentence.substring (lString+1,lSpace+2).toUpperCase();"

    I'm totally new to java and have no clue what could have went wrong :'(

    import java.util.Scanner;
    public class reverseSentence {
    public static void main ( String [] args) {
    String sentence, piece1,piece2;
    int lSpace;
    Scanner input = new Scanner (System.in);
     
    System.out.println ("Enter a sentence no punctuation");
    sentence = input.nextLine();
     
    lSpace = sentence.indexOf("");
    piece1 = sentence.substring(0,lSpace);
    piece2 = sentence.substring(lspace+2);
     
    String firstchar = sentence.substring (lString+1,lSpace+2).toUpperCase();
     
    system.out.println(firstChar + piece2 + " " + piece1);
     
     
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: New to Java having problem debugging my code.

    what could have went wrong
    Can you explain what is wrong?
    Post the program's output and add some comments explaining what is wrong and what the output should be.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New to Java having problem debugging my code.

    Enter a sentence no punctuation
    hello world
    Ello world
    Its supposed to switch the pieces and capitalize the first letter. The result is supposed to be
    World hello

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: New to Java having problem debugging my code.

    It looks like The current code strips off the first letter and capitalizes the second letter.

    What is the value returned by the indexOf() method? Is that what you want?
    If not, change the way indexOf() is used so it gives you the desired value.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New to Java having problem debugging my code.

    Is this a complete class? You seem to have not declared lString or firstChar anywhere. I run it on my IDE and it just throws an error due to this.
    Edit: You have declare firstChar but as firstchar. Watch out for this, I'll have a look through

    --- Update ---

    Does this make sense to you?
    import java.util.Scanner;
     
    public class reverseSentence {
     
        public static void main(String[] args) {
            String sentence, piece1, piece2;
            int lSpace;
     
            Scanner input = new Scanner(System.in);
     
            System.out.println("Enter a sentence no punctuation");
            sentence = input.nextLine();
     
            lSpace = sentence.indexOf(" ");
            //Selects the first word from 0 to first space
            piece1 = sentence.substring(0, lSpace);
            //Selects second word from space to string end
            piece2 = sentence.substring(lSpace + 1, sentence.length());
            //reverses the words and stores as ouput
            String output = piece2 + " " + piece1;
            //Get the first char of output as a String
            String firstChar = output.charAt(0) + "";
            //Make it uppercase
            firstChar = firstChar.toUpperCase();
            //Replace the first letter of output with the capitalised firstChar
            output = output.replace(output.charAt(0), firstChar.charAt(0));
            //Print it to console
            System.out.println(output);
        }
    }
    I hope this wasn't homework LOL

  6. #6
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New to Java having problem debugging my code.

    It wasn't homework it was my first day and we were copying the codes down and we had to copy the code.

    Apparently I copied it wrong or the professor wrote it wrong.

  7. #7
    Junior Member
    Join Date
    Jan 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New to Java having problem debugging my code.

    Oh right, yeah I'm currently in my 2nd year studying it, got my main exam in it Friday. Lecturers get code wrong all the time and my 1st year professor was awful at teaching Java, luckily I taught myself LOL. Good luck! its such a simple but powerful language once you get to grips with it. If you need any tips or questions feel free to message me ;-)

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: New to Java having problem debugging my code.

    I hope this wasn't homework LOL
    @rossc Please don't do the OPs work for him.
    See: http://www.javaprogrammingforums.com...n-feeding.html

    --- Update ---

    I hope this wasn't homework LOL
    @rossc Please don't do the OPs work for him.
    See: http://www.javaprogrammingforums.com...n-feeding.html

    --- Update ---

    I hope this wasn't homework LOL
    @rossc Please don't do the OPs work for him.
    See: http://www.javaprogrammingforums.com...n-feeding.html
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jan 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New to Java having problem debugging my code.

    Sorry, learnt my lesson

    --- Update ---

    Quote Originally Posted by Norm View Post
    @rossc Please don't do the OPs work for him.
    See: http://www.javaprogrammingforums.com...n-feeding.html

    --- Update ---


    @rossc Please don't do the OPs work for him.
    See: http://www.javaprogrammingforums.com...n-feeding.html

    --- Update ---


    @rossc Please don't do the OPs work for him.
    See: http://www.javaprogrammingforums.com...n-feeding.html
    Sorry, learnt my lesson

Similar Threads

  1. Error when debugging
    By Montrell79 in forum Java IDEs
    Replies: 0
    Last Post: March 14th, 2012, 06:07 PM
  2. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM
  3. Help with Error Debugging
    By bananasplitkids in forum Object Oriented Programming
    Replies: 3
    Last Post: March 9th, 2010, 10:16 AM
  4. Debugging
    By Altaf in forum Java IDEs
    Replies: 0
    Last Post: December 6th, 2009, 10:16 PM
  5. Debugging your program
    By helloworld922 in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: November 18th, 2009, 03:14 AM