Java ArrayIndexOutOfBoundsException: 20 multidimensional array - java

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)

Related

How can I generate a sorted list of LogN unique numbers, where N is the given size of the array?

So basically I want to generate random numbers and put them into a list with a given size of N.
for example, this generates a sorted list where the number of unique numbers is equal to roughly N/2
How can I modify this to make the unique numbers roughly equal to LogN?
for (int i = 0; i < N; i++) {
list.add(i, list.get(i) + new Java.util.Random.nextInt(2);
}
If you want a consistently accurate list of log(n) unique numbers, why not generate them first? You can fill an array later.
import java.util.Random;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class RandomExample {
public static void main(String[] args) {
System.out.println(fillLogNUnique(100, 0, 100));
}
public static List <Integer> fillLogNUnique(final int n, final int lowLimit, final int highLimit) {
final Random random = ThreadLocalRandom.current();
// Assuming log base 2.0.
final int requiredUniqueCount = (int) Math.floor(Math.log(n) / Math.log(2.0));
final List <Integer> uniques = random
.ints(lowLimit, highLimit)
.limit(requiredUniqueCount)
.boxed()
.collect(Collectors.toList());
return IntStream
.range(0, n)
.map(x -> uniques.get(random.nextInt(uniques.size())))
.boxed()
.collect(Collectors.toList());
}
}
The inputs are n: number of required random numbers, lowLimit: the lower bounds for the generated numbers, and highLimit: the upper bound.
The output for that is:
[17, 29, 98, 17, 17, 29, 64, 64, 64, 17, 98, 98, 91, 17, 64, 50, 17, 50, 50, 64, 98, 91, 29, 50, 91, 50, 91, 98, 91, 98, 98, 29, 91, 91, 98, 64, 29, 98, 91, 50, 64, 50, 64, 17, 17, 50, 29, 29, 50, 50, 91, 50, 17, 17, 98, 64, 17, 98, 64, 17, 50, 17, 50, 50, 91, 50, 64, 91, 64, 91, 64, 64, 64, 91, 64, 64, 50, 50, 91, 50, 17, 17, 64, 98, 91, 17, 17, 17, 64, 50, 91, 29, 29, 91, 29, 50, 29, 91, 17, 91]
...which has exactly 6, as log(100) with base 2 has an integer value (floored) of 6.
Note that this answer is for Java >= 8.

delay timer in gwt on quick sort

I'm writing a quick sort algorithm in GWT, between each iteration I'm trying to print the current status.
This is the method
private int partition(int[] list_of_numbers, int first, int last) {
int pivot = list_of_numbers[first];
int up = first;
int down = last;
do {
while ((up < last) && pivot >= list_of_numbers[up]) {
up++;
}
while (pivot < list_of_numbers[down]) {
down--;
}
if (up < down) {
swap(list_of_numbers, up, down);
sortedResult.setText( array_to_string(list_of_numbers));
logger.log(Level.SEVERE, sortedResult.getText());
}
} while (up < down);
swap(list_of_numbers, first, down);
sortedResult.setText( array_to_string(list_of_numbers));
logger.log(Level.SEVERE, sortedResult.getText());
return down;
}
It works really well and we can see from the logger that it puts out the following
30, 3, 23, 7, 77, 46, 62, 91, 89, 22, 48, 96, 32, 40, 95,
30, 3, 23, 7, 22, 46, 62, 91, 89, 77, 48, 96, 32, 40, 95,
22, 3, 23, 7, 30, 46, 62, 91, 89, 77, 48, 96, 32, 40, 95,
22, 3, 7, 23, 30, 46, 62, 91, 89, 77, 48, 96, 32, 40, 95,
7, 3, 22, 23, 30, 46, 62, 91, 89, 77, 48, 96, 32, 40, 95,
3, 7, 22, 23, 30, 46, 62, 91, 89, 77, 48, 96, 32, 40, 95,
3, 7, 22, 23, 30, 46, 40, 91, 89, 77, 48, 96, 32, 62, 95,
3, 7, 22, 23, 30, 46, 40, 32, 89, 77, 48, 96, 91, 62, 95,
3, 7, 22, 23, 30, 32, 40, 46, 89, 77, 48, 96, 91, 62, 95,
3, 7, 22, 23, 30, 32, 40, 46, 89, 77, 48, 96, 91, 62, 95,
3, 7, 22, 23, 30, 32, 40, 46, 89, 77, 48, 62, 91, 96, 95,
3, 7, 22, 23, 30, 32, 40, 46, 62, 77, 48, 89, 91, 96, 95,
3, 7, 22, 23, 30, 32, 40, 46, 62, 48, 77, 89, 91, 96, 95,
3, 7, 22, 23, 30, 32, 40, 46, 48, 62, 77, 89, 91, 96, 95,
3, 7, 22, 23, 30, 32, 40, 46, 48, 62, 77, 89, 91, 96, 95,
3, 7, 22, 23, 30, 32, 40, 46, 48, 62, 77, 89, 91, 95, 96,
but on the output to the screen it will only display the last iteration
I would like to put a delay of 1 second after each sort where the user can see the current array.
I'm not sure how to use the timer in this case. As well I don't know if it is changing the value each time so fast that I only see the last one or if it's only changing it at the end.
A quick google search yields a page guiding you through delaying calculations and updating the interface.
You can find it here.

How to calculate the coordinates of points in a circle using Java?

I found this website where you try drawing a perfect circle. Just for fun I coded this small Java application, that draws a circle from the middle of your screen:
// here is my massive array
private static int[] circle = {0, -100, 1, -100, 2, -100, 3, -100, 4, -100, 5, -100, 6, -100, 7, -100, 8, -100, 9, -100, 10, -100, 11, -99, 12, -99, 13, -99, 14, -99, 15, -99, 16, -99, 17, -98, 18, -98, 19, -98, 20, -98, 21, -98, 22, -97, 23, -97, 24, -97, 25, -97, 26, -96, 27, -96, 28, -96, 29, -96, 30, -95, 31, -95, 32, -95, 33, -94, 34, -94, 35, -94, 36, -93, 37, -93, 38, -92, 39, -92, 40, -92, 41, -91, 42, -91, 43, -90, 44, -90, 45, -89, 46, -89, 47, -88, 48, -88, 49, -87, 50, -86, 51, -86, 52, -85, 53, -85, 54, -84, 55, -83, 56, -84, 57, -83, 58, -82, 59, -82, 60, -81, 61, -80, 62, -79, 63, -79, 64, -78, 65, -77, 66, -76, 67, -75, 68, -74, 69, -73, 70, -72, 71, -71, 72, -70, 73, -69, 74, -68, 75, -67, 76, -66, 77, -65, 78, -64, 79, -63, 79, -62, 80, -61, 81, -60, 82, -59, 82, -58, 83, -57, 84, -56, 83, -55, 84, -54, 85, -53, 85, -52, 86, -51, 86, -50, 87, -49, 88, -48, 88, -47, 89, -46, 89, -45, 90, -44, 90, -43, 91, -42, 91, -41, 92, -40, 92, -39, 92, -38, 93, -37, 93, -36, 94, -35, 94, -34, 94, -33, 95, -32, 95, -31, 95, -30, 96, -29, 96, -28, 96, -27, 96, -26, 97, -25, 97, -24, 97, -23, 97, -22, 98, -21, 98, -20, 98, -19, 98, -18, 98, -17, 99, -16, 99, -15, 99, -14, 99, -13, 99, -12, 99, -11, 100, -10, 100, -9, 100, -8, 100, -7, 100, -6, 100, -5, 100, -4, 100, -3, 100, -2, 100, -1, 100, 0, 100, 1, 100, 2, 100, 3, 100, 4, 100, 5, 100, 6, 100, 7, 100, 8, 100, 9, 100, 10, 100, 11, 99, 12, 99, 13, 99, 14, 99, 15, 99, 16, 99, 17, 98, 18, 98, 19, 98, 20, 98, 21, 98, 22, 97, 23, 97, 24, 97, 25, 97, 26, 96, 27, 96, 28, 96, 29, 96, 30, 95, 31, 95, 32, 95, 33, 94, 34, 94, 35, 94, 36, 93, 37, 93, 38, 92, 39, 92, 40, 92, 41, 91, 42, 91, 43, 90, 44, 90, 45, 89, 46, 89, 47, 88, 48, 88, 49, 87, 50, 86, 51, 86, 52, 85, 53, 85, 54, 84, 55, 83, 56, 84, 57, 83, 58, 82, 59, 82, 60, 81, 61, 80, 62, 79, 63, 79, 64, 78, 65, 77, 66, 76, 67, 75, 68, 74, 69, 73, 70, 72, 71, 71, 72, 70, 73, 69, 74, 68, 75, 67, 76, 66, 77, 65, 78, 64, 79, 63, 79, 62, 80, 61, 81, 60, 82, 59, 82, 58, 83, 57, 84, 56, 83, 55, 84, 54, 85, 53, 85, 52, 86, 51, 86, 50, 87, 49, 88, 48, 88, 47, 89, 46, 89, 45, 90, 44, 90, 43, 91, 42, 91, 41, 92, 40, 92, 39, 92, 38, 93, 37, 93, 36, 94, 35, 94, 34, 94, 33, 95, 32, 95, 31, 95, 30, 96, 29, 96, 28, 96, 27, 96, 26, 97, 25, 97, 24, 97, 23, 97, 22, 98, 21, 98, 20, 98, 19, 98, 18, 98, 17, 99, 16, 99, 15, 99, 14, 99, 13, 99, 12, 99, 11, 100, 10, 100, 9, 100, 8, 100, 7, 100, 6, 100, 5, 100, 4, 100, 3, 100, 2, 100, 1, 100, 0,100,-1,100,-2,100,-3,100,-4,100,-5,100,-6,100,-7,100,-8,100,-9,100,-10,100,-11,99,-12,99,-13,99,-14,99,-15,99,-16,99,-17,98,-18,98,-19,98,-20,98,-21,98,-22,97,-23,97,-24,97,-25,97,-26,96,-27,96,-28,96,-29,96,-30,95,-31,95,-32,95,-33,94,-34,94,-35,94,-36,93,-37,93,-38,92,-39,92,-40,92,-41,91,-42,91,-43,90,-44,90,-45,89,-46,89,-47,88,-48,88,-49,87,-50,86,-51,86,-52,85,-53,85,-54,84,-55,83,-56,84,-57,83,-58,82,-59,82,-60,81,-61,80,-62,79,-63,79,-64,78,-65,77,-66,76,-67,75,-68,74,-69,73,-70,72,-71,71,-72,70,-73,69,-74,68,-75,67,-76,66,-77,65,-78,64,-79,63,-79,62,-80,61,-81,60,-82,59,-82,58,-83,57,-84,56,-83,55,-84,54,-85,53,-85,52,-86,51,-86,50,-87,49,-88,48,-88,47,-89,46,-89,45,-90,44,-90,43,-91,42,-91,41,-92,40,-92,39,-92,38,-93,37,-93,36,-94,35,-94,34,-94,33,-95,32,-95,31,-95,30,-96,29,-96,28,-96,27,-96,26,-97,25,-97,24,-97,23,-97,22,-98,21,-98,20,-98,19,-98,18,-98,17,-99,16,-99,15,-99,14,-99,13,-99,12,-99,11,-100,10,-100,9,-100,8,-100,7,-100,6,-100,5,-100,4,-100,3,-100,2,-100,1,-100,0,-100,-1,-100,-2,-100,-3,-100,-4,-100,-5,-100,-6,-100,-7,-100,-8,-100,-9,-100,-10,-100,-11,-99,-12,-99,-13,-99,-14,-99,-15,-99,-16,-99,-17,-98,-18,-98,-19,-98,-20,-98,-21,-98,-22,-97,-23,-97,-24,-97,-25,-97,-26,-96,-27,-96,-28,-96,-29,-96,-30,-95,-31,-95,-32,-95,-33,-94,-34,-94,-35,-94,-36,-93,-37,-93,-38,-92,-39,-92,-40,-92,-41,-91,-42,-91,-43,-90,-44,-90,-45,-89,-46,-89,-47,-88,-48,-88,-49,-87,-50,-86,-51,-86,-52,-85,-53,-85,-54,-84,-55,-83,-56,-84,-57,-83,-58,-82,-59,-82,-60,-81,-61,-80,-62,-79,-63,-79,-64,-78,-65,-77,-66,-76,-67,-75,-68,-74,-69,-73,-70,-72,-71,-71,-72,-70,-73,-69,-74,-68,-75,-67,-76,-66,-77,-65,-78,-64,-79,-63,-79,-62,-80,-61,-81,-60,-82,-59,-82,-58,-83,-57,-84,-56,-83,-55,-84,-54,-85,-53,-85,-52,-86,-51,-86,-50,-87,-49,-88,-48,-88,-47,-89,-46,-89,-45,-90,-44,-90,-43,-91,-42,-91,-41,-92,-40,-92,-39,-92,-38,-93,-37,-93,-36,-94,-35,-94,-34,-94,-33,-95,-32,-95,-31,-95,-30,-96,-29,-96,-28,-96,-27,-96,-26,-97,-25,-97,-24,-97,-23,-97,-22,-98,-21,-98,-20,-98,-19,-98,-18,-98,-17,-99,-16,-99,-15,-99,-14,-99,-13,-99,-12,-99,-11,-100,-10,-100,-9,-100,-8,-100,-7,-100,-6,-100,-5,-100,-4,-100,-3,-100,-2,-100,-1,-100,0};
public static void main(String[] args) {
try {
// Waiting for the website to load
Thread.sleep(3000);
} catch (InterruptedException e) {}
// Getting the center of the screen
Point p = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
// Drawing the circle by clicking on each point in the array relative to the middle of the screen
for (int x = 0; x < circle.length; x+=2) {
// Calculating the x and y coordinates
int xx = p.x + circle[x];
int yy = p.y + circle[x+1];
click(xx, yy);
// waiting 10ms so the website can calculate the action
try {
Thread.sleep(10);
} catch (InterruptedException e) {}
}
}
public static void click(int x, int y) {
try {
Robot bot = new Robot();
bot.mouseMove(x, y);
bot.mousePress(InputEvent.BUTTON1_MASK);
bot.mouseRelease(InputEvent.BUTTON1_MASK);
} catch (Exception e) {}
}
The application is made to be used on this website. But my problem is as you can see, that I have to save each point of the circle in an array and that array is very big. If I wanted to expand the radius (100px) I would need to rewrite all that points. Is there a easier way of doing so? Maybe by using an equation or something like that.
You can calculate the coordinates of a point on the circumference of a circle for a given angle (in radians) by doing {cos(angle), sin(angle)}.
So one way we can get a list of points is to loop around and get the coordinates for every n degrees, where n is configurable (you'll need more points for a larger radius to draw a smooth circle).
I'd first declare a Point class to hold these values:
class Point
{
final int x;
final int y;
Point(final double x, final double y)
{
this.x = (int) x; // we're dealing with pixels, so just truncate it
this.y = (int) y;
}
#Override
public String toString()
{
return "{" + x + ", " + y + "}";
}
}
Then we can do the following to populate an array of Points:
final int NUM_POINTS = 1000;
final double RADIUS = 100d;
final Point[] points = new Point[NUM_POINTS];
for (int i = 0; i < NUM_POINTS; ++i)
{
final double angle = Math.toRadians(((double) i / NUM_POINTS) * 360d);
points[i] = new Point(
Math.cos(angle) * RADIUS,
Math.sin(angle) * RADIUS
);
}
System.out.println(Arrays.toString(points));
You can use Circle Equations to draw a nearly perfect circle. You can not draw a perfect circle because of your screen resolution.
You only need x and y coordinates.
public Point [] generateCoordinates(Point center, int diameter) {
//You will use algorithm here
}
Here is an example algorithm;
https://www.tutorialspoint.com/computer_graphics/circle_generation_algorithm.htm
https://www.varsitytutors.com/hotmath/hotmath_help/topics/equation-of-a-circle
you should use this equation, so you can adjust circle size and precision
I used this code to generate my array but its very slow, thats why the answer by #Michael is better...
int r = Integer.parseInt(rad) / 2;
String[] circle = {"0", "0"};
String[] circleq = {"0", "0"};
String[] circlew = {"0", "0"};
String[] circlee = {"0", "0"};
for (int x = 0; x < r; x++) {
double yy;
yy = Math.sqrt((r*r) - (x*x));
int y = (int) Math.round(yy);;
circle = add(circle, Integer.parseInt("-" + x));
circle = add(circle, Integer.parseInt("-" + y));
}
String[] circle2 = circle;
for (int c = 0; c < circle2.length; c++) {
if ((c & 1) == 0 || c == 0) {
circleq = add(circleq, Integer.parseInt(circle2[c]));
} else {
circleq = add(circleq, Integer.parseInt(circle2[c].replace("-", "")));
}
}
for (int c = 0; c < circle2.length; c++) {
circlew = add(circlew, Integer.parseInt(circle2[c].replace("-", "")));
}
for (int c = 0; c < circle2.length; c++) {
if ((c & 1) == 0 || c == 0) {
circlee = add(circlee, Integer.parseInt(circle2[c].replace("-", "")));
} else {
circlee = add(circlee, Integer.parseInt(circle2[c]));
}
}
It saves each quarter of the circle in a seperate array.

Self calculated RGB values aren't matching with actual RGB values

I am working on a program where I take a portion of an image and calculate the average RGB of that image. When I calculate it I get totally different values as if I was to use the built in function. When I test my numbers and put them in an RGB color chart they are off while the built in function is very accurate. The problem with using the built in function is that it has literally no functionality. It just prints out what I calculate but I can't use that data. link to function called dump()
public void readSquares(Mat img){
int width = 20;
int height = 20;
int rSum = 0;
int gSum = 0;
int bSum = 0;
int rAvg = 0;
int gAvg = 0;
int bAvg = 0;
Imgproc.cvtColor(img, img, Imgproc.COLOR_BGR2RGB);
int channels = img.channels();
int totalBytes = (int)(img.total() * img.channels());
byte buff[] = new byte[totalBytes];
img.get(0, 0, buff);
for (int i=0; i< height; i++) {
//stride is the number of bytes in a row of smallImg
int stride = channels * width;
for (int j=0; j<stride; j+=channels) {
int r = buff[(i * stride) + j];
int g = buff[(i * stride) + j + 1];
int b = buff[(i * stride) + j + 2];
if(r < 0 || g < 0 || b < 0){
r = Math.abs(r);
g = Math.abs(g);
b = Math.abs(b);
}
rSum += r;
gSum += g;
bSum += b;
}
}
rAvg = (int) (rSum / img.total()); //total pixels in picture
gAvg = (int) (gSum / img.total());
bAvg = (int) (bSum / img.total());
System.out.println("R: " + rAvg);
System.out.println("G: " + gAvg);
System.out.println("B: " + bAvg);
This below prints out the accurate values but I can't get the separate channels or find the average RGB it just spits everything out as a string which I can't utilize.
System.out.println(img.dump());
}
Here is an example; I read Red image for pixels this was the output:
My Calculated
R:18
G:27
B:64
The function
The first number represents the R second the G and third the B. It does that for every pixel in the image.
[237, 24, 60, 236, 23, 59, 236, 26, 56, 234, 25, 55, 238, 27, 62, 238, 27, 62, 238, 27, 62, 236, 25, 60, 234, 24, 59, 233, 23, 57, 234, 24, 59, 234, 24, 59, 235, 22, 59, 235, 22, 59, 237, 21, 59, 235, 20, 58, 237, 19, 56, 237, 19, 56, 239, 19, 52, 237, 18, 51;
236, 24, 55, 237, 25, 56, 237, 25, 54, 237, 25, 54, 236, 26, 56, 237, 27, 57, 238, 28, 58, 239, 29, 59, 238, 27, 62, 234, 24, 59, 237, 24, 62, 237, 24, 62, 238, 22, 63, 237, 20, 61, 235, 20, 58, 235, 20, 58, 237, 19, 58, 239, 21, 61, 237, 19, 56, 236, 18, 55;
238, 26, 57, 238, 26, 57, 234, 25, 53, 232, 23, 50, 236, 26, 54, 237, 27, 55, 237, 27, 55, 237, 27, 55, 237, 26, 61, 236, 25, 60, 237, 24, 62, 237, 24, 62, 238, 22, 63, 237, 20, 61, 238, 22, 63, 238, 22, 63, 238, 20, 61, 238, 20, 61, 237, 19, 56, 236, 18, 55;
242, 30, 60, 240, 27, 58, 235, 24, 49, 233, 21, 47, 234, 22, 48, 237, 26, 52, 236, 25, 50, 237, 26, 52, 238, 26, 57, 237, 25, 56, 239, 23, 62, 239, 23, 62, 238, 22, 63, 238, 22, 63, 238, 19, 63, 239, 20, 65, 238, 20, 61, 239, 21, 63, 238, 20, 59, 236, 18, 57;
240, 27, 62, 236, 23, 59, 236, 24, 53, 236, 24, 53, 235, 23, 51, 234, 22, 50, 235, 23, 51, 236, 24, 53, 237, 24, 60, 236, 23, 59, 237, 24, 64, 237, 24, 64, 237, 24, 64, 236, 22, 63, 238, 22, 63, 238, 22, 63, 237, 20, 61, 237, 20, 61, 237, 21, 59, 235, 20, 58;
238, 25, 61, 237, 24, 60, 237, 24, 60, 237, 24, 60, 235, 22, 57, 236, 23, 59, 235, 22, 57, 235, 22, 57, 234, 21, 56, 236, 23, 59, 237, 24, 62, 236, 23, 61, 237, 20, 61, 237, 20, 61, 238, 21, 65, 238, 21, 65, 238, 22, 63, 237, 20, 61, 235, 22, 59, 235, 22, 59;
238, 27, 62, 237, 26, 61, 237, 24, 60, 236, 23, 59, 236, 24, 55, 236, 24, 55, 236, 24, 55, 234, 22, 52, 237, 22, 53, 237, 22, 53, 237, 21, 57, 237, 21, 57, 237, 21, 59, 237, 21, 59, 237, 20, 61, 237, 20, 61, 236, 22, 63, 236, 22, 63, 236, 22, 63, 235, 21, 61;
237, 26, 63, 236, 25, 62, 237, 24, 62, 237, 24, 62, 238, 25, 63, 237, 24, 62, 237, 21, 57, 238, 22, 59, 237, 21, 57, 237, 21, 57, 235, 20, 56, 235, 20, 56, 235, 20, 56, 237, 21, 57, 237, 21, 57, 238, 22, 59, 235, 23, 53, 236, 24, 55, 236, 24, 53, 236, 24, 53;
236, 29, 69, 235, 28, 68, 238, 26, 70, 239, 27, 71, 240, 25, 68, 238, 24, 67, 237, 24, 64, 236, 22, 63, 237, 20, 61, 235, 19, 60, 237, 19, 58, 236, 18, 57, 237, 21, 59, 237, 21, 59, 238, 22, 59, 240, 25, 61, 237, 25, 56, 236, 24, 55, 236, 24, 53, 237, 25, 54;
235, 28, 66, 235, 28, 66, 237, 25, 65, 240, 29, 68, 241, 27, 67, 238, 25, 65, 239, 23, 64, 238, 22, 63, 237, 19, 58, 237, 19, 58, 237, 17, 57, 237, 17, 57, 240, 20, 57, 241, 21, 59, 238, 21, 53, 239, 22, 55, 240, 26, 55, 238, 23, 53, 237, 22, 51, 237, 22, 51;
237, 25, 69, 237, 25, 69, 239, 27, 73, 239, 27, 73, 240, 25, 70, 241, 26, 71, 239, 22, 66, 237, 20, 63, 238, 20, 61, 237, 18, 60, 236, 17, 59, 237, 18, 60, 238, 20, 59, 239, 21, 61, 238, 22, 59, 238, 22, 59, 239, 25, 54, 237, 22, 51, 237, 23, 49, 235, 22, 48;
237, 25, 65, 238, 27, 66, 238, 27, 66, 238, 27, 66, 238, 27, 66, 237, 25, 65, 236, 22, 63, 235, 21, 61, 235, 22, 59, 235, 22, 59, 234, 18, 57, 237, 21, 59, 238, 22, 61, 238, 22, 61, 238, 22, 59, 238, 22, 59, 238, 20, 57, 238, 20, 57, 237, 20, 52, 237, 20, 52;
236, 25, 60, 236, 25, 60, 238, 27, 62, 239, 29, 63, 237, 26, 61, 236, 25, 60, 237, 26, 61, 236, 25, 60, 236, 23, 59, 235, 22, 57, 236, 23, 59, 235, 22, 57, 237, 24, 60, 236, 23, 59, 238, 22, 59, 237, 21, 57, 237, 21, 57, 235, 20, 56, 236, 18, 55, 235, 17, 54;
237, 25, 56, 238, 26, 57, 237, 27, 57, 237, 27, 57, 236, 26, 56, 237, 27, 57, 238, 28, 58, 237, 27, 57, 237, 27, 57, 238, 28, 58, 240, 27, 58, 237, 25, 56, 236, 23, 59, 236, 23, 59, 237, 21, 57, 237, 21, 57, 235, 20, 58, 235, 20, 58, 236, 18, 55, 236, 18, 55;
237, 25, 54, 237, 25, 54, 237, 25, 54, 235, 23, 51, 237, 25, 56, 240, 27, 58, 237, 26, 61, 236, 25, 60, 237, 26, 63, 237, 26, 63, 238, 27, 66, 236, 24, 64, 237, 24, 64, 236, 22, 63, 237, 20, 61, 235, 19, 60, 236, 17, 59, 236, 17, 59, 236, 18, 57, 235, 16, 56;
237, 25, 56, 237, 25, 56, 236, 23, 59, 238, 25, 61, 237, 24, 62, 238, 25, 63, 237, 24, 64, 238, 25, 65, 240, 25, 68, 238, 24, 67, 237, 23, 66, 237, 23, 66, 238, 21, 65, 238, 21, 65, 237, 20, 63, 237, 20, 63, 235, 19, 62, 235, 19, 62, 235, 19, 60, 235, 19, 60;
237, 25, 56, 237, 25, 56, 237, 24, 60, 237, 24, 60, 237, 24, 64, 234, 20, 60, 237, 23, 66, 238, 24, 67, 238, 24, 69, 237, 23, 68, 236, 23, 70, 234, 22, 69, 236, 22, 67, 233, 18, 63, 236, 22, 65, 235, 21, 63, 236, 22, 63, 234, 20, 60, 235, 22, 59, 235, 22, 59;
238, 27, 55, 238, 27, 55, 239, 24, 56, 241, 27, 58, 237, 24, 62, 237, 24, 62, 237, 23, 66, 238, 24, 67, 237, 24, 71, 237, 24, 71, 236, 23, 72, 234, 22, 71, 231, 19, 65, 236, 23, 70, 236, 24, 68, 236, 24, 68, 237, 24, 62, 236, 23, 61, 236, 24, 55, 238, 26, 57;
239, 29, 63, 236, 25, 60, 236, 25, 62, 236, 25, 62, 236, 24, 64, 237, 25, 65, 234, 27, 67, 232, 26, 66, 238, 31, 74, 236, 29, 71, 234, 26, 71, 231, 24, 69, 232, 25, 70, 234, 26, 71, 236, 29, 71, 238, 31, 74, 235, 28, 68, 232, 26, 66, 237, 26, 63, 236, 25, 62;
237, 24, 60, 237, 24, 60, 237, 26, 61, 236, 25, 60, 233, 22, 59, 234, 24, 61, 232, 26, 62, 234, 28, 63, 234, 27, 65, 234, 27, 65, 234, 26, 69, 234, 26, 69, 234, 26, 73, 234, 26, 73, 237, 29, 79, 238, 30, 80, 236, 29, 71, 234, 26, 69, 237, 26, 63, 238, 27, 64]
Strictly speaking, the Java Language Specification states that a byte data type has the range −128 - 127 and that's how Java will interpret a byte. If for instance a byte is promoted or cast to an int Java will interpret the first bit as a sign and use sign extension. There's no language support to help you interpret a byte differently. (There's, for instance, no unsigned keyword in Java.)
That being said, nothing prevents you from viewing a byte as an 8-bit value and interpret those bits as unsigned. Just keep in mind that there's nothing you can do to force your interpretation upon someone else's method. If a method accepts a byte, then that method accepts a value between −128 and 127 unless explicitly stated otherwise.
public static int unsignedToBytes(byte b) {
return b & 0xFF;
}
This method does what you asked. Hope this helps !!!
Edit: There are plenty of good answers on Stack Overflow and elsewhere. I would recommend you to try finding the answer before posting the question. Thanks.

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;
}
}

Categories