Results 1 to 8 of 8
Thread: Please help me on this
- 07-13-2009, 02:04 AM #1
Please help me on this
:confused:Could you please give me a code or at least a clue on how to make these:
A java program that will determine if the input value is a vowel, consonant, positive, negative or special characters.
Its strict that I will be using a JOptionPane dialog box and not a console or a GUI.
Ive tried using JOptionPane and yet i get an error message about the conflicts between a string and an integer? How will i resolve this?
And also
A java program that accepts a number and output is equivalent in words.
For example: If I input 100 then the output would be One Hundred.
It is said that its easier if i use the switch/case.But i dont know how.
Thanks for those who will participate.;)
-
1) What have you already done so far?
2) What specifically do you have a question about? Please note that specific questions usually receive specific helpful answers while general questions will usually get referrals to the tutorials.
Having said that, you may wish to have a look at the Sun Java tutorials which will show you how to create Java programs, how to get input, and how to use many other features of the Java language.
Best of luck
- 07-13-2009, 04:41 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
@arnelcolar, this is a forum? Do you know the idea of a forum? Please pay attention a lot on what Fuburable says.
- 07-13-2009, 04:19 PM #4
So sorry..
Oh! im sorry I was in a hurry making this. I'll just post what i had done so far on my first question. Thanks by the way.
- 07-15-2009, 06:29 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Ask your question more clearly, and put an effort first. Definitely we'll help you. :)
- 07-16-2009, 12:57 PM #6
Ok here's my first attempt
It's a java program to determine if the input value is a vowel,consonant,positive number, negative number or special characters.Java Code:import java.io.*; public class CharacterVaidator { public static void main(String args[]) { String validator; int validatorN; BufferedReader reader; reader = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a character to validate"); try { validator = reader.readLine(); if (validator.equals("a") || validator.equals("e") || validator.equals("i") || validator.equals("o") || validator.equals("u") || validator.equals("A") || validator.equals("E") || validator.equals("I") || validator.equals("O")|| validator.equals("U")){ System.out.println("The character" + validator + "is a vowel"); System.exit(0); } else if (validator.equals("b") || validator.equals("c") || validator.equals("d") || validator.equals("f") || validator.equals("g") || validator.equals("h") || validator.equals("j") || validator.equals("k") || validator.equals("l")|| validator.equals("m")|| validator.equals("n") || validator.equals("o") || validator.equals("p") || validator.equals("q")|| validator.equals("r")|| validator.equals("s") || validator.equals("t") || validator.equals("u") || validator.equals("v")|| validator.equals("w")|| validator.equals("x")|| validator.equals("y") || validator.equals("z") || validator.equals("B") || validator.equals("C") || validator.equals("D") || validator.equals("D") || validator.equals("G") || validator.equals("H") || validator.equals("J") || validator.equals("K") || validator.equals("L")|| validator.equals("M")|| validator.equals("N") || validator.equals("O") || validator.equals("P") || validator.equals("Q")|| validator.equals("R")|| validator.equals("S") || validator.equals("T") || validator.equals("U") || validator.equals("V")|| validator.equals("W")|| validator.equals("X")|| validator.equals("Y") || validator.equals("Z") ) { System.out.println("The character" + validator + "is a consonant"); System.exit(0); } /* if (validator.equals(validator)) { System.out.println("The character " + validator + "is a special character"); }*/ else { validatorN = Integer.parseInt(validator); if (validatorN >= 0) { System.out.println("The character " + validatorN + " is a positive number"); } else if (validatorN < 0) { System.out.println("The character " + validatorN + " is a negative number"); } } } catch (IOException ioe) { System.out.println("Error!"); } } }
I still have problems though.
(1):confused:I don't know where on the part of my code will I put a statement that tells if the user inputs a special character.
(2) Am I doing things right or am I coding too much? I mean, do you have any suggestions that could make this shorter but the logic is still there? Did I entered any unnecessary code that made this too long?
Again, thanks for those who'll help :)
- 07-16-2009, 07:40 PM #7
Member
- Join Date
- Jul 2009
- Posts
- 5
- Rep Power
- 0
Hi,
Here are some suggestions which might be helpful.
1. if you wanna keep the way in which you've written these codes,
a. make sure only the first character in the sequence is gonna be manipulated
you getta use trim() - String and substring(0,1) - String
b. get rid of redundant codes
to use toUpperCase() - String
c. exit(0) is not neccessary
just remove them or use the statement of "return;"
d. when using parseXXX for primitive dataType, the parsing codes are usually supposed to be surrounded by try-catch block
2. Accturally, there are a lot of alternative solutions to your problem
for example,
a. use 3 sets containing Vowels, Consonants and numbers
check out which set the target character the user just inputs is in
(notice that negative number consists of at least 2 characters including the sign)
b. use regular expression
c. use ascii code value in Dec or Hex to check
Good luck!
- 07-17-2009, 02:12 PM #8
Thanks! It's very informative.
The reason why my code is messy is that our instructor only thought us the basics like if/else, switch/case, bufferedreader,a part of javax.swing(well, just JOptionPane). And to give us that kind of problem with just little knowledge is like giving us the tools that we are not very familiar of. Thanks anyways. Keep up the good work. :):):)


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks