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 7 of 7

Thread: help! messy code

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    17
    My Mood
    Confused
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default help! messy code

    hi, i would just like to ask, if it is possible to make my code looks clean? i suppose to let the user to press any key from the keyboard, determine if it is a vowel,consonant and the rest is unrecognize.. my code works,, but it's annoying to see it like this .. is there any simple and possible way to make it look good?

    here's my code..

     
     
    package experiment;
    import java.io.*;
    public class WhatDidIType {
     
        public static void main(String[]args)throws IOException{
            BufferedReader dataIn=new BufferedReader(new InputStreamReader(System.in));
     
            String in,yes;
     
            do{
            System.out.print("press any key from the keyboard:");
            String input=dataIn.readLine();
     
            if(input.equalsIgnoreCase("a") || input.equalsIgnoreCase("e")|| input.equalsIgnoreCase("i")
               || input.equalsIgnoreCase("o") || input.equalsIgnoreCase("u")){
                 in="vowel";
                   System.out.println(input+" is "+in);
            }
            else if(input.equalsIgnoreCase("b")|| input.equalsIgnoreCase("c")|| input.equalsIgnoreCase("d")||
                    input.equalsIgnoreCase("k")|| input.equalsIgnoreCase("f")|| input.equalsIgnoreCase("g")||
                    input.equalsIgnoreCase("h")|| input.equalsIgnoreCase("j")|| input.equalsIgnoreCase("l") ||
                    input.equalsIgnoreCase("m") || input.equalsIgnoreCase("n")
                    || input.equalsIgnoreCase("p") || input.equalsIgnoreCase("q") || input.equalsIgnoreCase("r")
                    || input.equalsIgnoreCase("s") || input.equalsIgnoreCase("t") || input.equalsIgnoreCase("v")
                    || input.equalsIgnoreCase("w") || input.equalsIgnoreCase("x") || input.equalsIgnoreCase("y")
                    || input.equalsIgnoreCase("z")){
     
                      in="consonant";
                        System.out.println(input+" is "+in);}
     
              else{
     
                     System.out.println("Unrecognized");}
     
                     System.out.print("Try again y/n: ");
                     yes=dataIn.readLine();}
     
                  while(yes.equalsIgnoreCase("y"));
                      System.out.println("Bye Bye!!"); }
                }

    i just want to know how to do it properly,,, any help/suggestions will help ^^,

    tnx in advance..
    Last edited by niecah; June 8th, 2010 at 02:23 AM.


  2. #2
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: help! messy code

    You could maybe have 2 arrays 1 of vowels and 1 of consonants and then check if the key pressed is in either.

    Or you could use the Character.isLetter() method:
    Character (Java 2 Platform SE v1.4.2)

    change input to a character and then say something like
    if(input.toLowerCase == 'a'|| etc) //I dont think theres a equalsIgnoreCase but I think this would work
    System.out.println("Vowel");
    else if(input.isLetter() == true)
    System.out.println("Consonant");
    else
    System.out.println("Unrecognised");

    Or something like that maybe use an array of vowels instead of comparing them all.

    Is that what you meant?
    Last edited by Faz; June 7th, 2010 at 07:24 AM.

  3. The Following User Says Thank You to Faz For This Useful Post:

    niecah (June 10th, 2010)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,592
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: help! messy code

    You could maybe have 2 arrays 1 of vowels and 1 of consonants and then check if the key pressed is in either
    Instead of an array, have a String of letters all the same case and change the case of the letter that was input to be the same case. Then you can use the String.indexOf() method to detect if input is in the array

  5. The Following User Says Thank You to Norm For This Useful Post:

    niecah (June 10th, 2010)

  6. #4
    Member
    Join Date
    May 2010
    Posts
    38
    Thanks
    1
    Thanked 8 Times in 7 Posts

    Default Re: help! messy code

    Wouldn't something like this work?

    String const = "bcdfghjklmnpqrstvwxyz";
              String vowel = "aeiou";
              if(vowel.indexOf(input.toLowerCase()) > -1)
                    System.out.println("vowel")
              else
                    System.out.println("Const");

  7. The Following User Says Thank You to Lord.Quackstar For This Useful Post:

    niecah (June 10th, 2010)

  8. #5
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    491
    Thanks
    8
    Thanked 47 Times in 45 Posts

    Default Re: help! messy code

    Considering the semantics of the problem, String#contains(...) is a better choice than String#indexOf(...) and has the added advantage of not requiring a secondary comparison with -1.

    db
    Last edited by Darryl.Burke; June 9th, 2010 at 01:56 PM.

  9. The Following User Says Thank You to Darryl.Burke For This Useful Post:

    niecah (June 10th, 2010)

  10. #6
    Member
    Join Date
    May 2010
    Posts
    38
    Thanks
    1
    Thanked 8 Times in 7 Posts

    Default Re: help! messy code

    Quote Originally Posted by Darryl.Burke View Post
    Considering the semantics of the problem, String#contains(...) is a better choice than String#indexOf(...) and has the added advantage of not requiring a secondary comparison with -1.

    db
    Yea I constantly forget about that. indexOf is a hangover from my Javascript days

  11. #7
    Junior Member
    Join Date
    Apr 2010
    Posts
    17
    My Mood
    Confused
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: help! messy code

    guys,, thank you very much for all of your suggestions!! ^^,
    im doing it again,, but getting errors,, :p but encountering errors is very challenging..
    im not yet familiar with some of your codes but i'll try my best to figure how it works..