Java programming:
The feedback I received "responds badly to bad input. Fix 2"
Original question:
Write a program that creates a small (6-10) array of ints. Display your array elements, all on one line, using a foreach loop. In a try block, prompt the user to enter an index for the array and attempt to print the element with that index. Follow the try block with two catch blocks; one that detects an index out of bounds, and another that catches other bad inputs. Make sure the catch blocks are properly ordered (see page 464). Include a finally block that reports the size of the array. See Sample Runs below.
Sample Run 1
12 15 24 5 9 16
Enter any index of your array 5
The element at index 5 is 16
The array size is 6
Sample Run 2
12 15 24 5 9 16
Enter any index of your array 9
Error. Array index was out of bounds
The array size is 6
Sample Run 3
12 15 24 5 9 16 12 22
Enter any index of your array three
Bad input. Try again
The array size is 8
______________________________________________________________________________________________
My code:
import java.util.*;
public class Program2 {
public static void main(String[] args) {
Scanner input = new
Scanner(System.in);
int[] theElements = { 12, 15, 24, 5, 9, 16 };
int j = 1, index = -1;
String select;
while (true) {
System.out.println("Sample Run " + j);
System.out.print(Arrays.toString(theElements));
System.out.print("\nEnter any index of your array ");
try {
index = input.nextInt();
} catch
(InputMismatchException ex) {
System.out.print("Bad input. Try again");
}
// for each
loop
for (int i :
theElements) {
if (index > theElements.length || index <
0) {
System.out.print("\nError.
Array index was out of bounds");
} else {
System.out.print("\nThe
element at index " + index + " is " + theElements[index]);
System.out.print("\nThe array
size is " + theElements.length);
break;
}
}
j++;
System.out.print(" want to continue (y/n) ");
select =
input.next();
if (select ==
"y" || select == "Y") {
continue;
} else {
break;
}
}
}
}
import java.util.*;
public class Program2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] theElements = { 12, 15, 24, 5, 9, 16 };
int index = -1;
System.out.print(Arrays.toString(theElements));
System.out.print("\nEnter any index of your array ");
try {
index = input.nextInt();
} catch (InputMismatchException ex) {
System.out.print("Bad input. Try again");
}
// for each loop
for (int i : theElements) {
if (index > theElements.length || index < 0) {
System.out.print("\nError. Array index was out of bounds");
} else {
System.out.print("\nThe element at index " + index + " is " + theElements[index]);
System.out.print("\nThe array size is " + theElements.length);
break;
}
}
}
}


Java programming: The feedback I received "responds badly to bad input. Fix 2" Original question: Write...
Java programming question: Here is the feedback I received "sort fails. Use Arrays class method to sort." The actual question: Write a program as follows: Declare a five element array of Strings. Use a for loop and keyboard input to populate the array with the names of five friends. Sort the array in alphabetical order. Use a foreach loop to print the sorted array of friends, all on one line. Sample Output (inputs in italics) Enter the names of five...
JAVA:
(15 marks) Write a program that creates an integer array with 50 random values, prompts the user to enter the index of an element in the array between 0 and 49, then displays the corresponding element value. If the specified index is out of bounds, display an error message (e.g. "Out of Bounds") and ask the user to enter another index. Use a while loop that will keep prompting the user until a valid input is received. To handle...
Assignment 11 – Exceptions, Text Input/Output - Random Numbers Revised 9/2019 Chapter 12 discusses Exception Handling and Input/Output. Using try/catch/finally statement to handle exceptions, or declare/throw an exception as needed, create the following program: Create an array of 25 random numbers between 0 & 250 formatted to a field width of 10 & 4 decimal places (%10.4f). Use the formula Math.random() * 250. Display the array of random numbers on the console and also write to a file. Prompt the...
Given the ExceptionOriginal Class, run this class for Run 1plugin values 12 and 5, Run 2 plugin values 24 and 0, Run 3 plugin values 2e and view the exceptions thrown, if any. Now modify the ExceptionOriginal class and place a test restricting user from dividing by o --- use if.... else block and display appropriate messages. Run 1 plugin values 12 and 5, Run 2 plugin values 24 and 0, and view the exceptions thrown, if any. Now modify...
in java, my IF statement is being ignored & my print
statement won't work..why?
Start Page xIẾ》 Smallestinteger.java x 1|曰import java.util. Scanner; 3public class SmallestInteger i public static void main (String[] args) i Scanner input - new Scanner (System.in); int lowValue0 int nextöne; 10 int counter: 12 System.out.print ("How many integers?" 13 int index = input .next Int ( ) ; 14 15 for (counter=1; counter index -counter){ 16 System.out.print ("Enter integer, please: "); 17 nextOne - input.nextInt ) 18...
I have the following java-program, that creates a random walk
starting at (0,0) and continuing until x or y is greater than
abs(n).
First: I get an error, when I try closing the Scanner by
inserting "reader.close(); " in line 28. It says "Unreachable
code". How can I fix that?
Second: Is it possible to create a different colour for the
StdDraw.point when it reaches a point on one of the edges "n+1" or
"n-1" ?
1 import java.util.*; 3...
My Question is: I have to modify this program, even a small modification is fine. Can anyone give any suggestion and solution? Thanks in Advanced. import java.util.*; class arrayQueue { protected int Queue[]; protected int front, rear, size, len; public arrayQueue(int n) { size = n; len = 0; Queue = new int[size]; front = -1; rear = -1; } public boolean isEmpty() { return front == -1; } public boolean isFull() { return front == 0 && rear ==size...
PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java, Write a computer program that prompts the user for one number, n for the number of items in the array to sort, and create and sort 1000 arrays of this size timing the run to get an average time to sort an array of this size. Then do the following: Initiate a variable running_time to 0 Create a for loop that iterates 1000 times....
Write a Java program to input a number of values into an array and then process the array by passing it to various methods that will compute and return information we are interested in. These include: 1. the average of the values in the array 2. the standard deviation of the values in the array 3. the number of items in the array less than the average 4. whether the array’s values are in increasing order or not First, you...
need help editing or rewriting java code, I have this program
running that creates random numbers and finds min, max, median ect.
from a group of numbers,array. I need to use a data class and a
constructor to run the code instead of how I have it written right
now. this is an example of what i'm being asked
for.
This is my code:
import java.util.Random;
import java.util.Scanner;
public class RandomArray {
// method to find the minimum number in...