Results 21 to 33 of 33
Thread: "parse error at or before "}""
- 04-23-2009, 02:48 PM #21
- 04-23-2009, 02:50 PM #22
Member
- Join Date
- Apr 2009
- Posts
- 18
- Rep Power
- 0
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
-
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.
- 04-23-2009, 03:13 PM #24
Member
- Join Date
- Apr 2009
- Posts
- 18
- Rep Power
- 0
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?
- 04-23-2009, 03:18 PM #25
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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 04-23-2009, 03:52 PM #26
Member
- Join Date
- Apr 2009
- Posts
- 18
- Rep Power
- 0
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); } }
- 04-23-2009, 04:39 PM #27
Member
- Join Date
- Apr 2009
- Location
- Brisbane
- Posts
- 86
- Rep Power
- 0
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.
- 04-23-2009, 06:15 PM #28
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
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
- 04-23-2009, 06:26 PM #29
Member
- Join Date
- Apr 2009
- Location
- Brisbane
- Posts
- 86
- Rep Power
- 0
- 04-25-2009, 07:27 PM #30
Member
- Join Date
- Apr 2009
- Posts
- 18
- Rep Power
- 0
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. :(
- 04-25-2009, 07:35 PM #31
Member
- Join Date
- Apr 2009
- Posts
- 18
- Rep Power
- 0
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.
- 04-25-2009, 09:38 PM #32
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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 thoughIf 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!
- 04-25-2009, 09:49 PM #33
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Got it... your last bit of code should be
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.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 + ".");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!
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