Problems with Java, probably a stupid programmer ...

Status
Not open for further replies.

s-01

Ars Tribunus Militum
2,734
Subscriptor
What this program does is take in a lowercase letter from the keyboard
and outputs the number it corresponds to on the telephone keypad. What happens is that once you enter in a letter, it gives you the proper output and loops around again w/o asking you if you want to continue. Thanks for any help.

Anyway, my output looks like this:

Please enter a lowercase letter.
a
The letter a corresponds to digit 2
Would you like to enter another letter? [y/n]
Please enter a lowercase letter.
The letter you have entered in invalid.
Please enter a lowercase letter.
b
The letter b corresponds to digit 2
Would you like to enter another letter? [y/n]
Please enter a lowercase letter.
The letter you have entered in invalid.
Please enter a lowercase letter.

Heres the code:
The Source
 

MaximDiscord

Smack-Fu Master, in training
75
Use a string. Define it like 'String cont = "y"' ... you need to change your other variable to a String (or char if you so choose) as well because you are having the user input text. You will also need to change your other variable from an int to a String, because you are having the user input ASCII chars from the keyboard.

Also, when you read values in, you need to convert the input (which is going to be an integer) into a string. This should solve your problems.

Good luck.

Ron
 

Paralis

Ars Scholae Palatinae
930
The problem isn't with your choice of input method--well, it needn't be, but I'm tired, and I couldn't seem to figure out why the second call to System.in.read() wasn't working; you may have noticed that if you didn't explicitly declare cont = 'y'; after the second call that the program would end straightaway. Right.

To read in a String, the easiest way is to use class BufferedReader:
code:<HR>
Code:
BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) );

With that declaration, you can read in a single line of input as a string with the method call in.readLine();. Then it's quite simple to switch on the first character and generate your output.
 

Pika

Seniorius Lurkius
21
Sorry for the big post, but here is my solution to this problem. I have take the liberty of tiding up the code. The use of BufferedReader helps as you don't have to deal with ENTER being pressed. Also if your case statements you had lots of duplicated System.out and seemed to be missing the letter q.
code:<HR>
Code:
import java.io.BufferedReader;
import java.io.InputStreamReader;<P>public class hw1v2 {
	
	public static void main(String[] args) {
		
		String input = null;
		boolean cont = true;
		BufferedReader in = new BufferedReader(
		                            new InputStreamReader(System.in));
		while(cont) {
			System.out.println("Please enter a lowercase letter.");
			
			try {
				input = in.readLine();
			} catch(Exception E) {}
		
			if(!translate(input)) {
				continue;
			} else {
				System.out.println("Would you like to enter another letter? "
				                   + "[y/n]");
				try {
					input = in.readLine();
					if(-1 == input.indexOf("y")) {
					  cont = false;
					}
				} catch(Exception ex) { 
				  System.out.println("There was an input error ["
				                     + ex.getMessage() + "]"); 
				}
			}		
		}
		
		System.out.println("Thanks for using this program!");
	}
	
    public static boolean translate(String input) {
        char in = input.charAt(0);
        int num = 0;
        
        switch(in) {
    
    	case 'a':
    	case 'b': 
    	case 'c':
    	    num = 2;
            break;
    
    	case 'd':
    	case 'e':
    	case 'f':
    	    num = 3;
    	    break;
    
    	case 'g':
    	case 'h':
    	case 'i':
    	    num = 4;
    	    break;
    
    	case 'j':
    	case 'k':
    	case 'l':
    	    num = 5;
    	    break;
    
    	case 'm':
    	case 'n':
    	case 'o':
    	    num = 6;
    		break;
    
    	case 'p':
    	case 'q':
    	case 'r':
    	case 's':
    	    num = 7;
    		break;
    
    	case 't':
    	case 'u':
    	case 'v':
    	    num = 8;
    		break;
    
    	case 'w':
    	case 'x':
    	case 'y':
    	    num = 9;
    		break;
    
    	default:
    	    System.out.println("The letter you have entered in invalid.");
    		return false;
        }
        
        System.out.println( "The letter " + in + " corresponds to digit "
                            + num );
        return true;
	}
}
		
		<P>

The tabs seem to be a bit skewed.
 

s-01

Ars Tribunus Militum
2,734
Subscriptor
q and z were missing because they aren't on the telephone keypad. The excercise was to write a program that allows a user to enter in a letter and then the program would output it's corresponding number on the keypad.

And thanks to everyone for your help. Hopefully one of these I'll have enough brains to return some knowledge to the forums.
 
Status
Not open for further replies.