4 Replies - 277 Views - Last Post: 17 December 2013 - 08:59 PM Rate Topic: -----

#1 wcs139  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-December 13

putting an error message and and ask for the input up to three times

Posted 17 December 2013 - 04:06 PM

I'm a beginner in java and I'm trying to put an error message into my program. Where if the user does not input a state abbreviation, it asks for the abbreviation again three times. After the third time, it issues an error message saying the program will terminate. So I know to use a while loop with a if then inside but the problem is I don't know how to do that with strings. I'm not asking for code, I just want to know how to start it or give me hints or anything.

String abbreviation;
              
      System.out.println("give me a state abbreviation"); //asks user to input a state abbreviation
                Scanner in = new Scanner(System.in);
                abbreviation = in.next();
             
                char abbr = abbreviation.charAt(0);   //sets the first inputed abbreviation letter as a char value
                char abbr2 = abbreviation.charAt(1);  //sets the second inputed abbreviation letter as a char value
                switch (abbr)  
                {
                    case 'A': case 'a':                  //case for states that begin with letter A
                    //System.out.println("a entered");
                    switch (abbr2)
                    {
                        case 'L': case 'l':
                        System.out.println("Alabama");
                        break;
                        case 'K': case 'k':
                        System.out.println("Alaska");
                        break;
                        case 'R': case 'r':
                        System.out.println("Arizona");
                        break;
 default: 
System.out.println("input is invalid");                   
 }//end switch A
                   
  break;
                    case 'C': case 'c':    //case for states that begin with letter C
                    switch (abbr2)
                    {
                        case 'A': case 'a':
                        System.out.println("California");
                        break;
                        case 'O': case 'o':
                        System.out.println("Colorado");
                        break;
                        case 'T': case 't':
                        System.out.println("Conneticut");
                        break;
             default: 
System.out.println("input is invalid");         
 }//end case C
                    
break;
case 'D': case 'd':
                    System.out.println("Delaware");
                    break;
                    case 'F': case 'f':
                    System.out.println("Florida");
                    break;
                    case 'G': case 'g':
                   System.out.println("Georgia");
                    break;
                    case 'H': case 'h':
                    System.out.println("Hawaii");
                    break;
                    case 'I': case 'i':
                    switch(abbr2)
                    {
                        case 'D': case 'd':
                        System.out.println("Idaho");
                        break;
                        case 'L': case 'l':
                        System.out.println("Illinois");
                        break;
                        case 'N': case 'n':
                        System.out.println("Indiana");
                        break;
                        case 'A': case 'a':
                        System.out.println("Iowa");
                        break;
                default: 
System.out.println("input is invalid");       
}//end case I
break;                   
 case 'K': case 'k':
                    switch(abbr2)
                    {
                        case 'S': case 's':
                        System.out.println("Kansas");
                        break;
                        case 'Y': case 'y':
                        System.out.println("Kentucky");
                        break;
                    default: 
System.out.println("input is invalid");   
}//end case K
break;                   
 case 'L': case 'l':
                    System.out.println("Lousiana");
                    break;
                    case 'M': case 'm':
                    switch(abbr2)
                    {
                        case 'E': case 'e':
                        System.out.println("Maine");
                        break;
                        case 'D': case 'd':
                        System.out.println("Maryland");
                        break;
                        case 'A': case 'a':
                        System.out.println("Massachusetts");
                        break;
                        case 'I': case 'i':
                        System.out.println("Michigan");
                        break;
                        case 'N': case 'n':
                        System.out.println("Minnesota");
                        break;
                        case 'S': case 's':
                        System.out.println("Mississippi");
                        break;
                        case 'O': case 'o':
                        System.out.println("Missouri");
                        break;
                        case 'T': case 't':
                        System.out.println("Montana");
                        break;
                   default: 
System.out.println("input is invalid");   
 }//end case M
break;                   
 case 'N': case 'n':
                    switch(abbr2)
                    {
                        case 'E': case 'e':
                        System.out.println("Nebraska");
                        break;
                        case 'V': case 'v':
                        System.out.println("Nevada");
                        break;
                        case 'H': case 'h':
                        System.out.println("New Hampshire");
                        break;
                        case 'J': case 'j':
                        System.out.println("New Schmuckzie");
                        break;
                        case 'M': case 'm':
                        System.out.println("New Mexico");
                        break;
                        case 'Y': case 'y':
                        System.out.println("New York");
                        break;
                        case 'C': case 'c':
                        System.out.println("North Carolina");
                        break;
                        case 'D': case 'd':
                        System.out.println("North Dakota");
                        break;
                   default: 
System.out.println("input is invalid");   
 }//end case N
break;                   
 case 'O': case 'o':
                    switch(abbr2)
                    {
                        case 'H': case 'h':
                        System.out.println("Ohio");
                        break;
                        case 'K': case 'k':
                        System.out.println("Oklahoma");
                        break;
                        case 'O': case 'o':
                        System.out.println("Oregon");
                        break;
                   default: 
System.out.println("input is invalid");   
 }//end case O
break;                    
case 'P': case 'p':
                    System.out.println("Pennyslavania");
                    break;
                    case 'R': case 'r':
                    System.out.println("Rhode Island");
                    break;
                    case 'S': case 's':
                    switch(abbr2)
                    {
                        case 'C': case 'c':
                        System.out.println("South Carolina");
                        break;
                        case 'D': case 'd':
                        System.out.println("South Dakota");
                        break;
                  default: 
System.out.println("input is invalid");    
 }//end case S
break;                   
 case 'T': case 't':
                    switch(abbr2)
                    {
                        case 'N': case 'n':
                        System.out.println("Tennessee");
                        break;
                        case 'X': case 'x':
                        System.out.println("Texas");
                        break;
                 default: 
System.out.println("input is invalid");     
 }//end case T
break;                   
 case 'U': case 'u':
                    System.out.println("Utah");
                    break;
                    case 'V': case 'v':
                    switch(abbr2)
                    {
                        case 'T': case 't':
                        System.out.println("Vermont");
                        break;
                        case 'A': case 'a':
                        System.out.println("Virginia");
                        break;
                   default: 
System.out.println("input is invalid");   
 }//end case V
break;                    
case 'W': case 'w':
                    switch(abbr2)
                    {
                        case 'A': case 'a':
                        System.out.println("Washington");
                        break;
                        case 'V': case 'v':
                        System.out.println("West Virginia");
                        break;
                        case 'I': case 'i':
                        System.out.println("Wisconsin");
                        break;
                        case 'Y': case 'y':
                        System.out.println("Wyoming");
                        break;
                 default: 
System.out.println("input is invalid");      
}//end case W
                    
                    break;

                }//end switch
default:
System.out.println("input is not valid");
}
}

This post has been edited by andrewsw: 17 December 2013 - 04:20 PM
Reason for edit:: Use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: putting an error message and and ask for the input up to three times

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7649
  • View blog
  • Posts: 12,905
  • Joined: 19-March 11

Re: putting an error message and and ask for the input up to three times

Posted 17 December 2013 - 04:51 PM

Quote

So I know to use a while loop with a if then inside


Probably it'd make sense to try it with a less complicated scenario to get the hang of it.

Write a little toy program that accepts an input string. If that string is the character "A", they win. If they try three times and don't get it, they lose.

Quote

but the problem is I don't know how to do that with strings.


What don't you know how to do?
Was This Post Helpful? 0
  • +
  • -

#3 wcs139  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-December 13

Re: putting an error message and and ask for the input up to three times

Posted 17 December 2013 - 05:01 PM

I'm probably not making sense, I've been trying to work on this for a bit now...I don't know what the while condition would be to start the loop for the program to restart after the user inputs a wrong input.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7649
  • View blog
  • Posts: 12,905
  • Joined: 19-March 11

Re: putting an error message and and ask for the input up to three times

Posted 17 December 2013 - 05:06 PM

If you find you can't state the problem clearly, it's time to get up from the keyboard and take a walk for a little while.
You have to know what problem you're trying to solve, and sometimes that means taking a break.

Quote

I don't know what the while condition would be to start the loop for the program to restart after the user inputs a wrong input.


Presumably you want to prompt for input again if
- they get the answer wrong and
- they still have guesses remaining.
Was This Post Helpful? 1
  • +
  • -

#5 JNoobie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-December 13

Re: putting an error message and and ask for the input up to three times

Posted 17 December 2013 - 08:59 PM

Howdy!!! Jon!.
I am too a newb. But still. I think I managed to fix it somehow. Mostly because I did something similar whitch you can see I asked about earlier in the forum.

Anyway. You use counters. a counter (if thats the right technical term) is a variable that you can use to control a "while loop". I used it to produce different outputs to the user in my code.
So I thought, one could use em in your code aswell.

I put all the switches inside a "while loop"

I put a counter at each of the default statements.
It looks like this: count= count + 1.

As I understand it, its so usual in programming theres an abbreviation to that.
you simply type count++; and count++; would be the same as count = count + 1;

The reason you use it is to use the counter in its own turn as a continuation test for how long the while loop will go on.

once I declared a variable named counter, and made a "counter++;" thingy at everyplace you had default statement saying "input is invalid" It would then add 1 ( +1 ) to the counter.

Since It then adds 1 to every occasion the user makes an invalid input, the variable "counter" gets bigger. Starting from 0, then 1, 2, 3. So because of this I could use it in a continuation test for the "while loop" Like this:

while (counter <= 2) {

lotsa statments }


as soon the boleean expresion (counter <= 2) is false. the program jumps out of the loop and does whats next forinstance outputs a text saying "you typed wrong 3 times in a row"

usually It looks like this:

int variable;

variable = 0; //the variable needs initialization.

while (variable < 7){
statements
}



I did have to do some rearangements in original code. Most of it whitch I do not know why.
I just read about switches. So. If you find this helpfull maybe you could help me if I have trouble with switches some day? :wink:/>
Hope this helped. :)

SPOILER ALERT!!!


public class StateAbb {
	public static void main(String[]args) {
		char abbr;
		char abbr2;
		int count;
		String abbreviation;
        
	      System.out.println("give me a state abbreviation"); //asks user to input a state abbreviation
	                
	      count = 0;          //Initializing counter :)/>/>
	      
	      while (count <= 2){
	    	  
	    	  
	    	  abbr = 'a';     //*** I learned initialization should be made outside the loop. but I only got it to work
	      abbr2 = 'b';        //*** inside. Figured since variable 'char' then a and b would sufice as values.
	      
	      
	      abbreviation = TextIO.getln();
	             
	                abbr = abbreviation.charAt(0);   //sets the first inputed abbreviation letter as a char value
	                abbr2 = abbreviation.charAt(1);  //sets the second inputed abbreviation letter as a char value
	                switch (abbr)  
	                {
	                    case 'A': case 'a':                  //case for states that begin with letter A
	                    //System.out.println("a entered");
	                    switch (abbr2)
	                    {
	                        case 'L': case 'l':
	                        System.out.println("Alabama");
	                        break;
	                        case 'K': case 'k':
	                        System.out.println("Alaska");
	                        break;
	                        case 'R': case 'r':
	                        System.out.println("Arizona");
	                        break;
	 default: 
	System.out.println("input is invalid"); 
	count++;                                 //******** added a counter when input invalid**********
	 }//end switch A
	                   
	  break;
	                    case 'C': case 'c':    //case for states that begin with letter C
	                    switch (abbr2)
	                    {
	                        case 'A': case 'a':
	                        System.out.println("California");
	                        break;
	                        case 'O': case 'o':
	                        System.out.println("Colorado");
	                        break;
	                        case 'T': case 't':
	                        System.out.println("Conneticut");
	                        break;
	             default: 
	System.out.println("input is invalid");  
	count++;                               //*************added another counter and so on************
	 }//end case C
	                    
	break;
	case 'D': case 'd':
	                    System.out.println("Delaware");
	                    break;
	                    case 'F': case 'f':
	                    System.out.println("Florida");
	                    break;
	                    case 'G': case 'g':
	                   System.out.println("Georgia");
	                    break;
	                    case 'H': case 'h':
	                    System.out.println("Hawaii");
	                    break;
	                    case 'I': case 'i':
	                    switch(abbr2)
	                    {
	                        case 'D': case 'd':
	                        System.out.println("Idaho");
	                        break;
	                        case 'L': case 'l':
	                        System.out.println("Illinois");
	                        break;
	                        case 'N': case 'n':
	                        System.out.println("Indiana");
	                        break;
	                        case 'A': case 'a':
	                        System.out.println("Iowa");
	                        break;
	                default: 
	System.out.println("input is invalid");
	count++;
	}//end case I
	break;                   
	 case 'K': case 'k':
	                    switch(abbr2)
	                    {
	                        case 'S': case 's':
	                        System.out.println("Kansas");
	                        break;
	                        case 'Y': case 'y':
	                        System.out.println("Kentucky");
	                        break;
	                    default: 
	System.out.println("input is invalid"); 
	count++;
	}//end case K
	break;                   
	 case 'L': case 'l':
	                    System.out.println("Lousiana");
	                    break;
	                    case 'M': case 'm':
	                    switch(abbr2)
	                    {
	                        case 'E': case 'e':
	                        System.out.println("Maine");
	                        break;
	                        case 'D': case 'd':
	                        System.out.println("Maryland");
	                        break;
	                        case 'A': case 'a':
	                        System.out.println("Massachusetts");
	                        break;
	                        case 'I': case 'i':
	                        System.out.println("Michigan");
	                        break;
	                        case 'N': case 'n':
	                        System.out.println("Minnesota");
	                        break;
	                        case 'S': case 's':
	                        System.out.println("Mississippi");
	                        break;
	                        case 'O': case 'o':
	                        System.out.println("Missouri");
	                        break;
	                        case 'T': case 't':
	                        System.out.println("Montana");
	                        break;
	                   default: 
	System.out.println("input is invalid");
	count++;
	 }//end case M
	break;                   
	 case 'N': case 'n':
	                    switch(abbr2)
	                    {
	                        case 'E': case 'e':
	                        System.out.println("Nebraska");
	                        break;
	                        case 'V': case 'v':
	                        System.out.println("Nevada");
	                        break;
	                        case 'H': case 'h':
	                        System.out.println("New Hampshire");
	                        break;
	                        case 'J': case 'j':
	                        System.out.println("New Schmuckzie");
	                        break;
	                        case 'M': case 'm':
	                        System.out.println("New Mexico");
	                        break;
	                        case 'Y': case 'y':
	                        System.out.println("New York");
	                        break;
	                        case 'C': case 'c':
	                        System.out.println("North Carolina");
	                        break;
	                        case 'D': case 'd':
	                        System.out.println("North Dakota");
	                        break;
	                   default: 
	System.out.println("input is invalid");
	count++;
	 }//end case N
	break;                   
	 case 'O': case 'o':
	                    switch(abbr2)
	                    {
	                        case 'H': case 'h':
	                        System.out.println("Ohio");
	                        break;
	                        case 'K': case 'k':
	                        System.out.println("Oklahoma");
	                        break;
	                        case 'O': case 'o':
	                        System.out.println("Oregon");
	                        break;
	                   default: 
	System.out.println("input is invalid"); 
	count++;
	 }//end case O
	break;                    
	case 'P': case 'p':
	                    System.out.println("Pennyslavania");
	                    break;
	                    case 'R': case 'r':
	                    System.out.println("Rhode Island");
	                    break;
	                    case 'S': case 's':
	                    switch(abbr2)
	                    {
	                        case 'C': case 'c':
	                        System.out.println("South Carolina");
	                        break;
	                        case 'D': case 'd':
	                        System.out.println("South Dakota");
	                        break;
	                  default: 
	System.out.println("input is invalid");
	count++;
	 }//end case S
	break;                   
	 case 'T': case 't':
	                    switch(abbr2)
	                    {
	                        case 'N': case 'n':
	                        System.out.println("Tennessee");
	                        break;
	                        case 'X': case 'x':
	                        System.out.println("Texas");
	                        break;
	                 default: 
	System.out.println("input is invalid");
	count++;
	 }//end case T
	break;                   
	 case 'U': case 'u':
	                    System.out.println("Utah");
	                    break;
	                    case 'V': case 'v':
	                    switch(abbr2)
	                    {
	                        case 'T': case 't':
	                        System.out.println("Vermont");
	                        break;
	                        case 'A': case 'a':
	                        System.out.println("Virginia");
	                        break;
	                   default: 
	System.out.println("input is invalid"); 
	count++;
	 }//end case V
	break;                    
	case 'W': case 'w':
	                    switch(abbr2)
	                    {
	                        case 'A': case 'a':
	                        System.out.println("Washington");
	                        break;
	                        case 'V': case 'v':
	                        System.out.println("West Virginia");
	                        break;
	                        case 'I': case 'i':
	                        System.out.println("Wisconsin");
	                        break;
	                        case 'Y': case 'y':
	                        System.out.println("Wyoming");
	                        break;
	                 default: 
	System.out.println("input is invalid");
	count++;
	}//end case W
	                    
	                    break;

	                }//end switch
	
		
		//I had to remove the last default statement to make the program run. Dunno why.
		
		
		
	      }
		
		TextIO.putln("wrong 3 times in row"); //this is after the loop ends because 3 faults.
	}

}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1