How can I access the index of one character array and store those indexes into another array? I need this array of indices so as to perform an addition with another array.
Suppose I have a char array that stores all the letters of the alphabet (say alpha) and I have an another char array (say letter) that contains some letters in it. I want to retrieve those letters from the "letter" array and check its index in the "alpha" array and store that index into another integer array.
I am receiving an ArrayIndexOutOfBoundsException for the following code, which moves a creature through a 2D array maze. I have altered the clauses of the first if statement for the four direct methods (north, south, east, and west) multiple times (i.e. x + 1 >= 0 && x > 0 && x - 1 > 0 && x < array.length...etc). However, while the code occasionally runs, more often than that it returns this exception. Catching the exception seems like a poor workaround though if worst comes to worst I'll do that.
I included only the relevant functions of the code:
public boolean goNorth(char[][] array) { boolean success = true;; x = getX(); //x = this.x; y = getY(); //y = this.y; if ((x - 1 >= 0 && x - 1 < array.length) && (y >= 0 && y < array[x].length)) {
// 1 ***** student writes this method /** Searches for key in integer array named arr // arr is an instance variable of the class and has been instantiated and filled with random values. // @param key value to search for // @return if key is found, the index of the first element // in array whose value is key; if key is not found, // the method returns -1 */
public int sequentialSearch( int key ) { // Note: To animate the algorithm, put this method call as the first statement in your for loop // animate( i, 0 ); // where i is the index of the current array element return 0; // replace this statement with your return statement } // end of sequentialSearch
class Test { public static void main(String[] args) { int arr[]={1,2,3,4,5}; int search = 5; int i=0; boolean flag=false;
[Code] .....
Above program runs fine.Above in each iteration we have 2 conditions, first to check if iteration number is less then array length and second is to match it with the search integer.I am required to reduce these two conditions and make it one only.Have tried it but no success.
So I'm trying to write a program that prints out the "most-repeated integer" in an Array.
For example: if an array contains {1,2,2,3} It would print out 2 as the result.This is what I got so far and according to my knowledge I think I'm correct but for some reason it doesn't work.. Please give me some inputs.
public class MostInt{ public MostInt (){ int[] array = {0}; for(int i = 0;i>array.length;i++){ if(i==i++){ System.out.println(i);
I have to shuffle a deck (array) of 52 integers but I started with 3 for testing if it was an even shuffle and it will place the same integer in more than one spot in the random array. I'm not sure what I'm doing wrong...
import java.util.Random; public class shuffleDeck { public static void main(String[] args) { int[] Deck = new int[3]; for (int i=0; i<3; i++) {
Run the code along with the attached csv file. The GUI contains a short explanation of what I am looking for. I have tried converting the integer array to a string array but the output is not the same as the command line. I receive errors when I compile.
trying to get into Java and jump into just programming an idea.I want to go through an array of numbers at a certain pace and call different sounds as it goes through.
Write a program to create an integer array of size 20. Then, the program should generate and insert random integers between 1 and 5, inclusive into the array. Next, the program should print the array as output.
A tremor is defined as a point of movement to and fro. To simulate it, the program will generate a random number between 0 and 19, which represents the location in the array (i.e. index number). Then, the 3 numbers to the left and right of this location should be reset to the value 0. If there isn't 3 numbers to the left and right you may assume a lesser number depending on the boundaries of the array.
Then, the final array should be printed as output. There is no user input for this program.Your program must include, at least, the following methods:
-insertNumbers, which will take as input one integer array and store the random numbers in it. -createTremor, which will generate the random number as the location and return it.
-Create a main method declares and creates an integer array called nums that can hold 15 integers.
-Use a for loop to fill that array with multiples of 3: 0, 3, 6, 9, etc.
-Then use similar for loop to print each value in the array on one line, with each value separated by a single space.
-Compile and run the program to see the result:0 3 6 9 12 15 18 21 24 27 30 33 36 39 42
As you write other methods, you'll also modify the main method to make calls to them. The printArray MethodWrite a method called printArray that accepts an integer array as a parameter. This method does not return a value, and must be declared as static so that the main method can call it. Instead of printing the array in the main method, move that loop into this method. Call the printArray method from the main method. Compile and run the program to verify it prints the sam result as before.Add a println statement so that after printing the array values on one line, it then moves to the following line.Finally, modify the loop in the printArray method so that, instead of using a traditional for loop, it instead uses a for-each loop. Compile and run the program again.
Part III: More Array Methods
The linearSearch Method In lecture we looked at a method that performed a binary search on a sorted array. A much simpler (though much less efficient) search is a linear search, that simply starts at the front of the array and looks at each element in turn until it finds it or reaches the end.Create a method called linearSearch that accepts an integer array and a single int value as parameters. The goal of the method is to find the second parameter (the target) in the array. The method should return a single int representing the index of the target value. This method should not print any output itself. In this method, use a traditional for loop to scan through the elements in the array. As soon as you find the target value, return the index of that value.
If you scan through the entire array without finding the target value, return a -1.Modify the main method to call the linearSearch method and print the results. Call it twice, searching for the value 18 (which it should find) and the value 10 (which it should not). Including the previous activity, the output of the main method should now look similar to this:0 3 6 9 12 15 18 21 24 27 30 33 36 39 42
The index of 18 is 6
The index of 10 is -1
The sumArray Method
The sumArray method should take an integer array as a parameter and return a single integer representing the sum of all values in that array.Use a for-each loop to access each value in the array and compute a running sum. After the loop, return the total.Call the method from the main method, producing the following augmented output:0 3 6 9 12 15 18 21 24 27 30 33 36 39 42
The index of 18 is 6
The index of 10 is -1
The sum of this array is 315
The addValue Method...The addValue method should accept an integer array and a single int as parameters. The purpose of the method is to add the second parameter to EACH value in the array. The addValue method does not return a value, but the elements inside the array will be modified. Call the addValue method from the main method, adding 100 to each element in the array. Then call the printArray method again to see the modified array values:0 3 6 9 12 15 18 21 24 27 30 33 36 39 42
The index of 18 is 6
The index of 10 is -1
The sum of this array is 315 100 103 106 109 112 115 118 121 124 127 130 133 136 139 142
Test a Different Array..Finally, duplicate the content of the main method to perform similar tests on another array. Instead of filling it with multiples of 3, fill it with multiples of 4. And instead of using an array size of 15, use an array size of 20.Modify the values search for to include one that is in the array and one that isn't.Rerun the main method and carefully check the results.If you haven't been doing it all along (which you should), make sure the appropriate class and method documentation is included.When you're satisfied that all methods are working correctly, modify the main method to delete the second array tests.
package question.pkg3; import java.util.Scanner; public class Question3 { public static void main(String[] args) { // TODO code application logic here Scanner Luka=new Scanner(System.in); double sum=0;double count=0; int[] a=new int[10];
[code]....
I'm required to write a program that allows the user to enter up to 10 integer grades into an array. Stop the loop by typing in ‐1. Your main method should call an Average method that returns the average of the grades.I There's something wrong with my program , the count always stays 0 and the sum is always 1 less than the actual sum.Sample input and output :
Enter grade 1: 8 Enter grade 2: 9 Enter grade 3: 10 Enter grade 4: 5 Enter grade 5: 8 Enter grade 6: 9 Enter grade 7: -1 output
Average grade is 8.1666666667On line 13 I had count=count+1 ;
Question - Given an specific integer and an array of integers, find and remove all occurrences of the given integer and return an amended array. I solved it. Here is my solution -
a. Write a Java program to input 10 integer numbers into an array named fmax and determine the maximum value entered. Your program should contain only one loop, and the maximum should be determined as array element values are being input. (Hint: Set the maximum equal to the first array element, which should be input before the loop used to input the remaining array values.)
b. Repeat 1a, keeping track of both the maximum element in the array and the index number for the maximum. After displaying the numbers, display these two messages:
The maximum value is: _________ This is element number __________ in the list of numbers
Have your program display the correct values in place of the underlines in the messages.
c. Repeat 1b, but have your program locate the minimum value of the data entered.
I did parts a and b but for part see i just want to know if i did it correctly or not
import java.util.Scanner; public class MinimumValueArray { public static void main(String[] args) { //Variable Declaration Scanner keyboard = new Scanner(System.in); int size = 10;
[Code] ,.....
When I run it i get this The minimum value is 0.0
The element that holds the value is 0 right away. is this right for the minimum or am i supposed to enter values and it will display the minimum value like in parts a and b wit the maximum? will the minimum just always be 0 or ?
I have problems getting the right number of times for each number of the array. Here is my code:
public static void main(String[] args) { int[] a = { 3, 13, 5, 9, 13, 6, 9, 13, 2, 3 }; int num = 3; int count = numbers(a, num); for (int i = 0; i < a.length; i++) {
1. Create a program that will return the maximum and minimum numbers in the elements of ONE-dimensional integer array. Save it as MaxMin_OneDim.java
2. Create a program that will return the maximum and minimum numbers in the elements of each row in a TWO-dimensional integer array. Save it as MaxMin_TwoDim.java
3. Write a program PrintPattern which prompt a user to enter a number and prints the following patterns using nested loops (assumed user entered number is 8 output is:)
1 .... 87654321
12 .... 7654321
123 .... 654321
1234 .... 54321
12345 .... 4321
123456 .... 321
1234567 .... 21
12345678 .... 1
(Without the dots, i just put them to give spaces)
Write a function that accepts an array of non-negative integers and returns the second largest integer in the array.
Return -1 if there is no second largest.
The signature of the function is int f(int[ ] a)
Examples:
if the input array isreturn{1, 2, 3, 4}3{{4, 1, 2, 3}}3{1, 1, 2, 2}1{1, 1}-1{1}-1{}-1
In the signature what I understood is, I should write my function with the given signature,
The return type is "int"
method name is "f"
parameter is "a" right ?
Writing my doubts beside the particular line in the code
public static void main() // In the answer why they didn't use the class ?
In main method why they didn't use parameters ?(String[] args)
{ a1(new int[]{1, 2, 3, 4}); // what is "a1" here is it array name ? this line initializing the array ? a1(new int[]{4, 1, 2, 3}); a1(new int[]{1, 1, 2, 2}); a1(new int[]{1, 1}); a1(new int[]{1}); a1(new int[]{}); }
static int a1(int[] a) // what is "a" here parameter ? and "a1" is method name ? why they used the array name and method name same ?
{ int max1 = -1; int max2 = -1; for (int i=0; i<a.length; i++)
I am trying to make different arrays each being filled with random numbers from 0 to 2000, however when I run this code I get the error mentioned in the header. here is part of my code
for (int i = 1; i <= 14; i++) { int n = (int) Math.pow(2, i); int[] list = new int[n]; for( int j = 0; j <= list.length; j++){ list[j] = (int) (Math.random() * 2000); } }
I've been trying to work with custom Exception classes, but I keep running into what I think is an array error. It's in a very monolithic format because I was just trying to bang it out and get it done. Anyway, my issues is I am trying to compare values in the array to the minimum and maximum possible scores for a student (0 and 100) but I have totally forgotten how to do it.
Here is the code, the offending bit is at the very bottom:
package org.CIS407.Lab6; import java.util.*; public class TestScore { public static void main(String[] args) throws ScoreException { Scanner scan = new Scanner(System.in); int sz; //holds scanner values int[] studentArray;
[Code] .....
Right now I'm getting an error when I go to enter the very last student score. It throws an exception.
Here is a sample output of what I'm getting:
Enter number of students 2 Array created successfully. Enter student ID's into array 1 2 Enter number of scores 2 Array created successfully. Enter student scores into the array 50 32
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at org.CIS407.Lab6.TestScore.main(TestScore.java:64)
I have an assignment on sorting, i kno i can get the sorting down but im having an issue with inputing the 512 ints in a file into an array. the instructor provided us with a file with 4 equal sets of ints. i tried to make my array of size [scan.nextInt()] and it cuts off the last 21 ints. and skips the first int. how can i get all of the integers in the text file into my array? this is what i have so far. if i hard code the array to size 50000 and then try to print the array it compiles but errors out when running it.
System.out.println("Please Enter text file in this format, XXXXX.txt :"); String file =fileName.nextLine(); Scanner scan = new Scanner(new File(file)); int [] data = new int[scan.nextInt()]; <-------here it skips first int int count= data.length; for (int i=0; i<data.length-1;i++) { data[i]=scan.nextInt(); } System.out.print(Arrays.toString(data));
rst 4 ints in output are: 501, 257, 390, 478...., supposed to be 492,501,390....and last ints are: ....88, 83, 79, 0 and supposed to be :88 83 79 77 76 72 71 71 66 57 56 48 48 41 33 30 23 23 18 17 15 13 9....it replace last ints with 0. why ? and how do i fix this. attached it the text file