Java Help Please:
I'm doing something wrong with my while loop. I want my code to
check the phone number that is input to make sure it matches. If it
doesn't I want it to print out Error! and I want my While loop to
ask the user to try again by inputting another phone number. When
the user puts in a phone number that matches I want the program to
continue to output my tokens. When I run this I get the message
that my code is invalid even when I enter a number that matches my
pattern. I know it's my while loop because when I comment out the
while loop the program recognizes when the number I put in
matches.
class Main{
public static boolean
validatePhone(String phoneNumber){
return
phoneNumber.matches( "\\([1-9]\\)\\d{3}-[1-9]\\d{3}-[1-9]\\d{4}"
);
}
public static void main(String[] args){
String phoneNumber;
Boolean validatePhone;
Scanner scanner = new Scanner
(System.in);
System.out.println("\nPlease enter phone number: ");
phoneNumber = scanner.nextLine();
while(!validatePhone(phoneNumber)){
System.out.println("Error!");
System.out.println("\nEnter phone number: ");
phoneNumber =
scanner.nextLine();
}
String[]tokens =
phoneNumber.split("-|\\(|\\)"); //String Split Method
System.out.printf("1 %s",tokens[1]); //tokenized area code
System.out.printf("\2: %s",tokens[2]); //tokenized 1st 3
digits
System.out.printf("\n3 : %s",tokens[3]);//tokenized last 4
digits
}
}
import java.util.Scanner;
class Main {
public static boolean validatePhone(String phoneNumber) {
return phoneNumber.matches("([1-9]{3})-([1-9]{3})-([1-9]{4})");
}
public static void main(String[] args) {
String phoneNumber;
Scanner scanner = new Scanner(System.in);
System.out.println("\nPlease enter phone number: ");
phoneNumber = scanner.nextLine();
while (!validatePhone(phoneNumber)) {
System.out.println("Error!");
System.out.println("\nEnter phone number: ");
phoneNumber = scanner.nextLine();
}
String[] tokens = phoneNumber.split("-"); //String Split Method
System.out.printf("1 : %s", tokens[0]); //tokenized area code
System.out.printf("\n2 : %s", tokens[1]); //tokenized 1st 3 digits
System.out.printf("\n3 : %s", tokens[2]);//tokenized last 4 digits
}
}
Java Help Please: I'm doing something wrong with my while loop. I want my code to...
I need help on creating a while loop for my Java assignment. I am tasked to create a guessing game with numbers 1-10. I am stuck on creating a code where in I have to ask the user if they want to play again. This is the code I have: package journal3c; import java.util.Scanner; import java.util.Random; public class Journal3C { public static void main(String[] args) { Scanner in = new Scanner(System.in); Random rnd = new Random(); System.out.println("Guess a number between...
Can you help me rearrange my code by incorporating switch I'm not really sure how to do it and it makes the code look cleaner. Can you help me do it but still give the same output? Also kindly add an in-line comment on what changes and rearrangement you've done so I can understand what's going on. import java.util.ArrayList; import java.util.Scanner; public class Bank { /** * Add and read bank information to the user. * @param arg A string,...
I am currently doing homework for my java class and i am getting an error in my code. import java.util.Scanner; import java.util.Random; public class contact { public static void main(String[] args) { Random Rand = new Random(); Scanner sc = new Scanner(System.in); System.out.println("welcome to the contact application"); System.out.println(); int die1; int die2; int Total; String choice = "y";...
Can you help me rearrange my code to make it look cleaner but still give the same output? Also kindly add an in-line comment on what changes and rearrangement you've done so I can understand what's going on. import java.util.ArrayList; import java.util.Scanner; public class Bank { /** * Add and read bank information to the user. * @param arg A string, double and int array containing * the command line arguments. * @exception Any exception * @return an arraylsit of...
Could you help me pleas , this is my code I want change it to insert student by user , and i have problem when i want append name it just one time i can't append or present more one. >>>>>>>>>>>>>>>>>>>. import java.util.Iterator; import java.util.Scanner; public class studentDLLTest { static int getChoice() { Scanner in = new Scanner(System.in); int choice; do { System.out.print("\nYour choice? : "); choice = in.nextInt(); } while (choice < 1 || choice > 9); return choice;...
Please edit my following JAVA code so that there is no main function. I would like one function in this class that completes what the current program is trying to do so that I can call this class in my main class which will be separate. import java.math.RoundingMode; import java.text.DecimalFormat; import java.util.Scanner; public class enterwklyincme { private static DecimalFormat df = new DecimalFormat("0.00"); public static float readFloat(Scanner reader) { float num; while (true) { try { num = Float.parseFloat(reader.nextLine()); return...
JAVA: Rewrite the following code to where the inputs are from a file. The file name will be from the input from the user scanner. CODE: import java.util.*; public class Q1 { public static int sumOD (int k) { int sumOD = 0; int in = k; while (in != 0) { int digitON = in % 10; sumOD += digitON; in /= 10; } return sumOD; } public static void main(String[] args) { int n, i, k; System.out.println("Enter...
This question deals with extending already existing Java code via a while loop. Ask the user how many year inputs he wants to check - an integer. Assume that the user always gives an input which is between 1 and 5 (inclusive). Next, ask the user for K number of year inputs where K = the number user has given as input before (inside a while loop). Check for each year input whether that year is a leap year or...
JAVA: amortization table: Hi, I have built this code and in one of my methods: printDetailsToConsole I would like it to print the first 3 payments and the last 3 payments if n is larger than 6 payments. Please help! Thank you!! //start of program import java.util.Scanner; import java.io.FileOutputStream; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.lang.Math; //345678901234567890123456789012345678901234567890123456789012345678901234567890 /** * Write a description of class MortgageCalculator here. * * @author () * @version () */ public class LoanAmortization { /* *...
I need help with my Java code. A user enters a sentence and the program will tell you what words were duplicated and how many times. If the same word is in the sentence twice, but one is capitalized, it will not count it as a duplicate. How can I make the program read the same word as the same word, even if one is capitalized and the other is not? For example, "the" and "The" should be counted as...