Results 1 to 6 of 6
Thread: Help with a compilation error
- 08-15-2011, 04:36 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 3
- Rep Power
- 0
Help with a compilation error
Hello,
I have put together the following code which is designed to calculate a translation cost for a sentence input by a user. However I am getting a compilation error details of which is attached. Can you solve the error for me as I don't understand it.
import javax.swing.JOptionPane;
public class TranslationCost1
{
public static void main(String [] args)
{
String userInput, result;
String words[];
int wordCount = 0;
userInput = JOptionPane.showInputDialog(null, "Enter a sentence to translate.");
words = userInput.split(' ');
//for(int index = 0; index < userInput.length(); index++)
//if (userInput.charAt(index) == ' ')
wordCount = words.length();
result = "Cost of translating this sense is" + ((wordCount)* .05);
JOptionPane.showMessageDialog(null, result, "Output",1);
}
}
- 08-15-2011, 04:44 PM #2
Did you check out the API? Where is the split() method that takes a char as an argument? Where is the length() method?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 08-15-2011, 04:54 PM #3
Member
- Join Date
- Aug 2011
- Posts
- 3
- Rep Power
- 0
- 08-15-2011, 05:13 PM #4
What didn't you understand? Googling "java API" would have brought you to this page: Java Platform SE 6
From there, you can look at the methods in String. Compare its methods to how you're using them.
You could also google "java array length" to figure out what's going on there. The compiler is already telling you what you're doing wrong.
Just because you're new to java doesn't mean you're going to be spoonfed an answer.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 08-15-2011, 05:17 PM #5
Member
- Join Date
- Aug 2011
- Posts
- 95
- Rep Power
- 0
In the String class, the split method accepts a String parameter/argument.
In Java, ' ' is a char, while " " is a single-character String. The two are different. Very different. It's important that you use the right one.
For the other problem...
'.length' on an array should not have the parenthesis. It works like a field reference, not like a method call.
- 08-15-2011, 05:20 PM #6
Member
- Join Date
- Aug 2011
- Posts
- 3
- Rep Power
- 0
So when it comes to the ' ' space character should I use " " instead? I understand your response better than the previous response. Thank you. sometimes people don't have the time to be reading piles of material to solve a minor error. I am practising on my own laptop for an exam so this is a help. Thank you.
Similar Threads
-
compilation error
By NoufalpRahman in forum Java ServletReplies: 0Last Post: 04-26-2011, 08:08 PM -
Classes, compilation error
By l flipboi l in forum New To JavaReplies: 2Last Post: 02-11-2011, 07:38 PM -
bean compilation error
By technical_helps@yahoo.com in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 07-29-2009, 11:21 PM -
JAVA compilation error in UNIX
By satish kumar in forum Advanced JavaReplies: 9Last Post: 08-08-2008, 07:36 AM -
compilation error with Jcreator
By Heather in forum JCreatorReplies: 2Last Post: 06-30-2007, 04:12 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks