When running my program, nothing is shown in the console - java

public class quartz {
public static void main (String[] args) {
int i = 0;
int sum = 0;
int[] n1 = {54, 40, 37, 64, 81, 15, 65, 72, 61, 57, 83, 3, 67, 12, 30, 54, 11, 51, 3, 78, 48, 31, 68, 77, 64, 68, 95, 68, 35, 82, 57, 26, 67, 41, 47, 80, 36, 88, 5, 9, 55, 87, 77, 8, 65, 31, 7, 79, 49, 22, 32, 94, 34, 12, 20, 30, 91, 12, 57, 77, 37, 96, 22, 29, 17, 76, 36, 56, 80, 33, 20, 65, 57, 40, 50, 97, 20, 92, 25, 14, 19, 84, 12, 62, 20, 42, 99, 52, 88, 29, 75, 48, 27, 73, 46, 72, 48, 84, 19, 55};
while (i < n1.length)
sum += n1[i];
i = 0;
System.out.println("Sum: " + sum + ".");
System.out.println("Average: " + (sum/n1.length) + ".");
double[] NE = {-0.6179074665219488, 0.012080423982449795, -0.21346000509541063, 0.08299652983289585, 2.44401680106775, 4.902778859313734, 1.7132831483350532, -4.2004763396051725, -4.043856195861675, -2.6728507023602326, 5.181911533071974, -2.1235877432845354, 7.603895698367564, -5.6730748575837975, -3.5868701412258464, 8.50824673494424, 1.9530312960520657, 1.4057711751329447, -6.6010520166956885, -0.8889270825881894, -3.066437913144831, -1.047977711607209, -0.6183425325427638, -1.9567474971238643, -1.6108985491087715, -3.4762343504063105, -0.7819967483948718, 1.2787199500848474, -1.724036624119682, 4.134045084705252, -4.302090407212001, 4.522452909896921, -9.510982189042458, 4.483571903648103, 4.734972592935479, 1.3007048393668028, -1.5733757395516363, -1.9343054344201707, 6.787212280236046, -0.35603892339489995, -1.3414921239899753, -0.9393551256779856, -1.9298884254368263, 6.043295105337908, 2.9330671137121733, 2.8561036619044047, -0.8293767467550212, 6.123622142714353, -2.2350961485598777, 7.20722805161423, 5.515389689089437, -2.7666432567169745, -4.344590134196103, 3.3453531590362613, 10.413288779778698, 0.3986583788822756, 1.8156402784897105, -6.495232639280744, -2.5586148068696852, 2.456750085945401, -2.0241915465317994, 2.6640207424833706, -3.221638093253812, -0.13291701098446618, 4.525894152095317, 3.833943185257407, -2.892260297173234, -3.247865929061468, 6.129696012756685, 4.451839001858698, -3.142375819178058, -1.0758596832313212, -7.85705595464708, -3.376343621066232, -3.993944532318441, 13.146850947670861, -1.3900676627648902, 3.8600378751921256, 3.9652071948870447, -2.4382860496298324, 3.0864605092488304, -10.769089293963074, 1.9773754511588617, 4.826841112732377, 7.9219782116860324, -3.266132871461332, 1.8118819669439024, 0.698579723806034, 7.119629551067371, -0.9141128559070014, 1.5143207368301361, -8.587596597534729, -0.9387144566983379, 2.5641381148921805, 10.628593146418485, 3.794317923770138, 6.2802756227726615, -0.05171930511667566, 0.8736426098894451, 0.6226851580000003};
double[] NW = {9.631212195521316, -2.1235831279282698, 3.7468670477204773, -4.559878135521824, 3.2444286767576545, 6.2877828741148605, 6.520597627024687, 2.642307472836288, 1.2002893113069557, 0.32620641006622675, -3.368962812990781, -2.588868228199504, 4.356518441561512, -0.5955112302723241, 0.3875648501871751, -2.9311051175998064, 2.0095554763173666, -1.228769483871199, 5.900445902470515, 0.41527619439744434, 2.9752128071432145, 4.805920315662717, -4.797853823364673, 5.752192282393844, 2.9073605365834556, 2.4870719041084497, -1.7994046436584152, 7.79554996548367, 4.4174973514255536, 2.084039895979635, 0.6281302992116424, -3.1466915662704524, 3.646400672147826, 0.9609952887592054, -6.070082172976056, -0.9392599054917704, 0.904301836858967, 5.926867039519574, 3.238559698585232, -4.439332575192746, 1.352444182896236, -0.24594080100384297, -1.6395807550351367, 3.591208179788307, 4.15757174804611, -3.5334824535956173, 0.5302366137985215, 9.564674975899017, 4.175389023096817, -0.9827335882191762, 4.305890552392608, 3.059083687714633, 2.3224548745551488, 0.1934380213592375, 1.0235814, 1.1716370685853148, -2.931711339626567, -4.214035402157694, -1.0093422753964358, -4.843082160061708, -7.148710177896536, -1.910725804980465, -0.22905523068711164, 3.8200222938181367, -1.744095856344644, 1.354958988184811, 0.9933832752568843, 0.8820951391051288, -2.062035935350486, -7.633897329029599, 0.49911238393151325, -1.1684033502541722, 4.090099097765502, 4.566828839384462, 0.6901115935421007, -4.30695891725898, -5.637531096381548, 2.6920329212478507, -1.522395621132775, 6.351734133984433, 0.4895678835360122, -4.755548841958967, -2.826990702897114, 1.974618789378563, -6.999938959339396, 0.6289774718852977, 3.2732671487606266, -1.2781272997669557, 6.725303989648547, -7.163688015215646, 5.547683884070125, -3.0189942298996213, -0.2487963910538069, -0.46314538549764894, 5.3913279138183645, -4.018219623545416, 6.491084381355617, -1.5629014732514819, -6.557894883162792, -3.856421007612216};
int sumEeven = 0, sumEodd = 0, sumWeven = 0, sumWodd = 0;
while (i < 99) {
sumEeven += NE[i];
sumEodd += NE[i+1];
i += 2;
}
i = 0;
while (i < 99) {
sumWeven += NW[i];
sumWodd += NW[i+1];
i += 2;
}
System.out.println("Front coordinates:");
System.out.println("Latitude: " + sumEeven + " Longitude: " + sumEodd);
System.out.println();
System.out.println("Back coordinates:");
System.out.println("Latitude: " + sumWeven + " Longitude: " + sumWodd);
}
}
This code has no errors, and runs, but the console is empty. I have my run configurations set up correctly, so not sure what the issue is. I also made sure that my console was linked to this project and not another, and it is.
I'm running Eclipse Neon if that changes anything.
Some info about the project, although seemingly irrelevant:
n1 calculates the sum of that array of ints and then take the average of the sum
NE and NW calculate the sum of the even parts of each array and the odd parts of each array, and adds each up individually, and they end up being latitudinal and longitudinal coordinates.

I think your program will never leave this loop what are you trying to do here:
while (i < n1.length)
sum += n1[i];
0 < n1.lenth it's always true
try this and see
while (i < n1.length){
sum += n1[i];
i++;
}

Related

Write a program to display even numbers from 1 to 100 seperated by comma

Write a program to display even numbers from 1 to 100 seperated by
comma
public class Evennumbers {
public static void main(String[] args) {
int i=1;
while(i<=100)
{
if(i%2==0)
System.out.print( i+" ,");
i++;
}
}
}
The Out put of the program is placed below :
2 ,4 ,6 ,8 ,10 ,12 ,14 ,16 ,18 ,20 ,22 ,24 ,26 ,28 ,30 ,32 ,34 ,36 ,38
,40 ,42 ,44 ,46 ,48 ,50 ,52 ,54 ,56 ,58 ,60 ,62 ,64 ,66 ,68 ,70 ,72
,74 ,76 ,78 ,80 ,82 ,84 ,86 ,88 ,90 ,92 ,94 ,96 ,98 ,100 ,
/** I want to remove comma after 100 **/
Try it like this. Print the first number and then print the rest preceded by a comma.
String comma = "";
for(int i = 2; i <= 100; i+= 2) {
System.out.print(comma + i);
comma = ", ";
}
prints
2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42,
44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82,
84, 86, 88, 90, 92, 94, 96, 98, 100

looking for a sorting algorithm that can sort 1-100 using the least amount of statements

As the title says, I am looking for a sorting algorithm that can sort the numbers between 1 and 100 using the least amount of statements in only one method which is the main.
Here's an example to give you an idea on how I can check it(using a counter)
class Solution{
public static void main(String[] args) {
//declaration
int count = 0,inner, outer, temp, h = 1; count++;
int [] array = {100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1,}; count++;
System.out.println("Before: " + Arrays.toString(array)); count++;
//start of the sort
while (h <= array.length / 3) //start of while loop 1
{
h = h * 3 + 1; count++;
}count++; //end of while loop 1
while (h > 0) //start of while loop 2
{
for (outer = h; outer < array.length; outer++) //start of for loop
{
temp = array[outer]; count++;
inner = outer;count++;
while (inner > h - 1 && array[inner - h] >= temp) //start of while loop 3
{
array[inner] = array[inner - h]; count++;
inner -= h; count++;
}count++; //end of for loop 3
array[inner] = temp; count++;
}count++; //end of for loop
//equation to count for
count = count + array.length-h+1 + array.length - h +1 -1;
h = (h - 1) / 3; count++;
}count++; //end of while loop 2
System.out.println("After: " + Arrays.toString(array) + "\n" + "Counter = " + count);
}
}
The fastest algorithm with example would be quicksort, also as you are asking i believe this would also take the least amount of statments. Below you can find a link and also description of how it works.
https://www.geeksforgeeks.org/quick-sort/

Java: Searching an unsorted integer array for the first occurrence of a user inputted integer

I've noticed two problem and would really appreciate the help!
With (what I believe) to be with how the methods "arrayIndex" and "position" are interacting with each other. I say this because when I enter the same exact numbers for both of the fields they return the same exact values even though they are supposed to handle completely different tasks.
With my "position" method which is supposed to return the location of the searched values first occurrence in the array it seems to return incorrect values and after inspecting it I still can not figure out why. If the user searches for a values position that doesn't exist it's supposed to return a "-1".
My Entire Code:
import java.util.Scanner;
public class Prog9ArrayMethods {
public static void main(String[] args) {
// Daily high temperatures for Portland Maine Jan 1 - Dec 31 2015
int[] tmax = {32, 38, 34, 35, 41, 17, 25, 17, 29, 24, 26, 33, 31, 24,
29, 38, 20, 49, 49, 36, 31, 38, 35, 32, 37, 20, 17, 26,
30, 32, 22, 26, 12, 20, 35, 34, 19, 28, 22, 15, 30, 23,
20, 17, 16, 19, 21, 21, 32, 33, 19, 34, 35, 31, 19, 34,
21, 27, 27, 30, 36, 32, 46, 39, 23, 38, 40, 44, 47, 56,
41, 39, 38, 36, 45, 44, 28, 32, 34, 36, 35, 34, 39, 42,
49, 49, 41, 41, 40, 48, 45, 46, 66, 49, 48, 41, 47, 42,
35, 43, 54, 68, 66, 70, 65, 55, 67, 55, 57, 48, 63, 60,
53, 54, 55, 56, 58, 63, 57, 60, 55, 54, 62, 76, 75, 72,
84, 58, 59, 83, 68, 82, 64, 68, 70, 63, 74, 61, 65, 67,
69, 67, 65, 83, 84, 91, 79, 80, 77, 84, 73, 51, 50, 61,
60, 58, 73, 67, 65, 68, 81, 86, 80, 85, 78, 61, 61, 75,
72, 80, 69, 72, 72, 67, 82, 78, 67, 70, 59, 69, 75, 68,
78, 80, 71, 82, 82, 76, 84, 72, 84, 87, 90, 78, 76, 82,
76, 74, 70, 81, 84, 70, 82, 78, 76, 67, 67, 77, 83, 88,
86, 86, 86, 81, 81, 80, 82, 80, 76, 80, 77, 77, 67, 80,
77, 80, 85, 85, 89, 86, 83, 75, 73, 78, 70, 79, 75, 80,
79, 77, 75, 81, 86, 80, 84, 86, 72, 78, 82, 92, 89, 86,
78, 73, 74, 62, 73, 83, 85, 82, 83, 75, 72, 69, 65, 74,
74, 63, 63, 67, 74, 75, 69, 62, 55, 58, 58, 61, 69, 67,
63, 59, 56, 68, 70, 62, 68, 57, 61, 57, 46, 48, 66, 58,
65, 54, 47, 62, 54, 52, 59, 73, 58, 51, 58, 64, 64, 64,
68, 69, 65, 53, 58, 53, 47, 53, 60, 46, 53, 54, 47, 47,
53, 59, 46, 42, 42, 42, 41, 51, 61, 57, 41, 32, 38, 44,
45, 47, 51, 51, 57, 39, 45, 53, 48, 57, 47, 48, 56, 42,
50, 46, 40, 38, 47, 49, 47, 51, 62, 51, 43, 34, 23, 28,
44};
int max = arrayMax(tmax);
int min = arrayMin(tmax);
double average = arrayAverage(tmax);
int count = arrayIndex(tmax);
int i = arrayIndex(tmax);
System.out.println("Maximum value is: " + max);
System.out.println("Minimum value is: " + min);
System.out.println("Average value is: " + average);
System.out.println("The number of values above the specified value is: " + count);
System.out.println("The first occurence of the searched value is: " + i);
}
// Returns the maximum value in the array
public static int arrayMax(int[] a) {
int max = a[0];
for (int i = 0; i < a.length; i++)
if (a[i] > max)
max = a[i];
return max;
}
// Returns the minimum value in the array
public static int arrayMin (int[] a) {
int min = a[0];
for (int i = 0; i < a.length; i++)
if (a[i] < min)
min = a[i];
return min;
}
// Returns the average value in the array
public static double arrayAverage(int[] a) {
int sum = 0; // Why does it double the decimal value with "int sum = a[0]"?
double average;
for(int i=0; i < a.length; i++){
sum = sum + a[i];
}
average = (double)sum/a.length;
return average;
}
// Returns the number of values greater than the user's indexed values
public static int arrayIndex(int[] a) {
Scanner user_input = new Scanner( System.in );
System.out.println("Enter a value to search: ");
int userSearch = user_input.nextInt();
int count = 0;
for(int i = 0; i < a.length; i++) {
if(a[i] > userSearch) {
++count;
}
}
return count;
}
public static int position(int [ ] a, int match) {
Scanner user_input = new Scanner( System.in );
System.out.println("Enter a value to search: ");
int userSearch = user_input.nextInt();
for (int i = 0; i < a.length; i++)
{
if ( a[i] == userSearch )
return i;
}
return -1;
}
}
In your main method, when you want to get i to be the count, you actually get arrayIndex() instead of position().
Just change int i = arrayIndex(tmax); to int i = position(tmax); and get rid of the int match() as an argument to position()
Here is the solution for your problem. Your were calling arrayIndex method twice instead of calling position method. I rectified your code. Now it works fine.
import java.util.Scanner;
public class Prog9ArrayMethods {
public static void main(String[] args) {
// Daily high temperatures for Portland Maine Jan 1 - Dec 31 2015
int[] tmax = {32, 38, 34, 35, 41, 17, 25, 17, 29, 24, 26, 33, 31, 24,
29, 38, 20, 49, 49, 36, 31, 38, 35, 32, 37, 20, 17, 26,
30, 32, 22, 26, 12, 20, 35, 34, 19, 28, 22, 15, 30, 23,
20, 17, 16, 19, 21, 21, 32, 33, 19, 34, 35, 31, 19, 34,
21, 27, 27, 30, 36, 32, 46, 39, 23, 38, 40, 44, 47, 56,
41, 39, 38, 36, 45, 44, 28, 32, 34, 36, 35, 34, 39, 42,
49, 49, 41, 41, 40, 48, 45, 46, 66, 49, 48, 41, 47, 42,
35, 43, 54, 68, 66, 70, 65, 55, 67, 55, 57, 48, 63, 60,
53, 54, 55, 56, 58, 63, 57, 60, 55, 54, 62, 76, 75, 72,
84, 58, 59, 83, 68, 82, 64, 68, 70, 63, 74, 61, 65, 67,
69, 67, 65, 83, 84, 91, 79, 80, 77, 84, 73, 51, 50, 61,
60, 58, 73, 67, 65, 68, 81, 86, 80, 85, 78, 61, 61, 75,
72, 80, 69, 72, 72, 67, 82, 78, 67, 70, 59, 69, 75, 68,
78, 80, 71, 82, 82, 76, 84, 72, 84, 87, 90, 78, 76, 82,
76, 74, 70, 81, 84, 70, 82, 78, 76, 67, 67, 77, 83, 88,
86, 86, 86, 81, 81, 80, 82, 80, 76, 80, 77, 77, 67, 80,
77, 80, 85, 85, 89, 86, 83, 75, 73, 78, 70, 79, 75, 80,
79, 77, 75, 81, 86, 80, 84, 86, 72, 78, 82, 92, 89, 86,
78, 73, 74, 62, 73, 83, 85, 82, 83, 75, 72, 69, 65, 74,
74, 63, 63, 67, 74, 75, 69, 62, 55, 58, 58, 61, 69, 67,
63, 59, 56, 68, 70, 62, 68, 57, 61, 57, 46, 48, 66, 58,
65, 54, 47, 62, 54, 52, 59, 73, 58, 51, 58, 64, 64, 64,
68, 69, 65, 53, 58, 53, 47, 53, 60, 46, 53, 54, 47, 47,
53, 59, 46, 42, 42, 42, 41, 51, 61, 57, 41, 32, 38, 44,
45, 47, 51, 51, 57, 39, 45, 53, 48, 57, 47, 48, 56, 42,
50, 46, 40, 38, 47, 49, 47, 51, 62, 51, 43, 34, 23, 28,
44};
int max = arrayMax(tmax);
int min = arrayMin(tmax);
double average = arrayAverage(tmax);
int count = arrayIndex(tmax);
int i = position(tmax);
System.out.println("Maximum value is: " + max);
System.out.println("Minimum value is: " + min);
System.out.println("Average value is: " + average);
System.out.println("The number of values above the specified value is: " + count);
System.out.println("The first occurence of the searched value is: " + i);
}
// Returns the maximum value in the array
public static int arrayMax(int[] a) {
int max = a[0];
for (int i = 0; i < a.length; i++)
if (a[i] > max)
max = a[i];
return max;
}
// Returns the minimum value in the array
public static int arrayMin (int[] a) {
int min = a[0];
for (int i = 0; i < a.length; i++)
if (a[i] < min)
min = a[i];
return min;
}
// Returns the average value in the array
public static double arrayAverage(int[] a) {
int sum = 0; // Why does it double the decimal value with "int sum = a[0]"?
double average;
for(int i=0; i < a.length; i++){
sum = sum + a[i];
}
average = (double)sum/a.length;
return average;
}
// Returns the number of values greater than the user's indexed values
public static int arrayIndex(int[] a) {
Scanner user_input = new Scanner( System.in );
System.out.println("Enter a value to search for arrayIndex: ");
int userSearch = user_input.nextInt();
int count = 0;
for(int i = 0; i < a.length; i++) {
if(a[i] > userSearch) {
++count;
}
}
return count;
}
public static int position(int [ ] a) {
Scanner user_input = new Scanner( System.in );
System.out.println("Enter a value to search: ");
int userSearch = user_input.nextInt();
for (int i = 0; i < a.length; i++)
{
if ( a[i] == userSearch )
return i;
}
return -1;
}
}

Using Insertion Sort to sort an array

I'm supposed to take an array of the numbers: {51, 63, 48, 98, 75, 63, 92, 30, 32, 32, 36, 89, 4, 76, 73, 90, 64, 99, 36, 96} and sort them from lowest to highest, and then highest to lowest.
When I try to print the highest to lowest it makes the first output the same. Does anyone see any errors in my code?
package l7c14sort;
import java.util.Arrays;
public class L7C14Sort {
public static void main(String a[]){
int[] arr1 = {51, 63, 48, 98, 75, 63, 92, 30, 32, 32, 36, 89, 4, 76, 73, 90, 64, 99, 36, 96};
int[] arr2 = doInsertionSort(arr1);
int[] arr3 = doInsertionSortAgain(arr1);
System.out.println("Original input: "+Arrays.toString(arr1)+"\n");
System.out.println("Lowest to highest:\n");
for(int i:arr2)
{
System.out.print(i);
System.out.print(", ");
}
System.out.println("\n\n");
System.out.println("Highest to lowest:\n");
for(int k:arr3)
{
System.out.print(k);
System.out.print(", ");
}
System.out.println("\n");
}
public static int[] doInsertionSort(int[] input){
int temp;
for (int i = 1; i < input.length; i++) {
for(int j = i ; j > 0 ; j--){
if(input[j] < input[j-1]){
temp = input[j];
input[j] = input[j-1];
input[j-1] = temp;
}
}
}
return input;
}
public static int[] doInsertionSortAgain(int[] input2){
int temp2;
for (int k = 1; k < input2.length; k++) {
for(int j = k ; j > 0 ; j--){
if(input2[j] > input2[j-1]){
temp2 = input2[j];
input2[j] = input2[j-1];
input2[j-1] = temp2;
}
}
}
return input2;
}
}
Output:
Original input: [99, 98, 96, 92, 90, 89, 76, 75, 73, 64, 63, 63, 51,
48, 36, 36, 32, 32, 30, 4]
Highest to lowest:
99, 98, 96, 92, 90, 89, 76, 75, 73, 64, 63, 63, 51, 48, 36, 36, 32, 32, 30, 4,
Lowest to highest:
4,30,32,32,36,36,48,51,63,63,64,73,75,76,89,90,92,96,98,99
Good news: Your algorithm works fine.
In Java, arrays are passed by reference, not by value. What this means is that when you set int[] arr2 = doInsertionSort(arr1);, the array arr2 is being set to the result of your doInsertionSort method, which returns its input parameter after sorting it. Basically, arr1, arr2, arr3, input and input2 are all pointing to the very same array.
You have two easy options to fix the fact that you're printing:
Restructure main() so that you use one array: print its contents, sort it lowest to highest, print its contents again, sort it highest to lowest, then print its contents again. (This is probably what your instructor intends for you to do, if this is coursework.)
Make a copy of the input parameter to operate on. You can do this with System.arraycopy() like so:
int[] myArray;
System.arraycopy(input, 0, myArray, 0, input.length );
Then, for option 2, you would need to edit your method to use myArray instead of input for every other time you use input.
As a note, you don't need to call your variables input2, temp2, etc. Just like i, j and k go out of scope and are forgotten after the end of a for loop, your variables input and temp mean nothing outside of the block you declared them in.
Hope this helps!
You are ended up with same results, because arrays are mutable. Because of the following code, input array is mutated and its final value is printed.(Highest to lowest).
int[] arr2 = doInsertionSort(arr1);
int[] arr3 = doInsertionSortAgain(arr1);
If you organize your code like:
public static void main(String a[]) {
int[] arr1 = {51, 63, 48, 98, 75, 63, 92, 30, 32, 32, 36, 89, 4, 76, 73, 90, 64, 99, 36, 96};
System.out.println("Original input: " + Arrays.toString(arr1) + "\n");
System.out.println("Lowest to highest:\n");
int[] arr2 = doInsertionSort(arr1);
for (int i : arr2) {
System.out.print(i);
System.out.print(", ");
}
System.out.println("\n\n");
System.out.println("Highest to lowest:\n");
int[] arr3 = doInsertionSortAgain(arr1);
for (int k : arr3) {
System.out.print(k);
System.out.print(", ");
}
System.out.println("\n");
}
You are going to get:
Original input: [51, 63, 48, 98, 75, 63, 92, 30, 32, 32, 36, 89, 4, 76, 73, 90, 64, 99, 36, 96]
Lowest to highest:
4, 30, 32, 32, 36, 36, 48, 51, 63, 63, 64, 73, 75, 76, 89, 90, 92, 96, 98, 99,
Highest to lowest:
99, 98, 96, 92, 90, 89, 76, 75, 73, 64, 63, 63, 51, 48, 36, 36, 32, 32, 30, 4,

Java ArrayIndexOutOfBoundsException: 20 multidimensional array

I am a new programmer, I have had a class but haven't been back to school yet, so I am trying to get ahead on my own when I have time b doing the problems at projecteuler.net. I have searched on this site and on google for the solution but all of their fixes are using the wrong variable in the for loops which I checked multiple times.
The exception:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20
at Euler11.main(Euler11.java:37)
My code:
public class Euler11 {
/**
* #param args
*/
public static void main(String[] args) {
int[][] grid = new int[][] {
{8, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 2, 12, 50, 77, 91, 8},
{49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00},
{81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 03, 49, 13, 36, 65},
{52, 70, 95, 23, 04, 60, 11, 42, 69, 24, 68, 56, 01, 32, 56, 71, 37, 02, 36, 91},
{22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80},
{24, 47, 32, 60, 99, 03, 45, 02, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50},
{32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70},
{67, 26, 20, 68, 02, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21},
{24, 55, 58, 05, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72},
{21, 36, 23, 9, 75, 00, 76, 44, 20, 45, 35, 14, 00, 61, 33, 97, 34, 31, 33, 95},
{78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 03, 80, 04, 62, 16, 14, 9, 53, 56, 92},
{16, 39, 05, 42, 96, 35, 31, 47, 55, 58, 88, 24, 00, 17, 54, 24, 36, 29, 85, 57},
{86, 56, 00, 48, 35, 71, 89, 07, 05, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58},
{19, 80, 81, 68, 05, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 04, 89, 55, 40},
{04, 52, 8, 83, 97, 35, 99, 16, 07, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66},
{88, 36, 68, 87, 57, 62, 20, 72, 03, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69},
{04, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36},
{20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 04, 36, 16},
{20, 73, 35, 29, 78, 31, 90, 01, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 05, 54},
{01, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 01, 89, 19, 67, 48}
};
long product = 0;
long hp = 0;
long vp = 0;
long d1p = 0;
long d2p = 0;
for (int h = 3; h < 23; h++) {
for (int v = 3; v < 23; v++) {
hp = grid[h][v] * grid[h][v + 1] * grid[h][v + 2] * grid[h][v + 3];
vp = grid[h][v] * grid[h + 1][v] * grid[h + 2][v] * grid[h + 3][v];
d1p = grid[h][v] * grid[h + 1][v + 1] * grid[h + 2][v + 2] * grid[h + 3][v + 3];
d2p = grid[h][v] * grid[h - 1][v + 1] * grid[h - 2][v + 2] * grid[h - 3][v + 3];
if (hp > product) {
product = hp;
}
if (vp > product) {
product = vp;
}
if (d1p > product) {
product = d1p;
}
if (d2p > product) {
product = d2p;
}
}
}
}
}
I apologize for any sloppiness in the code and if you have any advice on that I am always willing to accept criticism.
Looking over the documentation for the ArrayIndexOutOfBoundsException, it mentions negative numbers and <= symbols but I do not have any that I have noticed.
I tried setting h = 0 and v = 0 and having them go to 19 instead of 23 and got the exception but it said -1 instead of 20 and was on line 40. The MultiDimensional array was my friends idea and I feel like there is probably an easier way, but I don't know what it could be so I went with his suggestion. If you have a site that you used to find the answer and could link it I would appreciate it.
Thank you ahead of time for any advice you can give me.
I don't want to come up with the answer for you, since this is for your learning, and that wouldn't help out much. But I will give a hint for you:
ArrayOutOfBoundsException means that you tried to access an element of an array that doesn't exist. You've got a 20x20 array, so you can use integers between 0 and 19 to access the elements in the array (Remember that arrays are 0-indexed, meaning they start counting from 0 rather than 1). Think on this: Is your code ever trying to access the array with numbers outside of that range?
These array bounds issues are quite common when starting programming, and they are a little confusing at first. Once you understand them, however, they're trivial mistakes forever after.
Your array is 20x20 elements. Inside code you access indexes with [h+3] (maximum) and [h-3] (minimum). Use the for loops like this:
for (int h = 3; h < 17; h++) {
for (int v = 3; v < 17; v++) {
....
Hope this helps.
You are attempting to access a part of the array that is greater than the size of the array (which is 20 by 20; i.e. its indexes go between 0 and 19), i.e, a part of the array that doesn't exist.
Your loops go between 3 and 23, so on this line:
hp = grid[h][v] * grid[h][v + 1] * grid[h][v + 2] * grid[h][v + 3];
When h=0 and v=20 you attempt to access grid[0][20] and the exception is raised. If the exception wasn't raised you'd go on to access grid[0][21] and grid[0][22] on the next time round the loop; even more wrong; even further outside the array.
When you go between 0 and 19 you get the reverse problem
d2p = grid[h][v] * grid[h - 1][v + 1] * grid[h - 2][v + 2] * grid[h - 3][v + 3];
when h=0 and v=0 grid[h - 1][v + 1] attempts to access grid[0 - 1][1], i.e. grid[-1][1], this also does not exist
Without knowing exactly what your goal is (and not wanting to spoil your learning) I can't advise on what to do instead but you must not access parts of the array that do not exist. But it seems like you are manipulating the "inner region" of the array, possibly you don't want to go the whole way from 0-19, maybe you only want 3-16 (for example, see if that makes sense for the problem you're actually solving)

Categories