Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Trouble with the use of isDigit and isLetter

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trouble with the use of isDigit and isLetter

    I am trying to figure out how to concat two strings if certain conditions are met or not met. Say I were to enter a string, and the string did not contain any letters. I would then prompt the program to add 007 at the end of the string, and if I were to include numbers in the string in the first place it would output what I entered in the first place. I have tried the following sequences below, but have not found anything that works properly.

    Say I entered John1... I want it to output John1.

    If I were to enter John... Then I would want it to output John007.

    Or if I were to enter Area51Games... Then I would want it to output Area51Games.

    Does anyone have any helpful advice to help me solve my problem, because isLetter, and isDigit just aren't working for me.


    //My code
    import javax.swing.JOptionPane;

    public class Word {
    public static void main(String[] args){

    String word = "";

    word = JOptionPane.showInputDialog("Enter a word: ");

    valid(word);

    }//end main method

    public static void valid(String input){
    String number = "007";
    String output = "";

    for(int i = 0; i < input.length(); i++){

    // else if(!Character.isLetter(output.charAt(i))){output+= number;break;}//end 007 else if

    // else if(Character.isLetter(output.charAt(i))){output+=n umber;break;}//end 007 else if


    // else if(!Character.isDigit(output.charAt(i))){output+=n umber;break;}//end 007 else if

    // else if(Character.isDigit(output.charAt(i))){output+=nu mber;break;}//end 007 else if


    // else if(!Character.isLetter(input.charAt(i))){output+=n umber;break;}//end 007 else if

    // else if(Character.isLetter(input.charAt(i))){output += number;break;}//end 007 else if


    // else if(!Character.isDigit(input.charAt(i))){output+=nu mber;break;}//end 007 else if

    // else if(Character.isDigit(input.charAt(i))){output+=num ber;break;}//end 007 else if

    }//end for loop

    JOptionPane.showMessageDialog(null,"The output is: " + output);
    }//end valid method
    }//end Word Class


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Trouble with the use of isDigit and isLetter

    Write a function that takes a String as a parameter and returns whether it contains any digits. Call that function by passing each String into it and taking the appropriate action.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by KevinWorkman View Post
    Write a function that takes a String as a parameter and returns whether it contains any digits. Call that function by passing each String into it and taking the appropriate action.
    an example?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Trouble with the use of isDigit and isLetter

    Quote Originally Posted by SeanEE89 View Post
    an example?
    What have you tried? Where are you stuck? What part of my suggestion did you not understand that requires an example?

    But here's a skeleton that should get you started:

    public boolean doesStringContainNumber(String str){
       //can I find a number? If so return true
     
       //if I can't find a number, return false
    }
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with the use of isDigit and isLetter

    I haven't had an opportunity to get to a computer to try anything out. I was just wondering if you'd be a able to provide an example.

    Wouldn't I be able to use a count to count how many letters are in the string, and then if the count doesn't match with the string.length() then I'd be able to concat the string with the "007"?

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Trouble with the use of isDigit and isLetter

    Quote Originally Posted by SeanEE89 View Post
    I haven't had an opportunity to get to a computer to try anything out. I was just wondering if you'd be a able to provide an example.

    Wouldn't I be able to use a count to count how many letters are in the string, and then if the count doesn't match with the string.length() then I'd be able to concat the string with the "007"?
    I suppose you could do it that way. It seems a lot easier to detect one digit than it is to compare the number of letters to the length. But go with whatever fits into your head the best.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. jTable trouble...
    By _lithium_ in forum AWT / Java Swing
    Replies: 3
    Last Post: March 6th, 2011, 07:05 PM
  2. trouble with GUI
    By MrHardRock in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 7th, 2011, 04:00 PM
  3. [SOLVED] array trouble
    By littlefuzed in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 13th, 2010, 08:56 PM
  4. Having trouble with a While statment
    By java0 in forum Loops & Control Statements
    Replies: 1
    Last Post: January 27th, 2010, 12:37 PM
  5. Having trouble with an if statement. please help!!
    By humdinger in forum Loops & Control Statements
    Replies: 11
    Last Post: January 21st, 2010, 02:49 PM