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 3 of 3

Thread: Help with interrupting a loop without break or continue

  1. #1
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Help with interrupting a 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).

    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
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,267
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Help with interrupting a loop without break or continue

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/49774-interrupt-loop-without-break-continue.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  3. #3
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: Help with interrupting a 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:

    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. How to convert or break String into Character
    By Bharath12 in forum Java Theory & Questions
    Replies: 2
    Last Post: September 14th, 2011, 11:41 PM
  2. Can't get lines to cut at 60 characters and continue on next line right
    By JavaN00b in forum What's Wrong With My Code?
    Replies: 10
    Last Post: May 22nd, 2011, 09:44 AM
  3. continue statements
    By monroe in forum Java Applets
    Replies: 1
    Last Post: March 20th, 2010, 06:26 PM
  4. How to make user press enter to continue in program?
    By BC2210 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 3rd, 2009, 05:08 AM
  5. Break it down
    By [Kyle] in forum Java Theory & Questions
    Replies: 5
    Last Post: September 19th, 2009, 09:04 PM