Results 1 to 4 of 4
  1. #1
    ultimashadow is offline Member
    Join Date
    Jan 2012
    Posts
    1
    Rep Power
    0

    Default Need help with convert to uppercase program!

    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");
    	
    	}
    }
    when i run it i get and dont know how to fix....

    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)

  2. #2
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Need help with convert to uppercase program!

    letter = stdin.nextInt(); is expecting value of type int.

  3. #3
    dinesh.guntha is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Default 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.

  4. #4
    bams is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Re: Need help with convert to uppercase program!

    Hope this helps

    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

  1. How to convert this into a program
    By AddieM in forum New To Java
    Replies: 6
    Last Post: 06-06-2011, 02:18 PM
  2. Convert .java into a program i.e. .exe file
    By ravi1 in forum New To Java
    Replies: 33
    Last Post: 03-30-2011, 10:23 PM
  3. Convert Java program to web-app
    By carderne in forum New To Java
    Replies: 3
    Last Post: 12-08-2010, 06:10 PM
  4. Automatic convert to uppercase in keyevents
    By ashin in forum SWT / JFace
    Replies: 4
    Last Post: 08-09-2010, 01:59 PM
  5. Convert a program to Applet
    By carl in forum Java Applets
    Replies: 2
    Last Post: 08-09-2007, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •