Problem: Please help me modify my program, When you entered a letter in age, It will not accept it and will display "Invalid Input". Do the same for name, when a number is entered it must display "Invalid Input" package examples; import java.util.*; public class sales { public static void main(String[] args) { Scanner kbd = new Scanner(System.in); String[] names; String[] address; String[] age; // String[] age; int n = 0; boolean successfulRead=false; do { try { System.out.print("How many names will be sorted: "); n = Integer.parseInt(kbd.nextLine()); if (n >=0 ) { successfulRead = true; } else { System.out.println("The amount cannot be a negative amount"); successfulRead = false; } } catch (NumberFormatException e) { successfulRead = false; System.out.println("\f You have enetered an invalid amount"); } } while (!successfulRead); names = new String[n]; address = new String[n]; age = new String[n]; // Populate array names for (int z = 0; z < names.length; z++) { System.out.println("Enter name of salesmen " + (z + 1) + ": "); names[z] = kbd.nextLine(); } for (int z = 0; z < address.length; z++) { System.out.println("Enter address of salesmen " + (z + 1) + ": "); address[z]= kbd.nextLine(); } for (int z = 0; z < age.length; z++) { System.out.println("Enter age of salesmen " + (z + 1) + ": "); age[z]= kbd.nextLine(); } // Sort the names array balloonSortArray2(names); //Show elements of names array System.out.print("\n"); System.out.println("============================"); System.out.printf("Sorted name of salesmen \n"); showElements(names); // Sort the address array balloonSortArray2(address); //Show elements of address array System.out.print("\n"); System.out.println("============================"); System.out.printf("Sorted address of salesmen \n"); showElements(address); // Sort the age array balloonSortArray2(age); //Show elements of age array System.out.print("\n"); System.out.println("============================"); System.out.printf("Sorted age of salesmen \n"); showElements(age); } public static void balloonSortArray2(String[] given) { for (int x = 0; x < given.length - 1; x++) { for (int y = x + 1; y < given.length; y++) { if (given[x].compareTo(given[y]) > 0) { String temp = given[x]; given[x] = given[y]; given[y] = temp; } // end of if } // end of second for } // end of first for } // end of method public static void showElements(String[] array) { for (int x = 0; x < array.length; x++) System.out.println(array[x]); } }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Problem: Please help me modify my program,
When you entered a letter in age, It will not accept it and will display "Invalid Input".
Do the same for name, when a number is entered it must display "Invalid Input"


package examples;

import java.util.*;

public class sales
{
public static void main(String[] args)
{
Scanner kbd = new Scanner(System.in);
String[] names;
String[] address;
String[] age;
// String[] age;
int n = 0;
boolean successfulRead=false;
do
{
try
{
System.out.print("How many names will be sorted: ");
n = Integer.parseInt(kbd.nextLine());
if (n >=0 )
{
successfulRead = true;
}
else
{
System.out.println("The amount cannot be a negative amount");
successfulRead = false;
}
}
catch (NumberFormatException e)
{
successfulRead = false;
System.out.println("\f You have enetered an invalid amount");
}
} while (!successfulRead);
names = new String[n];
address = new String[n];
age = new String[n];
// Populate array names
for (int z = 0; z < names.length; z++)
{
System.out.println("Enter name of salesmen " + (z + 1) + ": ");
names[z] = kbd.nextLine();
}
for (int z = 0; z < address.length; z++)
{
System.out.println("Enter address of salesmen " + (z + 1) + ": ");
address[z]= kbd.nextLine();
}
for (int z = 0; z < age.length; z++)
{
System.out.println("Enter age of salesmen " + (z + 1) + ": ");
age[z]= kbd.nextLine();
}
// Sort the names array
balloonSortArray2(names);

//Show elements of names array
System.out.print("\n");
System.out.println("============================");
System.out.printf("Sorted name of salesmen \n");
showElements(names);
// Sort the address array
balloonSortArray2(address);
//Show elements of address array
System.out.print("\n");
System.out.println("============================");
System.out.printf("Sorted address of salesmen \n");
showElements(address);
// Sort the age array
balloonSortArray2(age);
//Show elements of age array
System.out.print("\n");
System.out.println("============================");
System.out.printf("Sorted age of salesmen \n");
showElements(age);

}

public static void balloonSortArray2(String[] given)
{
for (int x = 0; x < given.length - 1; x++)
{
for (int y = x + 1; y < given.length; y++)
{
if (given[x].compareTo(given[y]) > 0)
{
String temp = given[x];
given[x] = given[y];
given[y] = temp;
} // end of if
} // end of second for
} // end of first for
} // end of method

public static void showElements(String[] array)
{
for (int x = 0; x < array.length; x++)
System.out.println(array[x]);
}
}

Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY