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).
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;
}
}
Re: Help with interrupting a loop without break or continue
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:
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;
}
}