Why doesn't this program execute? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Why doesn't this program execute?

what did I forget? https://code.sololearn.com/cD17ll0J4a3A/?ref=app

11th Oct 2017, 9:23 PM
Spinoza
Spinoza - avatar
6 Answers
+ 1
This might be closer to what you're looking for: import java.text.MessageFormat; import java.util.Scanner; public class Program { public static void main(String[] args) { System.out.println("enter the number of elements to save word in:"); Scanner sc = new Scanner(System.in); int n; n = sc.nextInt(); sc.nextLine(); // remove the remaining return '\n' character from the stream String[] arr = new String[n]; for(int i = 0;i<arr.length;i++) { System.out.println(MessageFormat.format("element number {0}:",i+1)); // i+1 to list starting with 1 instead of 0 arr[i] = sc.nextLine(); // Use nextLine() to get the whole string instead of just up to the 1st space or newline. } int input; input = sc.nextInt() - 1; // Expecting user to input value of element from list above if(input < arr.length && input >= 0) // Add check to make sure negative value isn't input { System.out.println(arr[input]); } else { System.out.println("i did not find this such element"); } } }
12th Oct 2017, 12:55 AM
ChaoticDawg
ChaoticDawg - avatar
+ 4
lol in my defense it's not blind if the text was different. change ln 11 to: n = reader1.nextInt(); (gotta match var names)
11th Oct 2017, 10:49 PM
Jordan Chapman
Jordan Chapman - avatar
+ 1
I try it thank u so much
12th Oct 2017, 8:02 AM
Spinoza
Spinoza - avatar
0
programs can't feel emotions silly goose
11th Oct 2017, 10:40 PM
Jordan Chapman
Jordan Chapman - avatar
0
see the question again Mr.Blind
11th Oct 2017, 10:46 PM
Spinoza
Spinoza - avatar
0
but is not working yet
11th Oct 2017, 11:20 PM
Spinoza
Spinoza - avatar