Results 1 to 3 of 3

Thread: Char troubles

  1. #1
    diggdude is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Default Char troubles

    Hello, i'm pretty new to the Java programming language and it seems I have run into a brick wall with the char type.

    I have written a program which translates a simple string of numbers into morse code. However, that part works perfectly, but my input validation seems to be incorrect in some way. I am constantly getting this message at compile time:

    Java Code:
    Question_1.java:22: char cannot be dereferenced
            if (input.charAt(i).isDigit() == false) {
                               ^
    To put this in better context, here is the rest of the code:

    Java Code:
    import javax.swing.JOptionPane;
    
    public class Question_1 {
    
        public static void main(String[] arg) {
        //Get the input
            String input = JOptionPane.showInputDialog("Enter a series of numbers: ");
            //Translate input and get rid of white space to the left and right of the string
            input = input.replaceAll("0"," -----");
            input = input.replaceAll("1"," .----");
            input = input.replaceAll("2"," ..---");
            input = input.replaceAll("3"," ...--");
            input = input.replaceAll("4"," ....-");
            input = input.replaceAll("5"," .....");
            input = input.replaceAll("6"," -....");
            input = input.replaceAll("7"," --...");
            input = input.replaceAll("8"," ---..");
            input = input.replaceAll("9"," ----.");
            input = input.trim();
            //Validation
            for (int i = 0; i <= input.length(); i++){
            if (input.charAt(i).isDigit() == false) {
                    JOptionPane.showMessageDialog(null,("Non numeric   characters are not a valid input"),"Error",
                    JOptionPane.INFORMATION_MESSAGE);
                    break;    
                }
            }
            //Output the results
            JOptionPane.showMessageDialog(null,("The morse code is: "+input),"Morse Code",
                    JOptionPane.INFORMATION_MESSAGE);
        }
    }
    Any help with this would be greatly appreciated, thanks in advance

    Edit:

    Something I forgot to add; the validation is checking for non numerical values in the string by iterating through each character, if found it will ouput an error message.
    Last edited by diggdude; 11-10-2009 at 03:19 PM.

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

    Default

    Java Code:
    if(!Character.isDigit(input.charAt(0))){
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    diggdude is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by PhHein View Post
    Java Code:
    if(!Character.isDigit(input.charAt(0))){
    Ah, so that's how you use that method. Thank you very much, solved.

Similar Threads

  1. Integer troubles
    By Tb0h in forum New To Java
    Replies: 11
    Last Post: 08-13-2009, 04:56 PM
  2. Gif decoding/LZW troubles
    By hellochar in forum Advanced Java
    Replies: 2
    Last Post: 07-14-2009, 11:26 PM
  3. Image troubles
    By Theodoreb in forum New To Java
    Replies: 24
    Last Post: 07-14-2009, 12:41 AM
  4. subclass troubles
    By xf021209 in forum New To Java
    Replies: 12
    Last Post: 04-20-2009, 11:46 PM
  5. drawing char by char with Graphics
    By diggitydoggz in forum New To Java
    Replies: 5
    Last Post: 12-27-2008, 12:49 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
  •