Solved

A Program that asks the question to select A B C or D

Posted on 2004-09-07
36
274 Views
Last Modified: 2010-03-31
Wanting to write a program that asks a user to type A, B, C, or D.  When the user types D the program ends.  When the user types A, B or C the program displays the message "Good Job!" and then asks for another imput.  When the user types anything else, It will issue an error message and then ask for another imput.

This is what I have so far.

public class Imput
{
public static void main(String[] args) throws Exception
{
char eventType;
System.out.println
("Please make a choice of the Following Keys:");
      System.out.println("a");
      System.out.println("b");
      System.out.println("c");
      System.out.println("or q");
eventType = (char)System.in.read();
System.in.read(); System.in.read();
      //Absorbs Enter Key
            if(eventType = 'a' && 'b' && 'c')
                  System.out.print("Good Job!");
            while(eventType != 'q')
      }
}

I'm pretty sure that I'm going to have to use a loop but not sure how.  Any help on this would be much appreciated.

Thanks
0
Comment
Question by:RgrWalker
  • 19
  • 13
  • 4
36 Comments
 

Author Comment

by:RgrWalker
ID: 12002399
public class Imput
{
public static void main(String[] args) throws Exception
{
char eventType;
System.out.println
("Please make a choice of the Following Keys:");
     System.out.println("a");
     System.out.println("b");
     System.out.println("c");
     System.out.println("or d");
eventType = (char)System.in.read();
System.in.read(); System.in.read();
     //Absorbs Enter Key
          if(eventType = 'a' && 'b' && 'c')
               System.out.print("Good Job!");
          while(eventType != 'd')
     }
}

It would help if the q's were gone, lol
0
 
LVL 92

Expert Comment

by:objects
ID: 12002413
do
{
    System.out.println("Please make a choice of the Following Keys:");
     System.out.println("a");
     System.out.println("b");
     System.out.println("c");
     System.out.println("or q");
eventType = (char)System.in.read();
System.in.read(); System.in.read();
     //Absorbs Enter Key
          if(eventType == 'a' || eventTye== 'b' || eventType 'c')
               System.out.print("Good Job!");
}
while(eventType != 'd');
0
 

Author Comment

by:RgrWalker
ID: 12002464
public class ABCInput
{
public static void main(String[] args) throws Exception
{
char eventType;
System.out.println("Please make a choice of the Following Keys:");
System.out.println("a");
System.out.println("b");
System.out.println("c");
System.out.println("or q");
eventType = (char)System.in.read();
System.in.read(); System.in.read();
//Absorbs Enter Key
if(eventType == 'a' || eventType== 'b' || eventType 'c')
System.out.print("Good Job!");
}
while(eventType != 'd');
  }
}

This is giving me the same result as what I posted that I had so far.

From here I'm trying to get the program to ask once again if A B or C is chosen.  When the user types anything else, It will issue an error message and then ask for another imput.

I'm having problems getting the program to say Good Job one time and then giving an error the second time and asking again.
0
 
LVL 92

Expert Comment

by:objects
ID: 12002543
that code you posted doesn't actually compile
0
 
LVL 92

Expert Comment

by:objects
ID: 12002549
try this:

public class ABCInput
{
public static void main(String[] args) throws Exception
{
char eventType;
do
{
    System.out.println("Please make a choice of the Following Keys:");
     System.out.println("a");
     System.out.println("b");
     System.out.println("c");
     System.out.println("or d");
eventType = (char)System.in.read();
System.in.read(); System.in.read();
     //Absorbs Enter Key
          if(eventType == 'a' || eventType== 'b' || eventType=='c')
               System.out.print("Good Job!");
}
while(eventType != 'd');
}
}
0
 
LVL 1

Expert Comment

by:jLasitha
ID: 12003385
ry this:

public class ABCInput
{
public static void main(String[] args) throws Exception
{
char eventType;

MyLoop:
do
{
    System.out.println("Please make a choice of the Following Keys:");
     System.out.println("a");
     System.out.println("b");
     System.out.println("c");
     System.out.println("or d");
eventType = (char)System.in.read();
System.in.read(); System.in.read();
     
switch( eventType ){

case 'a':
case 'b':
case 'c':
System.out.print("Good Job!"); break;
case 'd':
break MyLoop;
}
         
}
while(true);
}
}
0
 

Author Comment

by:RgrWalker
ID: 12004914
Objects, The one you're posting isn't saying good job after you select the letter.  

Jlasitha, Yours is compiling but there are some problems with it.  

#1.  I select the letter a and hit enter and nothing happens.  I select the letter a again and hit enter and it say's Good Job! and asks again.

#2.  I select the Letter D and hit enter key and I have to select the letter d again and hit enter before it will end the program.

From here I'm trying to get the program to ask once again if A B or C is chosen.  When the user types anything else, It will issue an error message and then ask for another imput.

I'm having problems getting the program to say Good Job one time and then giving an error the second time and asking again.
0
 
LVL 92

Expert Comment

by:objects
ID: 12004944
> The one you're posting isn't saying good job after you select the letter

does when i run it here, do you have caps lock on as it is case sensitive?
0
 
LVL 1

Expert Comment

by:jLasitha
ID: 12005135
This works...

public class ABCInput {
   
   
    public static void main(String[] args) {
        char ch;
        try{
           
            MyLoop:
                do{
                   System.out.println("Please make a choice of the Following Keys:");
                   System.out.println("a");
                   System.out.println("b");
                   System.out.println("c");
                   System.out.println("or d");
                   
                    ch = (char)System.in.read();
                    System.out.flush();
                    switch( ch ){
                       
                        case 'a':
                        case 'b':
                        case 'c':
                            System.out.print("Good Job!"); break;
                        case 'd':
                            break MyLoop;
                    }
                }while(true);
        }catch(Exception e){
        }
    }
   
}
0
 

Author Comment

by:RgrWalker
ID: 12005525
public class ABCInput {
   
   
    public static void main(String[] args) {
        char ch;
        try{
           
            MyLoop:
                do{
                   System.out.println("Please make a choice of the Following Keys:");
                   System.out.println("a");
                   System.out.println("b");
                   System.out.println("c");
                   System.out.println("or d");
                   
                    ch = (char)System.in.read();
                    System.out.flush();
                    switch( ch ){
                       
                        case 'a':
                        case 'b':
                        case 'c':
                            System.out.print("Good Job!"); break;
                        case 'd':
                            break MyLoop;
                    }
                }while(true);
        }catch(Exception e){
        }
    }
   
}


Ok, this one works and does everything But when selecting a, b or c I'm getting this.

a
Good Job!Please make a choice of the Following Keys:
a
b
c
or q
Please make a choice of the Following Keys:
a
b
c
or q


It's saying Good Job! Please make a choice of the following keys and listing them and then saying please make a choice of the following keys Immediately after it lists them.

It should only say Good Job! Please make a choice of teh following keys one time.

I'm going to raise the Points for this question, It's obviously harder than what I thought.  Creating the first part was fairly simple, it's this last part that is kicking my butt.

Still trying to get the program to ask once after A B or C is chosen.  When the user types anything else, It will issue an error message and then ask for another imput.

I'm having problems getting the program to say Good Job one time and then giving an error the second time and asking again.
0
 
LVL 1

Expert Comment

by:jLasitha
ID: 12005797
Check this

public static void main(String[] args) {
        char ch ;
        boolean flag = true;
        try{
           
            MyLoop:
                do{
                   
                    if( flag ){
                        System.out.println("Please make a choice of the Following Keys:");
                        System.out.println("a");
                        System.out.println("b");
                        System.out.println("c");
                        System.out.println("or d");
                    }
                   
                    ch = (char)System.in.read();
                    System.out.flush();
                    if( Character.isLetter( ch ) ){
                        switch( ch ){
                           
                            case 'a':
                            case 'b':
                            case 'c':
                                System.out.print("Good Job!");
                                flag = false;
                                break;
                            case 'd':
                                break MyLoop;
                            default: flag = true; break;
                        }
                    }
                }while(true);
        }catch(Exception e){
        }
    }
0
 

Author Comment

by:RgrWalker
ID: 12005884
Please make a choice of the Following Keys:
a
b
c
or d
a
Good Job!
a
Good Job!c
Good Job!d

 ----jGRASP: operation complete.


It's saying good Job but not asking the question again after the first time of choosing a b or c.

Still trying to get the program to ask once after A B or C is chosen.  When the user types anything else, It will issue an error message and then ask for another imput.

I'm having problems getting the program to say Good Job one time and then giving an error the second time and asking again.

Sorry I'm not being a big help on this, I worked on it last night for a bit and I'm trying to do like 3 things at once this morning around the house.  I'm checking this thread about once every 15 minutes or so to see what we've (You all) have come up with.

Thanks for the help so far.  I think it's coming along ok.
0
 
LVL 1

Expert Comment

by:jLasitha
ID: 12006060
If we are going to use System.in.read() it reads twise in the loop.
First time the charcter and second time a white space.Actually it is a bad coding.The correct coding should come with java.io...as follow


import java.io.*;

/**
 *
 * @author  lasithag
 */
public class ABCInput {
   
    public static void main(String[] args) {
        char ch ;
        try{
           
            MyLoop:
                do{
                   
                   
                    System.out.println("Please make a choice of the Following Keys:");
                    System.out.println("a");
                    System.out.println("b");
                    System.out.println("c");
                    System.out.println("or d");
                   
                   
                    BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
                    ch = (char)br.read();
                    System.out.flush();
                   
                    switch( ch ){
                       
                        case 'a':
                        case 'b':
                        case 'c':
                            System.out.print("Good Job!");
                            break;
                        case 'd':
                            break MyLoop;
                        default:
                            System.out.println("Error!");
                            break;
                    }
                }while(true);
        }catch(Exception e){
        }
    }
   
}
0
 

Author Comment

by:RgrWalker
ID: 12010458
This didn't produce the error message for me, I see that it's in there but It didn't execute I guess.
0
 
LVL 1

Expert Comment

by:jLasitha
ID: 12012791
This error message will appere when you type a letter except a b c or d....is it wrong?
0
 

Author Comment

by:RgrWalker
ID: 12013970
naa, it's not wrong, I am needing it to appear after the first time you select a b c or d, if you select d the program should end.  If you select a b or c then the program should say good job and ask you again to select a b c or d.  If you select anything other than d then you should get an error message and refer you back to selecting a b c or d
0
 
LVL 1

Expert Comment

by:jLasitha
ID: 12014017


import java.io.*;


public class ABCInput {
   
    public static void main(String[] args) {
        char ch ;
        boolean IsErr = false;
        try{
           
            MyLoop:
                do{
                   
                    if( !IsErr ){
                        System.out.println("Please make a choice of the Following Keys:");
                        System.out.println("a");
                        System.out.println("b");
                        System.out.println("c");
                        System.out.println("or d");
                    }
                   
                    BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
                    ch = (char)br.read();
                    System.out.flush();
                   
                    switch( ch ){
                       
                        case 'a':
                        case 'b':
                        case 'c':
                            System.out.print("Good Job!");
                            IsErr = false;
                            break;
                        case 'd':
                            break MyLoop;
                        default:
                            System.out.println("Error!");
                            IsErr = true;
                            break;
                    }
                }while(true);
        }catch(Exception e){
        }
    }
   
}
0
 

Author Comment

by:RgrWalker
ID: 12015565
Ok, I went back over this and I have this so far, I would like to work off what I have to make it do what I need.  This is what I have so far.

public class ABCInput
{
public static void main(String[] args) throws Exception
{
char eventType;
{
System.out.println("Please make a choice of the Following Keys:");
System.out.println("a");
System.out.println("b");
System.out.println("c");
System.out.println("or d");
eventType = (char)System.in.read();
while (eventType !='a' && eventType != 'b' && eventType != 'c')
System.in.read(); System.in.read();
System.out.println("Good Job! Please make another choice from the following keys:");
System.out.println("a");
System.out.println("b");
System.out.println("c");
System.out.println("or d");
eventType = (char)System.in.read();

}
}
}


Shouldn't I be able to use a for loop to control the definite number of loop interations required?

How and where would I use the for loop.

I'm working on this right now, so any info would be nice.  The above code doesn't do the error message that I need, I think I need to get the program to do the basic things first and then we can add to it.  I don't understand half of the information that has been posted so far.  

Thanks
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Expert Comment

by:jLasitha
ID: 12015626
can you make a pseudo code for your programme correctl
0
 

Author Comment

by:RgrWalker
ID: 12015783
Kind of not wanting to use import java.io.*;


Is it possible without it?
0
 
LVL 1

Expert Comment

by:jLasitha
ID: 12015820
When i debug the program there is an issue if we usr System.in.read().It reades the input twice.The better solution is useing java.io
0
 

Author Comment

by:RgrWalker
ID: 12016299
What if we used do while loop?
0
 
LVL 1

Expert Comment

by:jLasitha
ID: 12016332
I try to do that way but i didn't find a solution..actually your problem rise with that...
0
 

Author Comment

by:RgrWalker
ID: 12016934
Ok, I've added a bit so far this is what I have.

public class ABCInput
{
public static void main(String[] args) throws Exception
{
char keystroke;
{
System.out.println("Please make a choice of the Following Keys:");
System.out.println("a");
System.out.println("b");
System.out.println("c");
System.out.println("or d");
keystroke = (char)System.in.read();
System.in.read(); System.in.read();
      //Absorbs enter key
while (keystroke == 'a' && keystroke == 'b' && keystroke == 'c');
{
System.out.println("Good Job! Please make another choice from the following keys:");
System.out.println("a");
System.out.println("b");
System.out.println("c");
System.out.println("or d");
keystroke = (char)System.in.read();
System.in.read(); System.in.read();
      //Absorbs enter key
while (keystroke == 'd');

}
}
}
}

0
 

Author Comment

by:RgrWalker
ID: 12017026
How would I make that code above End if the d key was entered?

How would I make the code above give an error report if anything other than d was entered and then ask for the user to type in another key and keep responding with the error report unless d was pressed?

(EXIBIT A) Basically the program must do one of two things at the start of the program.  Either #1 END if the letter d was entered or #2 Say, "Good Job! Please make another choice from the following keys" and list them once again.

(EXIBIT B) Now THIS time the program must do one of two things.  #1 Same as above, if d was entered then it should end.
#2 Say, "Error! Error! Error! Please make another choice from the following keys" and list them again.

AS is the program above executes Exibit A flawlessly and basically Exibit B doesn't exist in the code yet.  


0
 

Author Comment

by:RgrWalker
ID: 12017783
I'm going to up the point value on this because although I'm not to sure how difficult it is, it sure has taken a few days to get where we're at.  
0
 

Author Comment

by:RgrWalker
ID: 12018799
I swear I think IF and Else Statements will work here...

What do you think?
0
 

Author Comment

by:RgrWalker
ID: 12022389
public class ABCInput
{
public static void main(String[] args) throws Exception
{
char keyStroke;
do
{
System.out.println("Please make a choice of the Following Keys:");
System.out.println("a");
System.out.println("b");
System.out.println("c");
System.out.println("or d");
keyStroke = (char)System.in.read();
System.in.read();
if(keyStroke == 'a' || keyStroke == 'b' || keyStroke =='c')
System.out.print("Good Job!");
}
while(keyStroke != 'd');
}
}


With the Above Code it seems like we're VERY Close.  Any Suggestions?  Do I need to put an ungodly amount of points on this to get more people helping?  
0
 

Author Comment

by:RgrWalker
ID: 12022409
I've increased the point value to get more help on this.  I need to finish, I'm losing way to much sleep at night thinking about it, Had nightmares of Code all night long.  No matter how hard I tried to go to sleep my mind kept trying to figure it out.  


I'm sure you guys have never had that problem before?

/e turns sarcasm off
0
 
LVL 1

Expert Comment

by:jLasitha
ID: 12023490
Tell me the outputs that you want step by setp.I  will fix the problem
0
 

Author Comment

by:RgrWalker
ID: 12025305

#1.  Program asks user to please make a choice of the following keys and then list A, B, C or D.

#2.  User will select a Key.  If it is A, B or C then the Program will say, Good Job! and ask the question again.  THIS Will only happen ONCE.  If the user types the Letter D it will say Good Job! and end the program.

#3.  If the user types any key other than D the program will give an error message and ask the user to type another choice.

As of right now............

The program is asking the user to select a key and then saying Good Job if A B or C is chosen and Saying Good Job and ending if D is chosen.

What the program is not doing is Saying Good Job ONLY ONE time and giving an Error a Second time if the user types A B or C for a second time.

Hope this helps.
0
 
LVL 1

Accepted Solution

by:
jLasitha earned 500 total points
ID: 12025429
Sorry i didn't get your logic correctly this time it's clear...

import java.io.*;


public class ABCInput {
   
    public static void main(String[] args) {
        char ch ;
        boolean IsErr = false;
        boolean IsSelected = false;//True when you select a b or c
        try{
           
            MyLoop:
                do{
                   
                    if( !IsErr ){
                        System.out.println("Please make a choice of the Following Keys:");
                        System.out.println("a");
                        System.out.println("b");
                        System.out.println("c");
                        System.out.println("or d");
                    }
                   
                    BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
                    ch = (char)br.read();
                    System.out.flush();
                   
                    switch( ch ){
                       
                        case 'a':
                        case 'b':
                        case 'c':
                            if( IsSelected ){
                               
                                 System.out.println("Error!");
                                 IsErr = true;
                            }
                            else{
                               
                                System.out.print("Good Job!");
                                IsErr = false;
                                IsSelected = true;
                            }
                           
                            break;
                           
                        case 'd':
                            break MyLoop;
                        default:
                            System.out.println("Error!");
                            IsErr = true;
                            break;
                    }
                }while(true);
        }catch(Exception e){
        }
    }
   
}
0
 

Author Comment

by:RgrWalker
ID: 12025500
jLasitha, THis works and I am giving you all of the points but could you please help me understand what you wrote?  There are some things I don't understand and it doesn't do me much good to have a program that works without knowing how it is doing it.

Is there anyway, I mean you can even take all day to do it, I'm not in that big of a rush, and explain line by line what each one is doing?

Thank you
0
 
LVL 1

Expert Comment

by:jLasitha
ID: 12026037
Sorry i got to go now..i will do it on next monday...
0
 
LVL 1

Expert Comment

by:jLasitha
ID: 12042938
Ok here is my explanation

import java.io.*;//Java.io package for handling input stream


public class ABCInput {
   
    public static void main(String[] args) {
        char ch ;
        boolean IsErr = false;//This flag true when an error ocurre
        boolean IsSelected = false;//True when you select a b or c
        try{
           
            MyLoop://Indicatore for the loop( just like a variable -name )
                do{
                   
                    if( !IsErr ){//If there is no error
                        System.out.println("Please make a choice of the Following Keys:");
                        System.out.println("a");
                        System.out.println("b");
                        System.out.println("c");
                        System.out.println("or d");
                    }
                   
                    BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
                    ch = (char)br.read();
                    System.out.flush();//read input charcter
                   
                    switch( ch ){//Useful alternative for if condition.When you need to put several if condition it is easy to have switch statement . ch input variable.
                       
                        case 'a'://If( ch == 'a')
                        case 'b'://If( ch == 'b')
                        case 'c'://If( ch == 'c')
                            if( IsSelected ){//This flag is true when you selected a,b or c before.Then this only false at the first time only
                               
                                 System.out.println("Error!");//Print error
                                 IsErr = true;//set error flag
                            }
                            else{
                               
                                System.out.print("Good Job!");//For first occurance of a,b or c selection
                                IsErr = false;//reset error flag(if there was an error last step)
                                IsSelected = true;//select flag true for a,b or c selection occured
                            }
                           
                            break;//break switch statment.not the loop
                           
                        case 'd'://If( ch == 'd')
                            break MyLoop;//break the loop( the loop will be broken )
                        default://this is default situation.when you enterd a letter except a,b or c
                            System.out.println("Error!");
                            IsErr = true;//set error flag
                            break;//break switch statement
                    }
                }while(true);
        }catch(Exception e){
        }
    }
   
}
0
 

Author Comment

by:RgrWalker
ID: 12043922
Thank you for explaining this, When I get home tonight I'll try to make myself understand it, I may have some more questions tomorrow.  Just so I'm sure on how to do it.

Thanks again
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now