Page 1 of 2 12 LastLast
Results 1 to 20 of 33
  1. #1
    constden is offline Member
    Join Date
    Apr 2009
    Posts
    18
    Rep Power
    0

    Question "parse error at or before "}""

    I don't know how to fix my code. I keep on getting a message saying ""parse error at or before "}"" :(

    I would also love you guys if you could please help me out with figuring out how it is that I can capitalize the first letter of the printed out message.

    import java.util.Scanner;

    public class R{
    public static void main (String[] args){

    int x , lgth;
    String [] words , sentence;
    String input , phrase ;
    Scanner scan1 = new Scanner(System.in);
    System.out.println("Type a sentence");
    input = scan1.nextLine();
    Scanner scan2 = new Scanner(input);

    index = index.replace("." , "\\s");
    index = index.toLowerCase();

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

    for (x = 20; x >= 0; x--){
    if (words[x] != null)
    do {System.out.print(words[x] + " " + ".");
    }

    }
    }

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Remove the "do", that makes no sense at all. But don't panic, you'll get loads of other errors.

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Java Code:
    if (words[x] != null)
      do {System.out.print(words[x] + " " + ".");
      }
    
    }
    What is the "do" doing there? Did you want a do/while loop? If so, where's the while? If not, why the do?
    Last edited by masijade; 04-23-2009 at 11:18 AM. Reason: Edit: Grrrrrrrr, a slow bastid, I am!

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

    Default

    lol thank you.
    "But don't panic, you'll get loads of other errors."
    lol, that is what i get from trying to write a code in a computer that does not let me run or really compile my code. I've been trying to complile it online with little help.
    But thanks, really. :)
    Do you have any idea of how I could capitalize the first letter of the printed out message?

  5. #5
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Java Code:
    String s = "blabla";
    s = s.substring(0,1).toUpperCase() + s.substring(1);

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

    Default

    Thank you so much.

  7. #7
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    You're welcome

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

    Default

    wow, I just fixed it how you said I should, and your are right, there are a whole bunch of errors. :/
    do you know what they mean by "cannot find symbol"?

  9. #9
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    That means it's unknown, e.g. you're using index, but that's never declared.

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

    Default

    i don't know how I am supposed to use the hasNext(). Why is it telling me it has not been declared if it is supposed to be a boolean method that tells you if there is any more to read already defined in the Scanner class. :(

  11. #11
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    ooohh the Scanner's hasNext() , then I suggest you call that.
    Java Code:
    scan1.hasNext();
    or
    Java Code:
    scan2.hasNext();

  12. #12
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    And maybe you want to read this basics tutorial again.

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

    Default

    I think I've figured out most of it thanks to you, but there is still one last thing that I don't know how to fix.
    I keep on getting "incompatible types found" in my first for loop where I'm trying to split my string into an array. I'm trying to make my loop continue until there are 20 words or there are no more words to read, but the technique I'm using to split does not seem to work.

  14. #14
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    split returns a String[], but you're trying to insert the array into a string:
    Java Code:
    words[x]= phrase.split("\\s");
    this would work:
    Java Code:
    words= phrase.split("\\s");

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

    Default

    Have a peek at the String#split(...) method in String's API. It doesn't work as you expect that it does. I usually do something like this:
    Java Code:
    String[] words = phrase.split("\\s"); 
      // or what ever regex is required here
    The key is that the result of a call to split is a complete array, not a String as your code currently tries to do.

    edit: dang, too darn slow!
    Last edited by Fubarable; 04-23-2009 at 02:10 PM.

  16. #16
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Uhm constden, what exactly is your program supposed to do? I cannot really figure out what you're trying to do.

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

    Default

    Thank you for being patient with me. Thank you so very much. You have helped me a great deal.

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

    Default

    I'm trying to reverse the order of the words in a sentence. For example, if the user writes "I do not know." The program will return "Know not do i."

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

    Default

    So what does your program look like now in its current state? Please don't forget to use indented code and code tags so that the forum software will show the indentations. to do this, place the tag [cod&#101;] above your code block and the tag [/cod&#101;] below the block like so:

    Java Code:
    [cod&#101;]
      //  your code block goes here
    [/cod&#101;]

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

    Default

    Java Code:
    for (x = [COLOR="Red"][B]1[/B][/COLOR] ; x <= 20 ; x++)
    Also, arrays start at index 0...not 1. Therefore, when filling an array with data in a loop, the limit should be the array length -1. For example:
    Java Code:
    for (int i = 0 ; i < array.length ; i++)
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Page 1 of 2 12 LastLast

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
  •