This question already has answers here:
Enhanced 'for' loop causes an ArrayIndexOutOfBoundsException
(2 answers)
Closed 4 years ago.
I am getting an array out of marks from this section, this section calculates the total mark by multiplying the weight of each assessment by the marks of each assessment. Any marks that gets a -1 is automatically set as 0 and not counted.
public double totalMark(){
int [] assessments = {2,2,2,2,1,1,1,1,2,2,1,3,70}; //Weight of each assessment
int totalMark = 0;
int overallmark = 0;
for (int i : marks) //marks of each assignment
if (marks[i] == -1) {
assessments[i] = 0;
}
for (int i : marks) {
totalMark =+ assessments[i] * marks[i];
}
for (int i : assessments) {
overallmark =+ assessments[i];
}
return totalMark/overallmark;
}
This is my main method:
public static void main(String args[]) {
int[] samsMarks = {50, 60, 65, 60, 65, 70, 55, 66, 60, 73, 65, 45, 68, 54};
int[] billysMarks = {50, 60, -1, 60, 65, 70, 55, 66, 60, 73, 65, 45, 68, 54};
Student sam = new Student("1111111", samsMarks);
Student billy = new Student("1111112", billysMarks);
System.out.println(billy.totalMark);
}
Replace all your
for (int i : marks)
with
for (int i = 0; i < marks.length; i++)
and
for (int i : assessments)
with
for (int i = 0; i < assessments.length; i++)
When you use
for (int i : marks)
i is not the index of marks but the elements that marks contains:
50, 60, 65, 60, 65, 70, 55, 66, 60, 73, 65, 45, 68, 54
As mentioned in the first answer you are using the for in this way:
for(element : list)
but since you need the index you should use it in the following form
for(initialization; termination; increment)
also a suggestion for the code instead of doing 3 loops through the element i suggest you do it in one iteration and you code can be as the following(since the number of element of both marks and assignment are the same)
for (int i = 0; i < marks.length; i++) {
if (marks[i] == -1) {
assessments[i] = 0;
}
totalMark =+ assessments[i] * marks[i];
overallMark =+ assessments[i];
}
You are iterating assessments array with a marks iterator and they might not be the same length. Check that.
These 2 loops are very problematic (both are accessing assessments and worry only about marks length:
for (int i : marks) //First problematic loop
if (marks[i] == -1) {
assessments[i] = 0;
}
for (int i : marks) { //Second problematic loop
totalMark =+ assessments[i] * marks[i];
Related
import java.util.Random;
public class Practice_assignment {
public static void main(String[] args){
int[] winning_numbers = {0,0,0,0,0,0,0,0,0,0} ;
//The i++ for the first loop is in the second loop. I was trying to ensure it only goes to
the next value of the loop once a unique value has been gotten.
for (int i=0; i<10;){
int max = 99;
int min = 1;
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
for (int j=0;j<=i;j++){
if (j<i && winning_numbers[j]==randomNum){
break;
}
else if (j==i && i<10){
winning_numbers[i] = randomNum;
System.out.println(winning_numbers[i]+" ");
i++;
}
}
}
}
}
If I understood correctly what you are trying to achieve, I think you could use something like this:
public class RandomNumbers {
public static void main(String[] args) {
int[] winning_numbers = {0,0,0,0,0,0,0,0,0,0} ;
Random random = new Random();
for(int i = 0; i < winning_numbers.length; i++) {
OptionalInt generatedInt = random.ints(1, 0, 100).findFirst(); // generates a stream of 1 number between 0 and 99 and gets the first (and only) one generated
while (contains(winning_numbers, generatedInt)) {
generatedInt = random.ints(1, 0, 100).findFirst(); // while the generated number is already in the array, generate a new one
}
winning_numbers[i] = generatedInt.getAsInt();
}
System.out.println(Arrays.toString(winning_numbers));
}
// this method will check if the given array already contains the generated int
private static boolean contains(int[] arr, OptionalInt generatedInt) {
return Arrays.stream(arr).anyMatch(number -> generatedInt.getAsInt() == number);
}
}
I ran it a couple of times and here are some outputs I generated with this code:
[52, 54, 21, 62, 47, 13, 94, 36, 82, 25]
[35, 37, 16, 81, 22, 71, 17, 94, 56, 8]
[51, 50, 80, 62, 18, 88, 1, 53, 44, 79]
[16, 95, 18, 66, 31, 4, 1, 55, 52, 26]
[4, 11, 65, 68, 22, 76, 95, 67, 35, 92]
[49, 87, 34, 88, 71, 57, 12, 76, 70, 78]
It appears you want to generate an array of winning_numbers. Here is one ways to do it.
create a helper method to look for duplicate numbers and return true if a duplicate is found.
then iterate over the array checking for the current random number and adding it if unique.
note that min, max, and rand should be initialized outside the loop.
Random rand = new Random();
int[] winning_nummbers = new int[10];
int max = 99;
int min = 1;
for (int i = 0; i < 10;) {
int randomNum = rand.nextInt((max - min) + 1) + min;
// if does not contain a duplicate, then assign
if (!contains(winning_numbers, randomNum)) {
winning_numbers[i++] = randomNum;
}
}
System.out.println(Arrays.toString(winning_numbers));
Prints something like this.
[46, 91, 5, 2, 42, 58, 74, 24, 53, 36]
The helper method.
public static boolean contains(int[] array, int v) {
for (int i = 0; i < array.length; i++) {
if (array[i] == v) {
return true;
}
}
return false;
}
If you want to use streams you can do it like so using a Random instance.
stream values form min to max inclusive.
drop duplicates with distinct
limit(10) limits to 10 values.
put them in an array
int[] winning_numbers = rand.ints(min,max+1)
.distinct()
.limit(10)
.toArray();
Ok, so I just started leaning java in college, and this code is bugging me. Basically, my professor asked us to write a code that would have a two-dementional array that would store the distance between six cities and it would look like this:
array
Then, we would have to collect info about the user's route in a simple array, ex. 1 5 4 3 2 1.
and, based on the numbers informed by the user, we would have to calculate de total distance he drove.
This is what I wrote:
package routes;
import java.util.*;
public class Routes {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[][] km = {{0, 20, 32, 45, 85, 90}, {20, 0, 20, 40, 65, 70}, {32, 20, 0, 25, 48, 49},
{45, 40, 25, 0, 39, 52}, {85, 65, 48, 39, 0, 36}, {90, 70, 49, 52, 36, 0}};
int[] routes = new int[6];
String[] route_r = new String[6];
String[] city = {"Belo Horizonte", "Contagem", "Betim", "Juatuba", "Pará de Minas", "Itaúna"};
int km_distance = 0;
for(int i = 0; i < 6; i++){
for(int j = 0; j < 6; j++){
System.out.printf("%d\t", km[i][j]);
}
System.out.printf("\n");
}
System.out.println("Enter your route: \n\n1. Belo Horizonte\t2. Contagem\t3. Betim\n"
+ "4. Juatuba\t5. Pará de Minas\t 6.Itaúna\n");
for(int i = 0; i < 6; i++){
rota[i] = input.nextInt();
}
System.out.println("\nRoute: \n");
for(int i = 0; i < 6; i++){
System.out.printf("%d. %s\n", i+1, city[route[i]-1]);
}
for(int i = 0; i < 6; i++){
km_distance = km_distance + km[route[i]][route[i+1]];
}
System.out.printf("\nTotal KM: %d\n", km_distance);
}
}
But for some unknown reason, it doesn't work. I debugged it and I realized the last for structure is only looping four times and then it crashes. Please send help.
edit: the output error
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
at rotas.Rotas.main(Rotas.java:41)
C:\...\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
RUN FAILED (exit value 1, total time: 47ms)
It seems that the correct variable name is rota not route. Anyway at this line:
km_distance = km_distance + city[route[i]][route[i+1]];
you are accessing index i+1 which will be 7 at the end of the loop. It must be giving you an index out of range exception.
I have 3 arrays iX, iY, and iZ with each holding 20 integers.
iX goes up by 5, iY goes up by 3, and iZ is the sum of both.
for (int i=5; i <=iX.length; i+=5)
{
iX[i] = i;
System.out.print (i + "\n");
}
for (int j=3; j <iY.length; j+=3)
{
iY[j] = j;
}
for (int k=0; k < iZ.length; k++)
{
iZ[k] = iX[k]+iY[k];
}
When I run it I get:
"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20
at Quiz10RTN.main(Quiz10RTN.java:61)"
Line 61 is : iX[i] = i;
I can't seem to get it to even print out 20 numbers, because it seems to be treating my limit of 20 integers as a range to stop at. Any help would be great, Thanks.
Issue is here
for (int i=5; i <=iX.length; i+=5)
^
There is no index match with iX.length in your array.
index of array start with 0, So if size of array is n, then you only have indexes from 0 to n-1.
You can use following to avoid the exception. But you need to think some other way to archive your goal.
for (int i=5; i <iX.length; i+=5)
Edit: for your comment I was trying to print out "5, 10, 15, 20, 25...etc"
You can try something like following
for (int i=0; i <iX.length; i++) {
iX[i]=(i+1)*5; // now your array become 5,10,15,...
}
You are confusing your array indices with the values you are storing in the arrays.
So, for example, if you want your iX array to contain the 20 integers 5, 10, 15,...100, your first loop should look like:
for (int i=0; i < iX.length; ++i)
{
iX[i] = (i + 1) * 5;
System.out.print (iX[i] + "\n");
}
You aren't using the array indexes properly (they must be incremental). You might also use Arrays.toString(int[]) to print your arrays. I believe you wanted something like
int[] iX = new int[20];
int[] iY = new int[20];
int[] iZ = new int[20];
int valfive = 5; // <-- our five increments.
int valthree = 3; // <-- the three increments.
for (int i = 0; i < iX.length; i++) {
iX[i] = valfive;
iY[i] = valthree;
iZ[i] = valfive + valthree;
valfive += 5; // <-- add 5
valthree += 3; // <-- add 3
}
System.out.println("Multiples of five: " + Arrays.toString(iX));
System.out.println("Multiples of three: " + Arrays.toString(iY));
System.out.println("Sums of fives and threes: " + Arrays.toString(iZ));
Output is (formatted for SO)
Multiples of five: [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70,
75, 80, 85, 90, 95, 100]
Multiples of three: [3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42,
45, 48, 51, 54, 57, 60]
Sums of fives and threes: [8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96,
104, 112, 120, 128, 136, 144, 152, 160]
I'm new to Java, and was given a question to solve as a weekend project. I ran into some troubles, and would like your help. Please understand I am a beginner. If I'm wrong somewhere, please explain it to me. I hope to be a good programmer too some day.
I did a through search, and found answers with things like "heatmaps" or "arraylists", something I probably won't be allowed to use since I haven't learned it yet.
Ok, so the question given to me was:
Find: 1) Mode, the most frequently occurring marks in the class. If 2 or more marks occur equally frequently, then the highest of these marks is the mode.
2) Mode Frequency: Frequency of mode.
It is assumed that the class has 10 students and marks are between 0 to 100.You are not allowed to sort the marks.
This is my code for finding the mode:
void mode()
{
int c[]=new int[10];
int arr[]=new int[10];
for (int i=0;i<10;i++)
{
for (int j=i+1;j<10;j++)
{
for (int k=0;k<10;k++)
{
if (marks[i]!=arr[k])
{
if (marks[i]==marks[j])
{
c[i]++;
break;
}
}
}
arr[i]=marks[i];
}
}
for (int k=0;k<10;k++)
{
System.out.println();
System.out.println(c[k]);
}
}
Where marks[] is the int array where I take the input, c[] is to count the number of times the number is occurring, and arr[] is an array to cross check if the number has previously occurred or not.
Let's assume the 10 numbers inputted are 99, 95, 97, 92, 80, 95, 73, 80, 95, 80. As you can see 95 and 80 occur three times.
So my c[] should have {0, 2, 0, 0, 2, 0, 0, 0, 0, 0}, but when I run it, it comes as {0, 2, 0, 0, 2, 1, 0, 1, 0, 0}, which means my program isn't cross checking with arr[].
I think I've made a mess using three for-loops. I can't seem to figure out how to solve it.
One solution would be to have an array of length 101 initialized to zero. This array would represent the number of times a specific mark had occurred. each time you encounter a specific mark you increment the count. Then to find the mode, you simple find the index with the highest count.
public class Loader
{
// We suppose that the parameter is not null
public static void mode_frequency(long collection[])
{
long frequencies[] = new long[collection.length];
for (int i = 0, l = collection.length; i < l; i++)
{
for (int j = i; j < l; j++)
{
if (collection[i] == collection[j])
{
++frequencies[i];
}
}
}
// With your example {99, 95, 97, 92, 80, 95, 73, 80, 95, 80}
// You should have somthing like {1, 3, 1, 1, 3, 2, 1, 2, 1, 1}
//
// As you can see, you just have to find the MAX frequency and then, print corresponding values from initial array
long frequency = 0;
for (int i = 0, l = frequencies.length; i < l; i++)
{
if (frequency < frequencies[i])
{
frequency = frequencies[i];
}
}
// Print each mode
for (int i = 0, l = collection.length; i < l; i++)
{
if (frequencies[i] == frequency)
{
System.out.println(collection[i]);
}
}
}
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
mode_frequency(new long[]
{
99,
95,
97,
92,
80,
95,
73,
80,
95,
80
});
}
}
You may want to use an algorithm in which you use secondary arrays to accumulate the counts for individual marks, and then search through those secondary arrays for the mark with the highest frequency with largest value. Consider:
package com.example.mode;
public class Mode {
public static void main(String[] args) {
int[] marks = { 99, 95, 97, 92, 80, 95, 73, 80, 95, 80};
//One algorithm .. insert marks and add occurances, keeping in order
int[] unique = new int[marks.length];
int[] count = new int[marks.length];
int maxUnique = 0;
for (int i=0; i < marks.length ; i++) {
int loc = -1;
for (int j=0; j < maxUnique; j++) {
if (marks[i] == unique[j]) {
loc = j;
break;
}
}
if (loc == -1) {
loc = maxUnique;
unique[loc] = marks[i];
count[loc] = 0;
maxUnique++;
}
count[loc] = count[loc]+1;
}
int maxValue = unique[0];
int maxCount = count[0];
for (int j=1; j < maxUnique; j++) {
if (count[j] > maxCount || (count[j] == maxCount && maxValue < unique[j]) ) {
maxValue = unique[j];
maxCount = count[j];
}
}
System.out.println("Mode = " + maxValue + ", frequency = " + maxCount);
}
}
I have created a method which takes generate random numbers with the condition that the next random number doesn't match with the previous one inside the array here's the code
// some code
int k=0;
//some code....
randomgenerator(k); // method call
public void randomgenerator(int j)
{
for(j=0; j<=99; j++){
if(j >= 1){
if (randomset.get(j) == randomset.get(j-1)){
randomset.add(0 + ( j , int)(Math.random() * ((99 - 0) + 1)));
}
else{
randomset.add(0 + (int)(Math.random() * ((99 - 0) + 1)));
}
}
}
}
The error I get is java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
Because initially randomset is empty therefore its size is 0 and returns exception at index 1. The best way to add randomset.add(0 + (int)(Math.random() * ((99 - 0) + 1))); if j < 1 (not >=1).
Correct code:
public void randomgenerator(int j)
{
for(j=0; j<=99; j++){
if(j >= 1){
if (randomset.get(j) == randomset.get(j-1)){
randomset.add(0 + ( j , int)(Math.random() * ((99 - 0) + 1)));
}
else{
randomset.add(0 + (int)(Math.random() * ((99 - 0) + 1)));
}
}
else {
randomset.add(0 + (int)(Math.random() * ((99 - 0) + 1)));
}
}
}
Use
for(j=0; j<randomset.size(); j++){
Don't use the same variable as your input parameter and your loop variable.
public void randomgenerator(int length)
{
for (int j = 0; j < length; j ++) ...
I'm not sure I follow the rest of the code, but that's a start.
You can't reference an element of ArrayList whose index is not in bounds [0, size() - 1]. Creating ArrayList via ArrayList() creates a list of size 0. To add elements to this array you must call one of the methods that adds an element, e.g. add(). Your first call is to get(), but the list has size 0, so even get(0) will cause an IndexOutOfBoundsException.
What to do depends on the expected contents of the list. In your case, I would recommend writing a helper function that generates a random number in range excluding specified number. You could use that function in a simple loop to generate the whole list, passing previous element to mentioned helper function.
Example:
public static int randomInRange(int a, int b) {
return (int)(Math.random() * (b - a + 1));
}
public static int randomInRangeExcluding(int a, int b, int excluding) {
int result = (int)(Math.random() * (b - a));
if (result == excluding) {
result++;
}
return result;
}
public static List<Integer> generateRandomList(int size) {
ArrayList<Integer> result = new ArrayList<Integer>();
for (int j = 0; j <= size; j++) {
if (j > 0) {
result.add(randomInRangeExcluding(0, size - 1, result.get(j - 1)));
} else {
result.add(randomInRange(0, size - 1));
}
}
return result;
}
and get the value using:
generateRandomList(100);
Calling this results in a list of random integers having no two consecutive elements equal:
[27, 34, 53, 92, 56, 93, 21, 22, 45, 95, 48, 25, 18, 26, 54, 1, 82, 26, 5, 62, 84, 23, 8, 84, 25, 0, 36, 37, 54, 95, 4, 26, 65, 53, 81, 16, 47, 56, 73, 46, 60, 50, 37, 89, 61, 84, 23, 79, 47, 87, 68, 49, 15, 17, 55, 71, 17, 55, 71, 51, 67, 33, 80, 47, 81, 24, 10, 41, 76, 60, 12, 17, 96, 43, 57, 55, 41, 56, 21, 85, 98, 40, 9, 39, 53, 28, 93, 70, 89, 80, 40, 41, 30, 81, 33, 53, 73, 28, 38, 87, 29]
What is with you're function? You receive a parameter named j and then you reassigned it?
randomset.get(j) == randomset.get(j-1) <- at this line you have a java.lang.IndexOutOfBoundsException because you call for the value from position 1 but in you're list you have only a value on position 0; so an error is thrown
and, what is this ? ((99 - 0) + 1)) ,you could white 100, it is easy, and more readable
by the way, at this line you have an error, randomset.add(0 + ( j , (int)(Math.random() * ((99 - 0) + 1)));
You should write a cleaner code.
I've prepared a solution for you: a function which generates a List with random numbers and respects you condition: two consecutive numbers are not the same.
You must call this method generateRandomList with the number of elements you want to generate.
public static final Integer MAX_RANDOM_NUMBER = 100;
public static List<Integer> generateRandomList(int randomNumbers) {
return generateRandomList(randomNumbers, -1);
}
private static List<Integer> generateRandomList(final int randomNumbers, final int previousNumber) {
if (randomNumbers == 1) {
return new ArrayList<Integer>() {
{
add(getNextNumber(previousNumber));
}
};
} else {
return new ArrayList<Integer>() {
{
int value = getNextNumber(previousNumber);
add(value);
addAll(generateRandomList(randomNumbers - 1, value));
}
};
}
}
private static int getNextNumber(int previousNumber) {
boolean generateNewValue = true;
int currentValue = 0;
while (generateNewValue) {
currentValue = (int) (Math.random() * MAX_RANDOM_NUMBER);
generateNewValue = currentValue == previousNumber;
}
return currentValue;
}