Task 1: What kind of errors are is caused by the following code, if any? How wou
ID: 3699024 • Letter: T
Question
Task 1: What kind of errors are is caused by the following code, if
any? How would we fix them?
//Task 1a.
String[] arr = new String[10];
arr[10] = "This is a String";
1a. Error:__________________________________
1a. How to Fix
It:___________________________
---------------------------------------------
------------------------------
//Task 1b.
String[] arr = new String[-1];
1b. Error:__________________________________
1b. How to Fix
It:___________________________
---------------------------------------------
------------------------------
//Task 1c.
String arr = new String[10];
1c. Error:__________________________________
1c. How to Fix
It:___________________________
---------------------------------------------
------------------------------
//Task 1d.
String[] arr = new String[10];
System.out.println(arr[2]);
1d. Error:__________________________________
1d. How to Fix
It:___________________________
---------------------------------------------
------------------------------
//Task 1e.
String[] arr = new String[10];
arr[5] = 17;
1e. Error:__________________________________
1e. How to Fix
It:___________________________
---------------------------------------------
------------------------------
//Task 1f.
String arr[] = new String[10];
1f. Error:__________________________________
1f. How to Fix
It:___________________________
---------------------------------------------
------------------------------
Task 2:
Have the user store values in an array.
2a. Create a new Java project, and ask the user how many
values they would like to store.
2b. Initialize the global String array "data" using the
input as size
2c. Create a method called "populateArray" which will
contain a loop that asks the user to
input each value and store them
within the global array "data"
2d. Print the contents of the changed
array. Use a loop.
Task 3:
Have the user access or modify a value in the array.
3a. Create a method called "changeArray" which will ask
the user for an index (location) in the array
3b. Print out the value at the specified index
3c. Ask the user if they would like to change that value.
If so, get a value from the user and modify "data"
at the specified index to match the
user's input.
3d. Print the contents of the changed
array. Use a loop.
Explanation / Answer
Here are the required solutions for Task1 questions.
Task 1: What kind of errors are is caused by the following code, if any? How would we fix them?
//Task 1a.
String[] arr = new String[10];
arr[10] = "This is a String";
1a. Error: will throw an ArrayIndexOutOfBoundsException
1a. How to Fix
It: Array indices range from 0 to SIZE-1, here we initialize an array with 10 capacity. So the possible index range from 0 to 9. There are two fixes – either usea valid index (0-9), or increase the array size (with capacity 11 or more), then we can safely access index 10.
//Task 1b.
String[] arr = new String[-1];
1b. Error: It will throw a NegativeArraySizeException
1b. How to Fix
It: Array size cannot be negative at any circumstances. Therefore the only possible fix is to initialize the array with a positive number.
//Task 1c.
String arr = new String[10];
1c. Error: Cannot convert from String array to String
1c. How to Fix
It: The left side of the assignment operator is a String variable, whereas the right side is a String array. We can solve the error by putting a pair of square brackets after arr or String , on the left side of the assignment operator (String arr[] = new String[10] )
//Task 1d.
String[] arr = new String[10];
System.out.println(arr[2]);
1d. Error: There is no error actually
1d. How to Fix
It: The statements are syntactically correct, but as the index 2 doesn’t have any values as of now, it will just print null. Set some value to that location before printing. (arr[2]=”Some String”; )
//Task 1e.
String[] arr = new String[10];
arr[5] = 17;
1e. Error: Will throw a compilation error ‘cannot convert from int to String’
1e. How to Fix
It: The error is happening because 17 is an integer, not a String value, put inside double quotes and it will work fine. ( arr[5]=”17”; )
//Task 1f.
String arr[] = new String[10];
1f. Error: No error
1f. How to Fix
It: There is no error to fix.
Here is the required program for tasks 2 and 3
// ArrayTest.java
import java.util.Scanner;
public class ArrayTest {
static String stringArray[];
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("How many values you would like to store?");
int n=Integer.parseInt(scanner.nextLine());
stringArray=new String[n];//initializing the array
populateArray();//populating
printArray();//printing
changeArray();//changing
printArray();//printing again
}
/**
* method to populate the array
*/
static void populateArray(){
for(int i=0;i<stringArray.length;i++){
System.out.println("Enter value "+(i+1)+": ");
stringArray[i]=scanner.nextLine();
}
}
/**
* method to print the array
*/
static void printArray(){
for(int i=0;i<stringArray.length;i++){
System.out.println(stringArray[i]);
}
}
/**
* method to change an array element
*/
static void changeArray(){
System.out.println("Enter the index value: ");
int n=Integer.parseInt(scanner.nextLine());
if(n<0 || n>stringArray.length){
System.out.println("Invalid index");
}else{
System.out.println("Value: "+stringArray[n]);
System.out.println("Do you want to change this? (y/n)");
String choice=scanner.nextLine();
if(choice.equalsIgnoreCase("y")){
System.out.println("Enter new value: ");
stringArray[n]=scanner.nextLine();
}
}
}
}
/*OUTPUT*/
How many values you would like to store?
5
Enter value 1:
Alice
Enter value 2:
Bob
Enter value 3:
Chris
Enter value 4:
Dave
Enter value 5:
Eric
Alice
Bob
Chris
Dave
Eric
Enter the index value:
3
Value: Dave
Do you want to change this? (y/n)
y
Enter new value:
Zaki
Alice
Bob
Chris
Zaki
Eric
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.