So I'm trying to write a program that will take in 5 numbers, store them in an array and then print out the array and the average of the array. Problem is that every time I run my program I get a "ArrayIndexOutOfBoundsException: 5" error and I don't know who to fix it.
public class ConstructorHomework {
final static int size = 5;
static double[] myArray = new double[size];
public static double average;
public void printArray(){
I'm trying to calculate the average of grades that a user puts in. Here is my code so far:
public static void main(String[] args) { Scanner input = new Scanner (System.in); System.out.println("Please enter an array of grades seperated bt a comma."); input.nextLine(); String arrayOfGrades = "100,50,100"; String[] grades = arrayOfGrades.split(",");
[Code] .....
I think I'm on the right track, the only big error I'm really getting is the line: sum += grades[i]. It's saying string can not be converted into a double.
public class StuTest { private static Scanner keyboard = new Scanner(System.in); public static void main(String[] args){ int[][] testScores; // [students][tests] String[] stuNames;
[Code] ....
The method I am having issues with is the "printStudentReport" method. I am trying to calculate the average of a randomly generated number of students and tests. For my "printTestReport" method, I have to calculate the average of the test by test number.
Find the numbers which is closest to the average sum for a given array of N (1<=N<=50) of natural numbers. If there are two numbers who meet the requirement, return the smaller of the two.
For example for the array of: 1,2,3,4,5,6 the average sum is 3.5, so both 3 and 4 are the closest to that, but the program has to return 3, because it's smaller than 4.
The array can also contain duplicates. First we type the number of elements in the array, then in each line we add the numbers. Name of the class: Array
**Note**: Create a data structure array and use it.
And this is what they have given to me, I just need to type the code:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Array<E> { public static int closestNumber( ) {
public static int getSum(int[] data) { int sum = 0; for (int i = 0; i < data.length; i++) { sum += data[i]; } return sum;
[code]....
The input is the following arary (Its from the Junit test that fails this): [Integer.MIN_VALUE, -1, 0, 1, Integer.MAX_VALUE].I get an average of 0.0 when it should be -0.2.
Conditions : Need to print all the inputted data(name,score) and the average.
import java.util.Scanner; public class Case2 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int[] score = new int[5]; String[] name = new String[5]; int ave = 0;
So for fun I've decided to write a program that can keep track of a simple card game my friends and I designed. I've built a class for an array, that stores the basic stats of each card. I need to be able to access this array from another class, that will print the array information to a file, so we can easily keep track of who's cards have leveled up and their stats. The main class will also be using a random number generator to determine how much damage the attacker/defender deals and takes respectively. So far I have the random number generator built as well. I'm just having issues creating a print to file class and what I need to change in my array class to make it accessible by the other class that prints it to a file.
public class StartingStats { public static void main(String[] args) { //Variable to use for how many spots in a stat array there are final int statArray = 2;
[code]...
what I need to change so that a print to file class can access EACH array in this array folder. Also if any cookie cutter printToFile class I could use/borrow/change that'd be really useful, as I've never done any printing to file before. Also, the levels of each stat and exp will be changing so that's why I need a separate class to print so that I can call it when it's been updated so we always have the most up to date stats saved.
My array isn't printing. I have tried everything. I have tried putting a nested for loop in different methods but still no luck. I have also tried System.out.print but that prints random characters/numbers.
import java.util.Scanner; public class Square { public static void main(String[] args)
[Code].....
^For some reason the site doesn't show the spaces between the *. It's supposed to be a 5x5 square.
I have tried to print array elements using standard print statement. I am getting errors. How to print them. Here is my code:
class arrayEx1{ public static void main(String args[]) { int a[]=new int[3]; //Declaring Single Diomentional Array a[0]=10; a[1]=20; a[2]=30; int total=a[0]+a[1]+a[2]; System.out.println("Values stored in a[0],a[1],a[2]elements are :" + a[0] a[1] a[2]); System.out.println("Total values of a[0],a[1],a[2]elements is :"+ total); } }
if i give comma (,) in between above print stament (print statement 1) stil i am getting errors.
Ok, so the program we are supposed to do is to create implement a employee class then create a main tester class that can accept user inputs for 5 employee names, salaries, and performance rating. After that, we have to input code to calculate a provided raise amount, then print out the array with the updated raises. I have completed the program with no syntax errors, but it doesn't do anything. not even a screen comes up on my end to accept user inputs. I know its something basic, but what am i missing
Employee class public class Employee { private String employeeName; private int salary; private int performanceRating;
I have a program that works, but I would like to know an easier way to record and print from a large array.
Here is what I have
package pa2; import java.io.IOException; public class PA2Delegate { //long[] array = new long[100000]; int arraySize = 100000; int iterations = 9999; Long[] array;
I am having a hard time trying to figure out how to print random numbers from a an array list. I tried google but nothing worked. I have to pick certain values from two lists and print them on the screen. I have included comments in the code to facilitate the explanation.
import java.util.Random; public class Parachute { public static void main(String[] args) { Random randomNumbers=new Random(); int number; int array []={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21}; char A[] = {'a', 'b', 'c','d','e','f','g','h', 'i','j','k','l','m','n','o','p','q'};
My assignment was to create a class and then write a program that uses the methods of the class I created. All the output is correct except for the Question # Missed. It prints [I@1dd0fe7. The wrong question numbers have to be stored into an array and that array printed. I've tried using a for loop to print, rearranging the code, setting the code to how I did in the other methods. I either get this [I@1dd0fe7 -or- a bunch of 0's. I'm not sure if I am calling the method incorrectly, or just do not have the correct set-up to print the array, or if I am just not grabbing the ones that are incorrect.
public class DriverExam { // Fields private boolean result; private int testTotal; private int numWrong; private int [] numMissed; private char [] answerKey;
public class ArrayPrinter { public static void main(String[] args) { int[] oneD = {5, 6, 7, 8}; PrintArray(oneD); } public static void PrintArray(int[] Arr) { System.out.println ('['); for (int i =0; i >= Arr.length; i++) { System.out.print (Arr[i]); if (i < Arr.length){ System.out.print(", "); } System.out.print (']'); } } }
I tried to format this to enhance readability but I'm not sure if I managed to improve it any... This code should be printing the contents of the array with spaces and commas its just printing [. I think this is because Arr is empty for some reason but I'm not sure why it would be when it gets the values of oneD passed to it.
So I need to generate 10 random integers in the range 1-20 but i have to store them in an array called numbers. Then I have to call a method called displayArray which displays the contents of the array and for the assignment i have to use a for loop to traverse the array. The method header for the displayArray method is:
public static void displayArray(int[] array)
This is what I have done
public class RandomIntegers { static int numbers = 0; public static void displayArray(int[] array) { System.out.println(numbers + "Numbers Generated");
I am taking the Class Algorithms and Datastructures and got an assignment for Lab that really throws me off. The goal is to create an Array out of a given CSV file, implement several Methods that get the size of array, etc.
I am still stuck in the first part where the CSV has to be imported into the Array. My problem is that I need a mechanism that figures out the needed size for the Array, creates the array, and only then transfers the data from the CSV.
The list consists of the following wifi related values:
MAC-Adress, SSID, Timestamp, Signalstrength.
These are on the list, separated by comma. The Columns are each of these, and the rows are the four types of values making up the information on a certain wifi network.
The catch is, we are not allowed to use any of the following:
java.util.ArrayList java.util.Arrays and any class out of java.util.Collection.
So far I used the BufferedReader to read in the file and tried to implement the array, but I get an arrayindexoutofboundsexception.
Below is my Code (Its still an active construction zone):
public class WhatsThere { public WhatsThere(String wifiscan) throws IOException { } public static void main(String[] args) throws IOException { // WhatsThere Liste = new WhatsThere(String wifiscan); String[][] arrayListe = new String[0][0];
I am working on a problem where i have to create a 2d array with given input of the dimensions (odd number) of array, along with a number within the array and to then print out all of the numbers surrounding that number.
Anyway, i am working on simply making the spiral, which should look like the one below.
n = 3
7 8 9 6 1 2 5 4 3
where the 1 always starts in the center with the 2 going to the right, 3 down, then left etc. etc. I was able to create the code by starting on the outer edges rather than the center and working my way to the middle, however my code always starts from the top left and goes around to the center where it needs to start from the top right. I am having trouble altering my code to meet this criteria. This is what i have thus far.
import java.io.*; public class Spiral { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter the number of elements : "); int n=Integer.parseInt(br.readLine());
We were given a class lab that asks us to write a program that create a multidimensional array ( 5 x 5 ), populates the array using nested loops with letter from A until Y, and displays the array to the screen. and the result should look like this:
A B C D E F G H I J K L M N O P Q R S T U V W X Y
How to write this program.. I have tried all my best but the results are not coming like this..
I have the following code in which I am looping through the rows of one array (composed of Strings) and copying it to another array. I am using .clone() to achieve this and it seems work as it changes the memory location of the rows themselves. I did notice that the String objects are still pointing to the same location in memory in both arrays but I won't worry about that for now, at the moment I just want to understand why the array I am cloning is not successfully assigning to the other array.
This is the incorrect line: ar[r] = maze[r].clone();