Results 1 to 20 of 33
Thread: "parse error at or before "}""
- 04-23-2009, 11:06 AM #1
Member
- Join Date
- Apr 2009
- Posts
- 18
- Rep Power
- 0
"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] + " " + ".");
}
}
}
- 04-23-2009, 11:15 AM #2
Remove the "do", that makes no sense at all. But don't panic, you'll get loads of other errors.
- 04-23-2009, 11:17 AM #3
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
What is the "do" doing there? Did you want a do/while loop? If so, where's the while? If not, why the do?Java Code:if (words[x] != null) do {System.out.print(words[x] + " " + "."); } }Last edited by masijade; 04-23-2009 at 11:18 AM. Reason: Edit: Grrrrrrrr, a slow bastid, I am!
- 04-23-2009, 11:26 AM #4
Member
- Join Date
- Apr 2009
- Posts
- 18
- Rep Power
- 0
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?
- 04-23-2009, 11:38 AM #5
Java Code:String s = "blabla"; s = s.substring(0,1).toUpperCase() + s.substring(1);
- 04-23-2009, 11:49 AM #6
Member
- Join Date
- Apr 2009
- Posts
- 18
- Rep Power
- 0
Thank you so much.
- 04-23-2009, 11:54 AM #7
You're welcome
- 04-23-2009, 12:27 PM #8
Member
- Join Date
- Apr 2009
- Posts
- 18
- Rep Power
- 0
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"?
- 04-23-2009, 12:37 PM #9
That means it's unknown, e.g. you're using index, but that's never declared.
- 04-23-2009, 01:20 PM #10
Member
- Join Date
- Apr 2009
- Posts
- 18
- Rep Power
- 0
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. :(
- 04-23-2009, 01:22 PM #11
ooohh the Scanner's hasNext() , then I suggest you call that.
orJava Code:scan1.hasNext();
Java Code:scan2.hasNext();
- 04-23-2009, 01:33 PM #12
And maybe you want to read this basics tutorial again.
- 04-23-2009, 02:00 PM #13
Member
- Join Date
- Apr 2009
- Posts
- 18
- Rep Power
- 0
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.
- 04-23-2009, 02:05 PM #14
split returns a String[], but you're trying to insert the array into a string:
this would work:Java Code:words[x]= phrase.split("\\s");
Java Code:words= phrase.split("\\s");
-
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:
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.Java Code:String[] words = phrase.split("\\s"); // or what ever regex is required here
edit: dang, too darn slow!Last edited by Fubarable; 04-23-2009 at 02:10 PM.
- 04-23-2009, 02:16 PM #16
Uhm constden, what exactly is your program supposed to do? I cannot really figure out what you're trying to do.
- 04-23-2009, 02:19 PM #17
Member
- Join Date
- Apr 2009
- Posts
- 18
- Rep Power
- 0
Thank you for being patient with me. Thank you so very much. You have helped me a great deal.
- 04-23-2009, 02:32 PM #18
Member
- Join Date
- Apr 2009
- Posts
- 18
- Rep Power
- 0
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."
-
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 [code] above your code block and the tag [/code] below the block like so:
Java Code:[code] // your code block goes here [/code]
- 04-23-2009, 02:42 PM #20
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 (x = [COLOR="Red"][B]1[/B][/COLOR] ; x <= 20 ; x++)
Luck,Java Code:for (int i = 0 ; i < array.length ; i++)
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
Similar Threads
-
Java, Military Format using "/" and "%" Operator!!
By sk8rsam77 in forum New To JavaReplies: 11Last Post: 02-26-2010, 03:03 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM -
<core:forEach var="" begin="+<%=j%>+">???
By freddieMaize in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 09-27-2008, 01:20 AM -
"Jumble" or "Scramble" Program
By Shadow22202 in forum Java AppletsReplies: 8Last Post: 04-30-2008, 03:42 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks