Question

Write a method that asks the user to enter a list of names and ages using...

Write a method that asks the user to enter a list of names and ages using Gui input and splits the info into an array of String. Validate that every other string is an int and check the value to ensure it is between 21 and 65 inclusive, print a message to the standard error object if not. Return the array. Use regular expressions to split and validate. Must follow java conventions and indent. In java with good indentations please

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import javax.swing.JOptionPane;//this is to use GUI
import java.util.*;//It is imported for scanner function
public class Main {
   public static String[] input(int n)
   {
       String name, age1,element;
       //age1 is string formate
       int age;
       //age is to store integer type of age1
       String[] a=new String[n];
       //string array
       try{
           for(int i=0;i<n;i++)
           {
               //taking gui input from user
               name= JOptionPane.showInputDialog("Name of person"+(i+1));
               age1 = JOptionPane.showInputDialog("age of person"+(i+1));
               age = Integer.parseInt(age1);
               if(age<21 || age>65)
                   //checking wheather the age is valid or not
               {
                   System.err.println("Invalid age:");
                   break;
               }
               else
               {
                   //storing the string in array
                   element=name+"-"+age;
                   a[i]=element;

               }

           }
       }
       catch(Exception e)
       {
           System.out.println(e+"\n please enter valid inputs");
       }
       finally{
       return a;//returning the array
       }  
   }

  
   public static void main(String[] args) {
      
       int n;
       Scanner ob=new Scanner(System.in);
       System.out.println("Enter no of names");
       n=ob.nextInt();//taking the number of names
       String a[]=input(n);
       System.out.println("Name\t age");
       try{
           for(int i=0;i<a.length;i++)
           {//regular expressions to split and validate
               String str = a[i];
       String[] arrOfStr = str.split("-", 5);
  
       for (String ar : arrOfStr)
       {
           //printing the splited string
          System.out.print(ar+"\t");
       }
       //to get in next line
       System.out.println();

           }
       }
       catch(Exception e)
       {
           //to print what exception raised
           System.out.println(e+" you entered invalid age");
       }

      
              
       System.exit(0);
       //terminate the programe
      
   }
}

Input X ? Name of person4 OK Cancel

gui for taking name of the person

Input х ? age of person5 OK Cancel

Gui for taking age of the person

C:\Users\sivaram\Desktop>javac Main.java C:\Users\sivaram\Desktop>java Main Enter no of names 5 age 54 34 Name Siva ram sai G

Add a comment
Know the answer?
Add Answer to:
Write a method that asks the user to enter a list of names and ages using...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (JAVA NetBeans) Write a Java program, which asks the user to enter a string, then (a)...

    (JAVA NetBeans) Write a Java program, which asks the user to enter a string, then (a) reverse the string, (b) change the case of the string (CaT cAt) (c) print the chars at even positions (Object Ojc)

  • In Java. Write a GUI contact list application. The program should allow you to input names...

    In Java. Write a GUI contact list application. The program should allow you to input names and phone numbers. You should also be able to input a name and have it display the previously entered phone number. The GUI should look something like the following, although you are welcome to format it in any way that works. This should be a GUI application with a JFrame. The program should contain two arrays of Strings. One array will contain a list...

  • Write a C++ program that asks user number of students in a class and their names....

    Write a C++ program that asks user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’ names and ranking. Follow the Steps Below Save the project as A4_StudentRanking_yourname....

  • In java, write a program with a recursive method which asks the user for a text...

    In java, write a program with a recursive method which asks the user for a text file (verifying that the text file exists and is readable) and opens the file and for each word in the file determines if the word only contains characters and determines if the word is alpha opposite. Now what I mean by alpha opposite is that each letter is the word is opposite from the other letter in the alphabet. For example take the word...

  • Make a program using Java that asks the user to input an integer "size". That integer...

    Make a program using Java that asks the user to input an integer "size". That integer makes and prints out an evenly spaced, size by size 2D array (ex: 7 should make an index of 0-6 for col and rows). The array must be filled with random positive integers less than 100. Then, using recursion, find a "peak" and print out its number and location. (A peak is basically a number that is bigger than all of its "neighbors" (above,...

  • Write a Java program called Flying.java that, firstly, prompts (asks) the user to enter an input...

    Write a Java program called Flying.java that, firstly, prompts (asks) the user to enter an input file name. This is the name of a text file that can contain any number of records. A record in this file is a single line of text in the following format: Num of passengers^range^name^manufacturer^model where: Num of passengers is the total number of people in the plane. This represents an integer number, but remember that it is still part of the String so...

  • Using c++ Write a program that reads in a list of up to 25 first names...

    Using c++ Write a program that reads in a list of up to 25 first names from an input file nameData.txt and allows the user to display the name data, sort it in ascending or descending order, count the number of occurrences of a given name, or exit the program. The input file consists of a single names per line with each line terminated with an end of line (i.e. using Enter key to end the line). Your program should...

  • Write the Java code for the class WordCruncher. Include the following members:

    **IN JAVAAssignment 10.1 [95 points]The WordCruncher classWrite the Java code for the class WordCruncher. Include the following members:A default constructor that sets the instance variable 'word' to the string "default".A parameterized constructor that accepts one String object as a parameter and stores it in the instance variable. The String must consist only of letters: no whitespace, digits, or punctuation. If the String parameter does not consist only of letters, set the instance variable to "default" instead. (This restriction will make...

  • Finish FormatJavaProgram.java that prompts the user for a file name and assumes that the file contains...

    Finish FormatJavaProgram.java that prompts the user for a file name and assumes that the file contains a Java program. Your program should read the file (e.g., InputFile.java) and output its contents properly indented to ProgramName_Formatted.java (e.g., InputFile_Formatted.java). (Note: It will be up to the user to change the name appropriately for compilation later.) When you see a left-brace character ({) in the file, increase your indentation level by NUM_SPACES spaces. When you see a right-brace character (}), decrease your indentation...

  • Activity: Writing Classes Page 1 of 10 Terminology attribute / state behavior class method header class...

    Activity: Writing Classes Page 1 of 10 Terminology attribute / state behavior class method header class header instance variable UML class diagram encapsulation client visibility (or access) modifier accessor method mutator method calling method method declaration method invocation return statement parameters constructor Goals By the end of this activity you should be able to do the following: > Create a class with methods that accept parameters and return a value Understand the constructor and the toString method of a class...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT