Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: How do I make my program accept letters?

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    40
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default How do I make my program accept letters?

    Ok, my problem is you cannot declare a switch with the type string or char, so how do I make it to where I type a letter, and it will display "Test complete" here is the code I have constructed,
    import java.util.Scanner;
    public class Text_Test {
     
    	/**
    	 * Author Tyler
    	 */
     
    	public static void main(String[] args) {
    		Scanner keyboard = new Scanner(System.in);
    		System.out.println("Type Some Letters...");
    		String Letters = keyboard.toString();
    		switch(Letters)
    		{
    		default:
    			System.out.println("Test Completed");
    		}
     
     
     
     
    	}
     
    }
    How will I make this work?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How do I make my program accept letters?

    make it to where I type a letter, and it will display "Test complete"
    What are the conditions you need to test that need to be true?
    You have read a single character
    the character is in the range: a-z and A-Z?

    The String class has a method to determine the number of characters it contains
    and for getting characters that can be tested.
    The Character class has methods for testing the contents of single characters.

    Use an if test with the results of the above tests to determine if your conditions are true.

  3. #3
    Member
    Join Date
    Sep 2011
    Posts
    40
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: How do I make my program accept letters?

    Anything typed from a - z, but how would I make it to where I can type it, and get an answer? I have always used switch statements but those only work with int types.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How do I make my program accept letters?

    how would I make it to where I can type it, and get an answer?
    The simple answer is: You write a program.
    You read it in
    You test it
    You put out an answer depending on the test.

    You could use a switch statement, but I think you'd have to have 26 case statements for the letters.
    You can use a char type in a switch as char are a type of integer.

  5. #5
    Member
    Join Date
    Sep 2011
    Posts
    40
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: How do I make my program accept letters?

    Ok I tried char, her is my code
    import java.util.Scanner;
    public class Text_Test {
     
    	/**
    	 * Author Tyler
    	 */
     
    	public static void main(String[] args) {
    		Scanner keyboard = new Scanner(System.in);
    		System.out.println("Type Some Letters...");
    		char Letters = (char) keyboard.nextShort();
    		switch (Letters)
    		{
    		default:
    			System.out.println("Test Complete");
    		}
     
     
     
     
     
    	}
     
    }
    And when I type letters I get this error
    Exception in thread "main" java.util.InputMismatchException
    	at java.util.Scanner.throwFor(Scanner.java:909)
    	at java.util.Scanner.next(Scanner.java:1530)
    	at java.util.Scanner.nextShort(Scanner.java:2030)
    	at java.util.Scanner.nextShort(Scanner.java:1989)
    	at Text_Test.main(Text_Test.java:11)
    What did I do wrong? I'm using an IDE

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How do I make my program accept letters?

    What did you type in? What is valid input for the nextShort() method?
    Why does the nextShort method throw that exception?

    Read the API doc for the Scanner class's nextShort method.

  7. #7
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: How do I make my program accept letters?

    there are few ways to get a char input,

    1.) "A" is not equal to 'A', so when you input a String, the String class provides a method .charAt(<integer index here>) that can do the char extraction for you in a String input or String object, example: if i have a String, say "Java", to get the first letter as a CHARACTER, i can say .charAt(0), then the program will give me the value 'J' as a character not a "J" as a String

    2.) if there is a System.out for an output routine. there is a System.in.read for an integer input, but there are 2 things you have to be concern with, first is, type casting the INTEGER value into a Character value, and Exception handling (i.e throwing an Exception),

    hope this helps
    Last edited by chronoz13; September 29th, 2011 at 08:47 PM.

  8. #8

    Default Re: How do I make my program accept letters?

    The switch statement accepts primitive datatypes and those type that wrap them.
    The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    A char can move seamlessly with an int. So if a switch can have int cases, it can have char cases.

    Your code:
    char Letters = (char) keyboard.nextShort();
    Instead, use:
    char Letters = (char) keyboard.nextInt();
    Kenneth Walter
    Software Developer
    http://kennywalter.com

  9. #9
    Junior Member
    Join Date
    Feb 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I make my program accept letters?

    One thing i personaly see is that u dont need the default in there

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How do I make my program accept letters?

    The problem with nextInt() is it won't like you typing in letters. It wants numeric characters 0-9.
    Not many users know the ASCII codes for the letters.

    To get a char, I think you'll need to read a String and use one of the String class's methods to get its characters.

  11. #11
    Member
    Join Date
    Sep 2011
    Posts
    40
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: How do I make my program accept letters?

    Thanks for the help guys, but your are allowed to switch on a string, my real problem was my IDE was outta date :/ so it always came back at me with an error, when there was no error at all..

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How do I make my program accept letters?

    I take it you are using jdk 1.7
    You seem to have forgotten to tell anyone.

  13. #13
    Member
    Join Date
    Sep 2011
    Posts
    40
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: How do I make my program accept letters?

    Yes, Yes I have. But I do have one last question, I plan to show this program to kids a school, but the java version on the computers at our school have jre 1.5... Would It be compatible?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How do I make my program accept letters?

    What program? You did not post the program's the current code.
    The compiler has options about version levels that could allow you to generate a version of a class file that is executable with a 1.5 version java program.

  15. #15
    Member
    Join Date
    Sep 2011
    Posts
    40
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: How do I make my program accept letters?

    Quote Originally Posted by Norm View Post
    What program? You did not post the program's the current code.
    The compiler has options about version levels that could allow you to generate a version of a class file that is executable with a 1.5 version java program.
    Thank you, I will look into that, and I'm making my first Java program, that's what I need help with, but its fixed now, so thank you!

Similar Threads

  1. Replies: 1
    Last Post: August 29th, 2011, 07:32 PM
  2. [SOLVED] Program to find how many letters start with vowels
    By Lokesh in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 14th, 2011, 05:58 AM
  3. Make JTextArea accept input from JButton
    By crazed8s in forum AWT / Java Swing
    Replies: 9
    Last Post: December 5th, 2010, 09:38 PM
  4. how do i make my program to do......
    By andys in forum Object Oriented Programming
    Replies: 6
    Last Post: November 29th, 2010, 07:44 AM
  5. how do i make my program to....
    By andys in forum Object Oriented Programming
    Replies: 2
    Last Post: November 26th, 2010, 10:31 AM