51603

Tables trouble. I need help to make them

Question:

I want to make a list going to n numbers depending on user input. I then want to put a second number in each place and print the entire table. As for testing I have tried with a length 4 and numbers 1,2,3,4 but I get a error: ArrayIndexOutOfBounds. I wanted it to print 1,2,3,4.

Scanner keyboard = new Scanner (System.in); System.out.println("Whats the length of the table?"); int lengde = keyboard.nextInt(); int[] minTabell = new int[lengde]; for (int i =1; i <= lengde+ 1; i++) { System.out.println((i) + (" give a number")); minTabell[i] = keyboard.nextInt(); } System.out.println(minTabell); keyboard.close();

Answer1:

Indexes in Java arrays are 0-based, while your for-loop starts from 1. So,

for (int i =1; i <= lengde+ 1; i++) { System.out.println((i) + (" give a number")); minTabell[i] = keyboard.nextInt(); }

should be

for (int i =0; i < lengde; i++) { // ^ ^^^^^^^^ System.out.println((i+1) + (" give a number")); // ^^^ minTabell[i] = keyboard.nextInt(); }

As for printing the content of the array, I suggest you use

for (int i : minTabell) System.out.println(i);

Answer2:

In Java array indexing starts at 0. The first element is positioned at minTabel1[0]. Your for-loop runs from 1 to lengde + 1, which means that you will try to fill a position outside of the array.

Answer3:

The first element of an array has the index 0. The last valid index of your array is lengde-1.

Try this:

for (int i=0; i < lengde; i++) { System.out.println((i) + (" give a number")); minTabell[i] = keyboard.nextInt(); }

To print the array, i suggest the following:

System.out.println(Arrays.toString(minTabell));

Recommend

  • Attraction Force as coding
  • Removing last random string from arraylist
  • Not able to send messages to kafka topic
  • Same values for every key in hashmap
  • java add items to arraylist at random time intervals
  • How would you share a random value across many classes?
  • Problem with cometd and jetty 6 / 7
  • java use Regular Expressions to generate a string [duplicate]
  • How to generate random events in android?
  • Inputting a number then reversing it
  • How can I get a specific line from a text file? [duplicate]
  • What does a hyphen at end of a term mean
  • On which thread JavaFX change listeners are executed ?
  • Why does IE8 fail to resolve my JQuery selector for a checked radio option?
  • parsing numbers from a text file
  • Rails AREL .where statement
  • UIBarButtonItem's action is not called when in a view with a UIGestureRecognizer
  • C++/CLI Thread synchronization including managed and unmanaged code
  • Can a Collections.shuffle be considered equivalent to a series of Randoms?
  • Specifying virtual keyboard type for EditText in XML
  • EditText is covered by Keyboard
  • Motorola barcode scanner SDK events C#
  • If statement skipping right to else after being called once?
  • jQuery: How to AJAXify WordPress Search?
  • using System.Speech.Synthesis with Windows10 universal app (XAML-C#)
  • Android device acting as an accessory
  • Reading a file into a multidimensional array
  • What is the purpose of TaskExecutor in spring?
  • how to find common suffix in java by using method
  • MySQL Order by column = x, column asc?
  • App restarts from wrong activity
  • Alternative to overridePendingTransition() - Android
  • Jetty Server not starting: Unable to establish loopback connection
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Validaiting emails with Net.Mail MailAddress
  • sending/ receiving email in Java
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • How does Linux kernel interrupt the application?