• Post Reply Bookmark Topic Watch Topic
  • New Topic

Vowel or Consonant?  RSS feed

 
Tommy Walker
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am new here. Currently a computer science major. I am trying to create a program for my class that requires the input of a letter and the output of whether or not it is a vowel or a consonant.
I am using eclipse, it is giving me no errors, but when entering the letter i get an error in the console


this is what i have

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carefully check the placement of your semicolons.

And, welcome to the Ranch.
 
Tommy Walker
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Carefully check the placement of your semicolons.

And, welcome to the Ranch.


Thanks for the welcome!

I don't see any misplaced semi colons, what am i missing?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look closer. Hint: particularly review the syntax of control statements.
 
Tommy Walker
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Look closer. Hint: particularly review the syntax of control statements.

the only thing i can think of is my semi colon after the if statement, i am not particularly experienced with writing code
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bingo
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pretend the if is Python; how would you indent it? What's the equivalent in Java?

Also, great article on user input here:
http://www.coderanch.com/how-to/java/UserInput

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. As a novice, you should never have an if statement without braces. That prevents errors such as this one.
 
Tommy Walker
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:P.S. As a novice, you should never have an if statement without braces. That prevents errors such as this one.


Well after removing the semi-colon i am still getting this error in the console after inputting 'a'.

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at w0571505_a2a.main(w0571505_a2a.java:18)
 
Tommy Walker
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Ishi wrote:Pretend the if is Python; how would you indent it? What's the equivalent in Java?

Also, great article on user input here:
http://www.coderanch.com/how-to/java/UserInput


Thanks for the article, i will give it a read!

ALSO, it is working just fine if i put say '7' in the place of 'a'. But when setting the int num equal to a letter it gives me an error
 
Junilu Lacar
Sheriff
Posts: 11489
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The confusion stems from your use of the literal 'a' (single quotes). This is a char literal and char is an integer type. That means that it will be converted to an int when you do comparisons like

On line 4, Java converts the char literal 'a' to its int equivalent, 97, before comparing it to the integer value of num. This is perfectly legal. The Scanner method nextInt(), however, takes what you entered on the console as a String and tries to convert it into an int. When you enter 7 on the console, this is fine because "7" can be converted to an int. However, "a" (double quotes) cannot be converted to an int, so you get the InputMismatchException.

Try this code:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!