Results 1 to 3 of 3
  1. #1
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Interrupt loop without break or continue

    My instructor will not allow us to use break, (unless it is within a switch statement) or continue. I have written a program that takes a phrase with a hyphen and prints out the corresponding phone number placing the hyphen in the correct position. Which I have done. Works great. I
    am supposed to account for the user putting in a blank space. The way I tried to accomplish was with the return value from my ReturnsSum(). If it was a 1 I incremented j (j++) to skip a head of the next index and I decremented i (i--) to account for the skip. but when there is a
    space before the hyphen it only prints those 2 numbers and then the hyphen or prints a 1. I am stuck. I just need someone to point me in the right direction (so I can have one of those slap myself in the head epiphanies).

    Java Code:
    import java.util.*;
     
    public class TelephoneConverter
    {
    	static Scanner console = new Scanner(System.in);
    	public static void main(String[] args)
    	{
    		String inputStr, lowerCase, preStr, postStr;
    		int hyphenIndex, i = 0, j = 0, k = 0, jump = 1, length, preLength, postLength, num = 0;
    		char character = ' ';
     
    		System.out.println("Enter the phone number as a phrase, including the hyphen: ");
     
    		inputStr = console.nextLine();
    		lowerCase = inputStr.toLowerCase();
    		hyphenIndex = lowerCase.indexOf("-");
    		length = lowerCase.length();
    		preStr = lowerCase.substring(0, hyphenIndex);
    		postStr = lowerCase.substring(hyphenIndex + 1, length);
    		preLength = preStr.length();
    		postLength = postStr.length();
     
    		System.out.print("Your number is: ");
     
    		if(preLength == 3)
    		{
    			i = preLength;
    			while(i > 0)
    			{
    				if(num == 1)//account for space logic in preStr*******************************
    				{
    					j++;
    					i--;
    				}
     
    				character = preStr.charAt(j);
    				i--;
    				j++;
    				System.out.print(ReturnsNum(character));
     
    			}
     
    				  System.out.print("-");
     
    			 j = 0;	
    			 i = 4;
    			 while(i > 0)
    			 {
    			 	character = postStr.charAt(j);
    				i--;
    				j++;
    				System.out.print(ReturnsNum(character));
    			 }	
     
    		 }
     
    		 else if(preLength > 3)
    		 {	
    		 	i = 3;
    			while(i > 0)
    			{
    		 		character = preStr.charAt(j);
    				i--;
    				j++;
    				System.out.print(ReturnsNum(character));
    		   }
    			System.out.print("-");
    			i = preLength - 3;
    			j = 3;
    			while(i > 0)
    			{
    				character = preStr.charAt(j);
    				i--;
    				j++;
    				System.out.print(ReturnsNum(character));
     
    			}
    			i = 7 - preLength;
    			j = 0;
    			while(i > 0)
    			{
    				character = postStr.charAt(j);
    				i--;
    				j++;
    				System.out.print(ReturnsNum(character));
    			}
     
    		  }
     
    		  else if(preLength < 3)
    		  {
    		  		j = 0;
    		 	   i = preLength;
    		  		while(i > 0)
    				{
    					character = preStr.charAt(j);
    					i--;
    					j++;
    					System.out.print(ReturnsNum(character));
    				}
    				j = 0;
    				i = 3 - preLength;
    				while(i > 0)
    				{
    					character = postStr.charAt(j);
    					i--;
    					j++;
    					System.out.print(ReturnsNum(character));
    				}
    					System.out.print("-");
     
    				j = 0;
    				i = 4;
    				while(i > 0)
    				{
    					character = postStr.charAt(j);
    					i--;
    					j++;
    					System.out.print(ReturnsNum(character));
    				}
    		  }
    	}
     
    	public static int ReturnsNum(char character)
    	{
    			int num = 0;
    				switch(character)
    						{
    						   case ' ':
    								num = 1;
    								break;
     
    							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':
    							case 'z':
    								num = 9;
    								break;							
    						}
    							return num;
    	}
     
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Interrupt loop without break or continue


  3. #3
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Re: Interrupt loop without break or continue

    I figured it out. I thought I would share the solution. I added commented asterics lines to show the beginning and end of the code blocks I added:

    Java Code:
    import java.util.*;
    
    public class TelephoneConverter
    {
    	static Scanner console = new Scanner(System.in);
    	public static void main(String[] args)
    	{
    		String inputStr, lowerCase, preStr, postStr;
    		int hyphenIndex, i = 0, j = 0, k = 0, jump = 1, length, preLength, postLength, num = 0;
    		char character = ' ';
    		
    		System.out.println("Enter the phone number as a phrase, including the hyphen: ");
    		
    		inputStr = console.nextLine();
    		lowerCase = inputStr.toLowerCase();
    		hyphenIndex = lowerCase.indexOf("-");
    		length = lowerCase.length();
    		preStr = lowerCase.substring(0, hyphenIndex);
    		postStr = lowerCase.substring(hyphenIndex + 1, length);
    		preLength = preStr.length();
    		postLength = postStr.length();
    		
    		System.out.print("Your number is: ");
    		if((preLength == 3) && (preStr.charAt(1) == ' '))               //**************************************************
    		{
    			System.out.print(ReturnsNum(preStr.charAt(0)));
    			System.out.print(ReturnsNum(preStr.charAt(2)));
    			System.out.print(ReturnsNum(postStr.charAt(0)));
    			System.out.print("-");
    			
    			i = 4;
    			j = 1;
    			
    			while(i > 0 )
    			{
    				character = postStr.charAt(j);
    				j++;
    				i--;
    				if(ReturnsNum(character) == 1)
    				{
    					System.out.print("");
    					j++;
    					i++;
    				}
    				else
    				{
    				 System.out.print(ReturnsNum(character));
    				}                                                             //***********************************************
    			}
    		}
    		
    		
    		if((preLength == 3) && (preStr.charAt(1) != ' '))
    		{
    			i = preLength;
    			while(i > 0)
    			{
    				if(num == 1)
    				{
    					j++;
    					i--;
    				}
    			   
    				character = preStr.charAt(j);
    				i--;
    				j++;
    				System.out.print(ReturnsNum(character));
    				
    			}
    			
    				  System.out.print("-");
    			 
    			 j = 0;	
    			 i = 4;
    			 while(i > 0)
    			 {
    			 	character = postStr.charAt(j);
    				i--;
    				j++;
    				if(ReturnsNum(character) == 1)               //*************************************************
    				{
    					System.out.print("");
    					j++;
    					i++;
    				}
    				else
    				{
    					System.out.print(ReturnsNum(character));
    				}	                                                                //***********************************************
    			 }	
    			 
    		 }
    		 
    		 else if(preLength > 3)
    		 {	
    		 	i = 3;
    			while(i > 0)
    			{
    		 		character = preStr.charAt(j);
    				i--;
    				j++;
    				System.out.print(ReturnsNum(character));
    		   }
    			System.out.print("-");
    			i = preLength - 3;
    			j = 3;
    			while(i > 0)
    			{
    				character = preStr.charAt(j);
    				i--;
    				j++;
    				System.out.print(ReturnsNum(character));
    				
    			}
    			i = 7 - preLength;
    			j = 0;
    			while(i > 0)
    			{
    				character = postStr.charAt(j);
    				i--;
    				j++;
    				if(ReturnsNum(character) == 1)            //********************************************
    				{
    					System.out.print("");
    					j++;
    					i++;
    				}
    				else
    				{
    					System.out.print(ReturnsNum(character));
    				}	                                                              //********************************************
    			}
    			
    		  }
    		  
    		  else if(preLength < 3)
    		  {
    		  		j = 0;
    		 	   i = preLength;
    		  		while(i > 0)
    				{
    					character = preStr.charAt(j);
    					i--;
    					j++;
    					System.out.print(ReturnsNum(character));
    				}
    				j = 0;
    				i = 3 - preLength;
    				while(i > 0)
    				{
    					character = postStr.charAt(j);
    					i--;
    					j++;
    					if(ReturnsNum(character) ==1)               //******************************************
    					{
    						System.out.print("");
    						j++;
    						i++;
    					}
    					else
    					{
    						System.out.print(ReturnsNum(character));
    					}	                                                               //****************************************
    				}
    					System.out.print("-");
    					
    				j = 0;
    				i = 4;
    				while(i > 0)
    				{
    					character = postStr.charAt(j);
    					i--;
    					j++;
    					if(ReturnsNum(character) == 1)                        //**********************************************
    					{
    						System.out.print("");
    						j++;
    						i++;
    					}
    					else
    					{
    						System.out.print(ReturnsNum(character));
    					}	                                                                   //******************************************
    				}
    		  }
    	}
    	
    	public static int ReturnsNum(char character)
    	{
    			int num = 0;
    				switch(character)
    						{
    						   case ' ':
    								num = 1;
    								break;
    								
    							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':
    							case 'z':
    								num = 9;
    								break;							
    						}
    							return num;
    	}
    
    }

Similar Threads

  1. break or stop for loop
    By mitra in forum New To Java
    Replies: 7
    Last Post: 09-13-2011, 12:39 AM
  2. Re-writing program with break/continue
    By rich123 in forum New To Java
    Replies: 10
    Last Post: 02-01-2011, 02:19 AM
  3. Replies: 10
    Last Post: 01-07-2011, 11:28 AM
  4. using from lable with break and continue
    By arefeh in forum New To Java
    Replies: 3
    Last Post: 01-06-2010, 01:52 PM
  5. Demonstration of break and continue keywords
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 08:47 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
  •