Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By m_mccutcheon
  • 1 Post By KevinWorkman

Thread: Help with a compilation error

  1. #1
    m_mccutcheon is offline Member
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Default 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);
    }
    }
    Attached Thumbnails Attached Thumbnails Help with a compilation error-compilation-error-re-translationcost1.jpg  
    JeffGrigg likes this.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default

    Did you check out the API? Where is the split() method that takes a char as an argument? Where is the length() method?
    JeffGrigg likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    m_mccutcheon is offline Member
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    Did you check out the API? Where is the split() method that takes a char as an argument? Where is the length() method?
    I am sorry but I don't understand your reply. this thread is for people new to java and so this reply isn't very helpful to me as I am new to java. Could you possibly put your reply differently so that I understand it.

    thanks for replying though.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default

    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!

  5. #5
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    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.

  6. #6
    m_mccutcheon is offline Member
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    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.
    Quote Originally Posted by JeffGrigg View Post
    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.
    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

  1. compilation error
    By NoufalpRahman in forum Java Servlet
    Replies: 0
    Last Post: 04-26-2011, 08:08 PM
  2. Classes, compilation error
    By l flipboi l in forum New To Java
    Replies: 2
    Last Post: 02-11-2011, 07:38 PM
  3. bean compilation error
    By technical_helps@yahoo.com in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 07-29-2009, 11:21 PM
  4. JAVA compilation error in UNIX
    By satish kumar in forum Advanced Java
    Replies: 9
    Last Post: 08-08-2008, 07:36 AM
  5. compilation error with Jcreator
    By Heather in forum JCreator
    Replies: 2
    Last Post: 06-30-2007, 04:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •