Results 1 to 4 of 4
- 01-31-2012, 06:21 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 1
- Rep Power
- 0
Need help with convert to uppercase program!
when i run it i get and dont know how to fix....Java Code:import java.util.Scanner; public class ToUpperCase { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int letter = (char)('a' - 'A'); System.out.print("Enter a letter "); letter = stdin.nextInt(); stdin.nextLine(); if ( letter >= 'a' && letter <= 'z' ) System.out.println(" The uppercase equivalent is " + (letter - 'a' - 'A') ); else System.out.println("The character is not a letter"); } }
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 ToUpperCase.main(ToUpperCase.java:11)
- 01-31-2012, 06:34 AM #2
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
Re: Need help with convert to uppercase program!
letter = stdin.nextInt(); is expecting value of type int.
- 01-31-2012, 07:18 AM #3
Member
- Join Date
- May 2011
- Posts
- 27
- Rep Power
- 0
Re: Need help with convert to uppercase program!
stdin.nextInt() reads int value.
you need to use the string value to read ie. stdin.nextLine() and the character from string & get the character from the string by using charAt(index) function.
- 01-31-2012, 09:49 AM #4
Member
- Join Date
- Jan 2012
- Posts
- 8
- Rep Power
- 0
Re: Need help with convert to uppercase program!
Hope this helps
-.gif)
Java Code:Scanner stdin = new Scanner(System.in); char letter = 'a'; System.out.print("Enter a letter "); try{ String inputLetter = stdin.next("[a-zA-Z]+"); letter = (inputLetter.trim().charAt(0)); if ( Character.isLetter(letter)){ System.out.println(" The uppercase equivalent is " + Character.toUpperCase(letter) ); }else{ System.out.println("The character is not a letter"); } }catch (Exception e) { System.out.println("The character is not a Letter"); }Last edited by bams; 01-31-2012 at 09:51 AM.
Similar Threads
-
How to convert this into a program
By AddieM in forum New To JavaReplies: 6Last Post: 06-06-2011, 02:18 PM -
Convert .java into a program i.e. .exe file
By ravi1 in forum New To JavaReplies: 33Last Post: 03-30-2011, 10:23 PM -
Convert Java program to web-app
By carderne in forum New To JavaReplies: 3Last Post: 12-08-2010, 06:10 PM -
Automatic convert to uppercase in keyevents
By ashin in forum SWT / JFaceReplies: 4Last Post: 08-09-2010, 01:59 PM -
Convert a program to Applet
By carl in forum Java AppletsReplies: 2Last Post: 08-09-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks