Question

In: Computer Science

Complete the program below in order to make run properly by competing validName methods and add...

Complete the program below in order to make run properly by competing validName methods and add trycatch block in the main method.

public class ExceptionWithThrow {

public static String validName(String name) throws InputMismatchException{

// check here if the name is a valid name

// through InputMismatchException if invalid //throw

// return the name if it is valid

}

public static void main(String[] args) {

// Ask the user to enter a name and their validity through validName method

// Add try and catch block around appropriate statements

}

}

In Java Please!

In Java Please!

In Java Please!

In Java Please!

In Java Please!

Solutions

Expert Solution

Program

import java.util.*;

public class ExceptionWithThrow {

   public static String validName(String name) throws InputMismatchException{
      
       for(int i=0; i<name.length(); i++)
       {
           char ch = name.charAt(i);
           if(!Character.isLetter(ch) && ch!= ' ')
               throw new InputMismatchException();
       }
       return name;
   }

public static void main(String[] args) {
  
   Scanner sc = new Scanner(System.in);
  
   System.out.print("Enter a name: ");
   String name = sc.nextLine();

   try{
       validName(name);
   }catch(InputMismatchException e){
       System.out.println ("Invalid name!");
   }
   }
}

Output:

Enter a name: John123
Invalid name!

Solving your question and helping you to well understand it is my focus. So if you face any difficulties regarding this please let me know through the comments. I will try my best to assist you. However if you are satisfied with the answer please don't forget to give your feedback. Your feedback is very precious to us, so don't give negative feedback without showing proper reason.
Always avoid copying from existing answers to avoid plagiarism.
Thank you.


Related Solutions

I need to write a program and can't seem to make it run properly. Someone can...
I need to write a program and can't seem to make it run properly. Someone can help with the coding? It's with python Thanks! Here is the program: The rules of Shut the Box are as follows (abbreviated version): + You have two 5-sided die + You have 5 wooden blocks (labeled 1 through 5) + Each turn, you roll the dice and knock down the blocks corresponding to the number on each die + You have to knock down...
Complete the java program. /* Note: Do not add any additional methods, attributes. Do not modify...
Complete the java program. /* Note: Do not add any additional methods, attributes. Do not modify the given part of the program. Run your program against the provided Homework2Driver.java for requirements. */ /* Hint: This Queue implementation will always dequeue from the first element of the array i.e, elements[0]. Therefore, remember to shift all elements toward front of the queue after each dequeue. */ public class QueueArray<T> { public static int CAPACITY = 100; private final T[] elements; private int...
This program works with strings complete the following functionsto allow the program to function properly;...
This program works with strings complete the following functions to allow the program to function properly;a) Write a C function int string_to_number(char *) that takes a string of decimal digits as an input parameter, and returns the integer value that the string represents. For example, if the char s[] ="256", then string_to_number(s) returns integer 256 (int data type). You are allowed to use strlen(…) function, but not allowed to use atoi and other functions. Ignore the negative sign and overall.b)...
Please add to this Python, Guess My Number Program. Add code to the program to make...
Please add to this Python, Guess My Number Program. Add code to the program to make it ask the user for his/her name and then greet that user by their name. Please add code comments throughout the rest of the program If possible or where you add in the code to make it ask the name and greet them before the program begins. import random def menu(): print("\n\n1. You guess the number\n2. You type a number and see if the...
Complete the attached program by adding the following: a) add the Java codes to complete the...
Complete the attached program by adding the following: a) add the Java codes to complete the constructors for Student class b) add the Java code to complete the findClassification() method c) create an object of Student and print out its info in main() method of StudentInfo class. * * @author * @CS206 HM#2 * @Description: * */ public class StudentInfo { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application...
complete all methods in java! /* Note:   Do not add any additional methods, attributes.         Do...
complete all methods in java! /* Note:   Do not add any additional methods, attributes.         Do not modify the given part of the program.         Run your program against the provided Homework2Driver.java for requirements. */ /* Hint:   This Queue implementation will always dequeue from the first element of         the array i.e, elements[0]. Therefore, remember to shift all elements         toward front of the queue after each dequeue. */ public class QueueArray<T> {     public static int CAPACITY = 100;...
In C++ Complete the template program. ADD to your c++ program as a comment the PARTIAL...
In C++ Complete the template program. ADD to your c++ program as a comment the PARTIAL output from executing your program - Only copy the last 6 lines of output. There is no input data for this problem. // Find Pythagorean triples using brute force computing. #include <iostream> using std::cout; using std::endl; int main() { int count = 0; // number of triples found long int hypotenuseSquared; // hypotenuse squared long int sidesSquared; // sum of squares of sides cout...
Please complete a program that will be an order entry program for a store or organization....
Please complete a program that will be an order entry program for a store or organization. The program will accept order entry input from two customers, calculate the total order amount of each order and display data about each order to the customer. There will be two orders, one will be a “delivery” order and the other will be a “pickup” order. The data entered by each customer will be: Customer name Item purchased Quantity purchased Price of the item...
A java program with classes and methods to manage employment. Program should be able to store/add...
A java program with classes and methods to manage employment. Program should be able to store/add employees, calculate payroll, display or schedule shifts. All classes should have at least a null constructor and copy constructor. Other constructors are up to your discretion. Include all necessary accessors and modifiers. To test the classes, create a container class with simply a main() method. The container class will have attributes that are class objects of each of the classes you create. The main()...
In order to make sure that all math formatting is properly displayed, you are strongly encouraged...
In order to make sure that all math formatting is properly displayed, you are strongly encouraged to reload this page after opening. In order to make sure that all math formatting is properly displayed, you are strongly encouraged to reload this page after opening.   Consider the following statistical studies. Which method of data collection would likely be used to collect data for each study? Explain your selection. What is the population of interest? Would you take a census or use...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT