I am writing a number decryption program. Everything runs well! except for the p
ID: 3632943 • Letter: I
Question
I am writing a number decryption program. Everything runs well! except for the part where i need to loop the whole program in the event that the user inputs y or yes (Y, or YES should work as well). Problem is, regardless of what input i put in, the program DOES NOT loop! please help. I am including my program. On jGrasp it is line 15 and 56-59. This may not be the case for you, so i added comments on the parts where the loop begins and ends. Can you please point out what i'm doing wrong, and how i can fix this so that the program loops. if you copy and paste my code the program WILL run, so you can test it out if you like.I would also like to note that I am a beginner in programming. The code may be inefficient and possibly hard to read. I'm sorry for that in advance. But please try to help.
import java.util.Scanner;
public class phoneNumber
{
public static final String VALID_DIGITS= "0123456789";
public static final String CHARS_TO_IGNORE="- ";
public static void main(String[] args)
{
String answer="YES";
String input="";
String encryptedInput="";
String encryptedShiftNumber="";
String decryptedOutput="";
Scanner in= new Scanner(System.in);
while(answer=="YES"||answer=="Y") //loop begins to do the whole program
{
do
{
System.out.println("A valid number has 10 digits plus any number of spaces and hyphens Enter a telephone number:");
input=in.nextLine();
extractValidDigits(input);
encryptedInput=extractValidDigits(input);
if(encryptedInput==null)
{
System.out.println(" ERROR in encrypted number: "+input);
System.out.println("");
}
}while(encryptedInput==null);
getDecryptedShift(encryptedInput);
encryptedShiftNumber=getDecryptedShift(encryptedInput);
decryptPhoneNumber(encryptedInput,encryptedShiftNumber);
decryptedOutput=decryptPhoneNumber(encryptedInput,encryptedShiftNumber);
if(decryptedOutput==null)
{
String wrongNum="";
for(int d=0;d<encryptedInput.length();d++)
{
if(d==3||d==6)
{
wrongNum+="-";
}
char currentNum=encryptedInput.charAt(d);
wrongNum+=currentNum;
}
System.out.print(" The telephone number ");
System.out.print(wrongNum);
System.out.println("cannot be decrypted with a 212 area code ");
}
else
{
System.out.print(" The telephone number is ");
System.out.print(decryptedOutput);
System.out.print(" with an encryption shift value of ");
System.out.println(encryptedShiftNumber);
}
System.out.println("Do you want to decrypt another phone number(Yes/No) or (Y/N)?");
answer=in.nextLine(); //right here we will get input.
answer=answer.toUpperCase();
System.out.println(answer);//if entered y, the answer will output Y. but loop ends. Why?
}//loop ends here.
System.out.println(" HAVE A NICE DAY!");
}
public static String decryptPhoneNumber(String decryptDigits, String encryptedDigit)
{
String rightNum="";
int changeNum;
char currNumber;
int numDecrypt;
char lastDecrypt;
if(encryptedDigit=="Cannot compute")
{
return null;
}
else
{
for(int b=0;b<decryptDigits.length();b++)
{
currNumber=decryptDigits.charAt(b);
changeNum=Character.getNumericValue(currNumber);
lastDecrypt=encryptedDigit.charAt(0);
numDecrypt=Character.getNumericValue(lastDecrypt);
for(int c=0;c<numDecrypt;c++)
{
if(changeNum<0)
{
changeNum=9;
}
changeNum=changeNum-1;
if(changeNum==-1)
{
changeNum=9;
}
}
if(b==3||b==6)
{
rightNum+="-";
}
rightNum+=changeNum;
}
return rightNum;
}
}
public static String getDecryptedShift(String encryptedDigits)
{
int controlI=0;
int controlJ=0;
int controlK=0;
int decryptI=0;
int decryptJ=0;
int decryptK=0;
String digits=encryptedDigits;
String decrypt="";
char firstChar;
char secondChar;
char thirdChar;
int first;
int second;
int third;
firstChar=digits.charAt(0);
secondChar=digits.charAt(1);
thirdChar=digits.charAt(2);
first=Character.getNumericValue(firstChar);
second=Character.getNumericValue(secondChar);
third=Character.getNumericValue(thirdChar);
if(first!=2)
{
for(int i=first; i!=2;i--)
{
if(i<0)
{
i=9;
}
controlI=controlI+1;
decryptI=controlI;
}
}
else
{
decryptI=0;
}
if(second!=1)
{
for(int j=second;j!=1;j--)
{
if(j<0)
{
j=9;
}
controlJ=controlJ+1;
decryptJ=controlJ;
}
}
else
{
decryptJ=0;
}
if(third!=2)
{
for(int k=third; k!=2;k--)
{
if(k<0)
{
k=9;
}
controlK=controlK+1;
decryptK=controlK;
}
}
else
{
decryptK=0;
}
if(decryptI==decryptJ && decryptJ==decryptI)
{
decrypt+=decryptI;
}
else
{
decrypt= "Cannot compute";
}
return decrypt;
}
public static String extractValidDigits(String encryptedNumber)
{
String encrypt="";
for(int count=0; count<encryptedNumber.length();count++)
{
char inputChar = encryptedNumber.charAt(count);
if(CHARS_TO_IGNORE.indexOf(inputChar) >= 0)
{
encrypt+="";
}
else if(CHARS_TO_IGNORE.indexOf(inputChar)==-1 && VALID_DIGITS.indexOf(inputChar)==-1)
{
return null;
}
else if(VALID_DIGITS.indexOf(inputChar) >=0)
{
encrypt+=inputChar;
}
}
int ten= encrypt.length();
if(ten!=10)
{
return null;
}
else
{
return encrypt;
}
}
}
Explanation / Answer
change your while statment to this. while(answer.equals("YES") || answer.equals("Y")) //loop begins to do the whole program U cannot use == to compare strings, use .equals instead for comparisons
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.