Reputation: 41
I'm having trouble with this code for my class : Here are the problems
1.) It does not Loop after success
(the code compiles but the when it asks "what is your number?" and i type 909-8930 the output just answers :correct until exit (with the phone number). I was under the assumption that its suppose to loop?)
2.) gives an error due to the parenthesis in:
phone = phone.replaceAll("(","");
phone = phone.replaceAll(")","");
but works fine when i comment them out and leave:
phone = phone.replaceAll("-","");
3.) does not have a max character validation using the boolean command. (we have to use code we've learned in class)
I'm using Vista (I know, all bad) cmd to compile the Java code.
/*
*/
import java.util.Scanner; //
public class PhoneNumber
{
//********number()******
public static boolean Number(String str)
{
int n=0;
while(n<str.length()) //while condition for the loop
{
char c=str.charAt(n);
if(!(c>='0'&&c<='9'))return(false);//0 to 9
n++; // counter and checked loop?
}
return (true);
}
// Phone Number
public static void main(String[] args)
{
// Create a Scanner object to read input.
String phone;
Scanner sc = new Scanner(System.in);
// Get the favorite city
System.out.print("What is your phone number?"); // no ln
phone=sc.nextLine( );
//replace all perenthesis and dashes
phone = phone.replaceAll("-","");
phone = phone.replaceAll("(","");
phone = phone.replaceAll(")","");
// validation of number
if(Number(phone))
{
// sub stuff to add back the dash and whateves
String first,middle,last;
first = phone.substring (0,3);
middle = phone.substring (3,6);
last = phone.substring (6);
String phonea = "("+ first + ")" +"-"+middle+ "-"+last;
// print stuff back
System.out.println ("correct until exit"+ phonea);
// condition to exit
if (phone.equalsIgnoreCase("quit")); //not working?
if (phone.equalsIgnoreCase("end"));
if (phone.equalsIgnoreCase("stop"))System.exit(0);
}
else
{
System.err.println("error-incorrect format: "+ phone); //error
}
}
}
Upvotes: 0
Views: 887
Reputation: 3458
In general, if you don't understand what your code is doing, I recommend:
System.out.println('reached line 17')
all over the code to see the order in which your program is doing things.That said, here are some (partial) answers.
1) It does not Loop after success
A loop repeatedly performs the statements inside of it. It sounds like what you want to be repeated is most or all of the program:
correct
If you want those three actions repeated over and over, you need to make a big loop around the code that does those three actions. No loop, no repetition.
(And somewhere inside that loop is going to be the code that exits the program if the input string is "quit", etc.)
2) gives an error due to the parenthesis in:
phone = phone.replaceAll("(","");
To fix this you need to know two things. First, replaceAll
treats its first argument "("
as a regular expression. Without worrying what that is, just know that replaceAll
is giving a special meaning to the (
character rather than treating it as a plain old (
. You can tell replaceAll to treat it normally by putting a \
before the (
. The \
is called an "escape". So the contents of your string argument should be: \(
Second, you can't just type "\("
into your source code because the Java compiler itself treats \
as a special character. You have to add one more backslash to tell the compiler to treat the other backslash normally. It sounds absurd, but so it goes.
phone = phone.replaceAll("\\(","");
3) does not have a max character validation using the boolean command.
Not sure what you want here: checking that it has more or less than a certain number of characters? Either way, you could write an if
to check whether phone.length() is more or less than some number.
Upvotes: 0
Reputation: 2397
About 1: If you want the program to ask for a second input you need to tell it to do so. Everything in the main function is only called once. You can put everything in a loop where you ask
while (!phone.equalsIgnoreCase("quit")) {
}
This would loop until you enter quit.
So far your exit conditions do not work, because you left the semicolon behind your if-statement. Because of this the compiler thinks that the line is over and jumps into the next line.
if (phone.equalsIgnoreCase("quit")); //remove the semicolon in this line, otherwise System.exit will never be called.
Also you put these lines into the if (Number(phone)) statement. But as "quit" is not a number, you will never reach this line of code when you actually entered quit.
Upvotes: 1
Reputation: 2561
The reason for the termination of your application has to do with how you determine what the next state of the application should be. Look at the code at the end of the correct phone number case:
// print stuff back
System.out.println ("correct until exit"+ phonea);
// condition to exit
if (phone.equalsIgnoreCase("quit")); //not working?
if (phone.equalsIgnoreCase("end"));
if (phone.equalsIgnoreCase("stop"))
System.exit(0);
Essentially, your intent is to see if the String
variable phone
contains the state change requested by the user (so, "continue" or "quit" would be my guess). Since phone
is just the digits of the number (or some kind of bad input) this will not give you the state information. In turn, these cases will rarely ever be true.
What you need to do post-validation is request that the user provide proper input so the application can determine what the next state should be. Since we (SO users) don't know exactly what the specification of the program is, we cannot determine what exactly you need to do. However, it would probably look something like this:
System.out.println("What would you like to do?");
String nextState = sc.nextLine();
while (!nextState.equals("quit") && !nextState.equals("exit")) {
//Do what you need to do (read the phone number, validate it, etc)
System.out.println("What would you like to do?");
String nextState = sc.nextLine();
}
System.out.println("End of application");
This essentially gives the application control in determining when the application should start and stop. While the user does not wish for the application to stop, you keep asking the user for phone numbers to validate.
While this isn't the entire answer to the issues you're having, it should hopefully give you some decent guidance on fixing some of the applications issues.
Upvotes: 1