• Post Reply Bookmark Topic Watch Topic
  • New Topic

Excluding Numbers  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still working on my determining vowel or consonant. All is good, except i want it to say "Invalid Input" When anything but a letter is entered. No idea where to start

 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no offense, but...

blargh that is ugly code.

"String num" is confusing. Is it a string? is it a number?

if you are going to use the String class, then check out its API...maybe you can find something that will IgnoreCase, which should simplify things a bit...

Something else to consider...since you are only dealing with a single letter at a time, perhaps there could be something better than a string...maybe something that lets you deal with a single character at a time, and then instead of doing a bunch of .equals, you do some inequalities...

(i'm being a little vague on purpose, to challenge you to think a bit).
 
Tommy Walker
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:no offense, but...

blargh that is ugly code.

"String num" is confusing. Is it a string? is it a number?

if you are going to use the String class, then check out its API...maybe you can find something that will IgnoreCase, which should simplify things a bit...

Something else to consider...since you are only dealing with a single letter at a time, perhaps there could be something better than a string...maybe something that lets you deal with a single character at a time, and then instead of doing a bunch of .equals, you do some inequalities...

(i'm being a little vague on purpose, to challenge you to think a bit).

As far as the ugly code, i am going to clean it up once i get it working how i'd like.
Maybe you are being a little too vague, i have no idea what i am doing.
I visited the link you sent me and tried "String vowel = "aAeEiIoOuU";" then set num = vowel and this did not work. I may have misunderstood what the link is telling me to do
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want to use good names so that you don't confuse yourself. Ever heard of the Stroop Effect? The same kind of thing is going on when you write:

As with trying to say the colors rather than the words in the Stroop Effect demonstration, poorly chosen names in your program hobble the person reading the code and places a larger cognitive load on him/her. So make the effort to use good names up front rather than wait until you get everything working. Besides, when you get everything working, there's more motivation to move on the the next program rather than clean up the working code. If you're serious about learning how to write good programs, keeping your code clean and understandable is a good habit to develop now rather than later and well chosen names that match their purpose and intent are one of the hallmarks of good, clean code.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you ignore the case of the letter such that "A" will be seen as the same as "a" and vice versa, then you can reduce the number of terms in your if-statement conditional by half. There is a method in the String API that allows you to compare strings while ignoring the case. Other methods that could work as well are toLowerCase and toUpperCase. Experiment with these methods to see what they do.
 
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The if and ors would be a good place to use a switch statement instead.
Check for the vowels and let them all fall through to printing "Is a vowel" followed by break,
and the default statement would print "Is a consonant"
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good tip, Guillermo. A relevant example of the switch statement can be found here: http://docs.oracle.com/javase/7/docs/technotes/guides/language/strings-switch.html
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote: . . . blargh that is ugly code. . . ..
It is slightly better now I have broken your disastrously long line, which makes the code difficult to read.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tommy Walker wrote:
As far as the ugly code, i am going to clean it up once i get it working how i'd like.
Maybe you are being a little too vague, i have no idea what i am doing.

It is probably a good idea to get in the habit of writing clean code all the time, rather than "going back later". You will be amazed at how hard it is to find the time later to clean it up.

next...my hint was that "IgnoreCase" was written as one word, and with weird capitalization. If you search the String API page I linked to, you will see that the String class has an equals method, and just below it is an "equalsIgnoreCase" method. You could use .equalsIgnoreCase("A") and it would match "a" or "A". That would eliminate half of your tests, and be a little clearer what you want.
 
Tommy Walker
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
Tommy Walker wrote:
As far as the ugly code, i am going to clean it up once i get it working how i'd like.
Maybe you are being a little too vague, i have no idea what i am doing.

It is probably a good idea to get in the habit of writing clean code all the time, rather than "going back later". You will be amazed at how hard it is to find the time later to clean it up.

next...my hint was that "IgnoreCase" was written as one word, and with weird capitalization. If you search the String API page I linked to, you will see that the String class has an equals method, and just below it is an "equalsIgnoreCase" method. You could use .equalsIgnoreCase("A") and it would match "a" or "A". That would eliminate half of your tests, and be a little clearer what you want.

I've gotten as far as the ignore caps. But i am still stuck with numbers and symbols showing as consonants
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show us what you have and where you're stuck.
 
Tommy Walker
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Show us what you have and where you're stuck.




I need another statement that says if anything but letters are entered it prints "Invalid entry" or something like that. Problem is, i have no idea how to do that, i've been trying setting a String to every other letter in the alphabet but thats too complicated, there has to be a better way
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Character class has an isLetter() method. The String class has a way to get to individual characters in a string - see the String.charAt() method.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to program it yourself and not use Character.isLetter(), then the char type has these characteristic that you can rely on:

'A' < 'B' < 'C' ... < 'Z'
'a' < 'b' < 'c' ... < 'z'

There are no non-letter values within the ranges 'A' to 'Z' and 'a' to 'z' and the codes in these ranges are consecutive integers.
There are some codes in between these two ranges, however, that represent non-letter values.

See the ASCII/Unicode chart for details.

If you use toUpperCase or toLowerCase, you can avoid having to check between these two ranges and just use one of them to check if a char value represents a letter or not.
 
Tommy Walker
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:The Character class has an isLetter() method. The String class has a way to get to individual characters in a string - see the String.charAt() method.

I am a novice, i don;t mean to belittle your suggestions, but i still do not fully understand what these links are telling me. I understand that it is defining a character, but can the same work for my string i have already established?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best way to understand them is to try experimenting with them to see what they do. Or you could google for "Examples of java String.charAt()" and "examples of java Character.isLetter()" Once you understand what these do, it's just a matter of figuring out how to put these two together to see if the first character in your string is a letter.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!