Arrays Outside Of Methods
Aug 22, 2014
I'm working on a side project, which will eventually hopefully be a Pokedex, and I've just been going to it at the end of every chapter and using the stuff I've learned to work on it.So I just read chapter 3, which is all about variables and teaches how to use arrays.
my question is, does an array have to be declared inside a method? Because I'm trying to create an array inside a class without any methods and without the main, and I continuously get errors. Here's a quick working of my code that won't compile.
class blah {
blah a[] = new blah[7];
a[0] = new blah();
}
The error message focuses on a[0] = new blah(); Telling me the 0 should be a ], the = is an illegal start of type, so on and so forth. The program compiles completely fine if it's within a method, like this:
class blah {
void a() {
blah a[] = new blah[7];
a[0] = new blah();
}
}
Or if I have public static void main (String[]args); But I'm trying to practice working outside of main.So does an array have to be within a method,
View Replies
ADVERTISEMENT
Jan 4, 2015
I am receiving one error when using arrays and methods. It's at the bottom of the code and I have commented what the error is .
import java.io.*;
import javax.swing.*;
public class CEO_PROGRAM_PartB
{
public static void main (String[] args) throws IOException
[code]....
View Replies
View Related
Nov 13, 2014
I am designing a program that generates 3 random numbers from 1-5 and if 2 match, the user wins $1. If 3 match, the user wins respectively:
All 1s - $5
2s - $10
3s - $25
4s - $50
5s - $100
I first used a loop to run until the user runs out of money or wishes to stop. Then I made 2 methods. 1 to generate the random numbers and 1 to see if the user won any money. I am storing the random numbers in an array called slotnumber.
This is what I have so far but I am getting compiling errors at while (cont == 'y') {
import java.util.Scanner;
import java.util.Random;
public class SlotMachine
{
[Code]......
View Replies
View Related
Sep 27, 2014
I am trying to get the average of 3 different fraction arrays. I made a fraction class and I made methods such as read() and average() in this new class.
package fractions;
import java.util.Scanner;
import java.util.Arrays;
public class FractionArrays {
public static void main(String[] args) {
Fraction completeFraction = new Fraction(5,6);
[Code] ....
I was wondering if there was any way to use the arrays I created in the read method in the average method. If I find that out I should be able to do it on my own. Is there a way to make the arrays public to use in different methods?
View Replies
View Related
Nov 19, 2014
Here are the errors I am getting:
SlotMachine.java:21: error: illegal start of expression
public static void getNums(int [] slots)
^
SlotMachine.java:21: error: illegal start of expression
public static void getNums(int [] slots)
[code]....
i keep fixing small things and cannot get it to compile. Below is the original code,
import java.util.Scanner;
import java.util.Random;
public class SlotMachine
{
public static void main (String args[]) {
int userMoney;
Scanner input = new Scanner(System.in);
System.out.print("How much money to start with?
[code]....
View Replies
View Related
Jul 23, 2014
My assignment is to essentially update a calculator program that uses strings, and methods, to include arrays. How to create and call an array that the user defines the size of and then inputs the numbers to fill the array. Here's the example my prof gave us of what the output should look like:
Menu
1. Add
2. Subtract
3. Multiply
4. Divide
5. Dot product
6. Generate random array
7. Quit
What would you like to do? 1
How many values are in the arrays? 3
Enter the values in the first array, separated by spaces:
2 4 6
Enter the values in the second array, separated by spaces:
1 3 5
The result is [3.0, 7.0, 11.0]
How to create an array that would allow the user to define the size of the array and then inputs the values for the array? I'm completely lost. I never should have taken java as an online class.
View Replies
View Related
Jun 28, 2014
I mainly would like to know is a int[4][4] or a int [4*4] is more efficient to use. Which takes more storage? Which requires more to process? that kind of stuff.
View Replies
View Related
Apr 23, 2015
I'm wonder about the issue of constructor for arrays. Let say I have a class tablica, and one component int[] tab. If I get it right until now tab is nothing more than empty reference to some unexisting array?
import java.util.Random;
class tablica{
int[] tab;
tablica (){ // i wish it was constructor
[code]....
Then, I'm trying to build the constructor for class tablica. What can be the parameter of such constructor? Is it fields of array? It is simple forf for basic variable
- I liken values defined in constructor with those global defined in class. But how to do it with array component tab.
If I create array object in main method then how can I use this constructor?
View Replies
View Related
Sep 19, 2014
How do i increase the size of a 2D array from 2X2 to 4X4 where only the boundary elements are zeroes and the rest of them remain the same?
View Replies
View Related
Jan 28, 2015
In the program below I populated three arrays: student id, name, and GPA. The findStudent() method attempts to match users input with the input stored in the studentID array using an if/ else statement. When I use the if/ else statement the else always executes regardless if the IDs match? I am trying to get JOptionPane.showMessageDialog(null, "Incorrect entry"); to print only if the IDs don't match.
Java Code:
//FILE: StudentIDArray.java
import javax.swing.*; //Used for the JOption dialog boxes
import java.util.*; //Used for Scanner input
public class StudentIDArray {
boolean nameFound = true;
final int numOfElements = 2; //Final int to control to loop for data
[code]....
View Replies
View Related
Nov 22, 2014
I have created a class and a matrix of doubles (or at least, I think I have, that's partly what I want to verify).I need to return the values of the array,Here is my class:
public class example{
double[][] Position=new double[2][11];
double calculate(){
for (int time=0;time<=10;time=time+1){
Position[1][time]=time;
Position[2][time]=time+1;
double A=Position[2][time];
return A;
}
}
}
I am getting the error: "This method must return a result of type double", though to me it looks like I am returning double (A).
View Replies
View Related
Mar 15, 2015
I am having trouble conceptualizing how to fill arrays. I understand that I can declare one and number it's elements, and it stores values of a certain data types. I don't really understand beyond that. Like how I would fill and use data in it.
I can be more specific with an assignment I have for school, but need to get started understanding how to fill arrays with data and then how to use that data.
View Replies
View Related
Sep 21, 2014
So im making this ghost game where i display an 8x8 filled with 0s and a randomly generator five 1s in there I can get it to display 0s and add 1s, however sometimes the 1s that are randomly generated sometimes go on the same spot making it look like there are only four 1s. How would i go about fixing that?
package Grade12;
import java.util.Random;
public class Ghost {
public static void main(String[] args) {
Random generator = new Random();
int gameboard [][] = new int [8][8];
int randomx, randomy, counter = 0, sum = 0;
for(int row = 0; row < 8; row++){
for(int col = 0; col < 8; col++){
(gameboard[row][col]) = 0;
}
[code]...
View Replies
View Related
Mar 4, 2014
The assignment goes like this...Write a Payroll class that uses the following arrays as fields:
employeeID - An array of seven integers to hold employee identification numbers. The array should be initialized with the following numbers:
5658845452012578951228777541
845127713028507580489
hours - An array of seven integers to hold the number of hours worked by each employee.payRate - An array of seven doubles to hold each employee's hourly pay rate.wages - An array of seven doubles to hold each employee's gross wages.The class should relate the data in each array through the subscripts.
For example, the number in element 0 of the hours array should be the number of hours worked by the employee whose identification number is stored in element 0 of the employeeID array. That same employee's pay rate should be stored in element 0 of the payRate array. In addition to the appropriate accessor and mutator methods, the class should have a method that accepts an employee's identification number as an argument and returns the gross pay for that employee.Demonstrate the class in a complete program that displays each employee number and asks the user to enter that employee's hours and pay rate. It should then display each employee's identification number and gross wages.Input Validation: Do not accept negative values for hours or numbers less than 6.0 for a pay rate.
My problem with this program is that everytime I try to print the employee ID's or the wages, I get hashcode or something like it (#[I1a77cdc or something like that). I tried using the toString method, but it lists all of the values, when I'm trying to display one at a time. Here is the code for the class:
// moduleArray class
public class moduleArray {
final int NUM_EMPLOYEES = 7;
int[] employeeID = {5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 7580489};
int[] hours = new int[NUM_EMPLOYEES];
[code]...
This is the demo program to list the ID's. I've been messing with it for some time, and right now I just want it to display values.
import java.util.Scanner;
public class moduleArrayDemo {
public static void main(String[] args) {
final int NUM_EMPLOYEES = 7;
int[] ID = new int[NUM_EMPLOYEES];
[code]...
View Replies
View Related
Dec 14, 2014
Okay, so the assignment was creating a word search for the given array. I've created all the code necessary, but I've ran into trouble. This problem occurs with the Up-Forward, Up-Backward, Down-Forward, and Down-Backward sections. If I only set one of these to run, it works. But if I have all 4 set at the same time, it errors out on the first one that runs.
public class P_WordSearch {
public static void main(String[] args) {
char[][] puzzle = {
{'B','O','O','G','S','R','O','W','I','N','G'},
{'E','B','L','G','N','I','M','M','I','W','S'},
{'L','C','E','A','T','I','P','U','P','I','S'},
{'C','M','I','N','C','A','X','Y','O','S','N'},
[Code] ....
View Replies
View Related
Dec 15, 2014
I am trying to concatenate two arrays but on on compiling it is showing
Java Code: import static java.util.Arrays.*;
class Mergesort
{
public static void main(String...s)
{
Mergesort r=new Mergesort();
int x[]={1,2,3,4,5,6};
int y[]={9,8,7,15,14,13,12,11};
[Code]...
View Replies
View Related
Dec 5, 2014
The assignment was to create 3 parallel arrays to make a student database.The first array will contain 4 digit student id's, the second a string array with student names, and the third array is student gpa's. The user is to receive a dialog box asking to enter the student id, and if the id is correct the user is to see the student name and grade. If the user input does not match any value in the student id array, the user is to receive a message stating invalid id. Here is the code I have so far.
For some reason no matter what the user enters, the information for the last array entry is displayed.
public class parallelStudent
{
public static int sequentialSearch(int[] array, int value)
{
int index; //loop control variable
int element; //element the value is found at
boolean found; //flag indicating search results
[code]...
View Replies
View Related
Nov 24, 2014
We had to use these methods with the given parameters and code them correctly. We cannot use any built in java methods for sets.
import java.util.Scanner;
public class setPractice {
public static Scanner kbd;
public static final int MAXSIZE = 20;
public static void main(String[] args) {
kbd = new Scanner(System.in);
int[] setA = new int[MAXSIZE];
int[] setB = new int[MAXSIZE];
int[] intersect = new int[MAXSIZE];
[Code] ....
This is the input given to the program:
How many numbers will be in the 1st set: 3 Enter list of integers for 1st set: 12 3 2
The ascending order for 1st is: 2 3 12
How many numbers will be in the 2nd set: 4
Enter list of integers for 2nd set: 2 3 6 1
The ascending order for the 2nd set is: 1 2 3 6
The intersection of the two sets is: 0 0 0 //Program not correctly print intersection of two sets
The difference of A-B is: //Program is not printing the difference either...
View Replies
View Related
Apr 13, 2014
I am having trouble grasping 2 dimensional arrays .I have a certain problem:
String[][] words = {
{"Red", "Blue", "Green"},
{"Orange", "Purple". "Yellow"}
[code]....
I ran the code and got OrangePurpleYellowRedBlueGreen.
View Replies
View Related
Sep 13, 2014
import java.util.*;
public class OneDimenArray {
public static void main(String[] args) {
double[] decimals = new double[12];
double nums = 0.0;
double a = 1.0;
[Code] ....
These are the erroe codes I'm getting;
OneDimenArray.java:13: error: ']' expected
double[] decimals = double[Scanner.nextDouble()];
^
OneDimenArray.java:13: error: ';' expected
double[] decimals = double[Scanner.nextDouble()];
^
2 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
View Replies
View Related
Sep 15, 2014
I need to send a byte array across a network. I know how to do this. (server->client)
byte[] myArray = new byte[]{0,1,2,3,4,5,6,7,8,9};
DataOutputStream.write(myArray);
... and I know how to receive it.
byte[] myArray = new byte[10];
DataInputStream.read(myArray);
When I send over one of these arrays, it ends up stopping storing values in the array when only about half the array is received, even though the array is sent from the server all at once. This results in the receiver's array, in this case, being something like {1,2,3,4,0,0,0,0,0,0}.
I can easily solve this - and already have - by simply adding a loop onto it and waiting for the bytes to all be received, as the method returns the amount of bytes actually read.
byte[] myArray = new byte[10];
int bytesRead = 0;
while( bytesRead < myArray.length) {
bytesRead += DataInputStream.read( myArray, bytesRead, myArray.length - bytesRead );
}
I am simply wondering if there is a better solution to this, as the current solution isn't that elegant. Did I do something wrong to cause only a part of the array to be sent first? Would it be better to use DataInputStream.readByte() to read off the bytes one by one rather than an array at once, and then store them in an array afterward? Would this cause a performance decrease as each byte is read individually? (I'm sending an array of several million bytes)
View Replies
View Related
Apr 22, 2015
I am writing a code based on the following question: "Write a method called arrayMin that accepts as an argument an array of numbers and returns the minimum value of the numbers in that array.Create an array to test your code with and call the method from main to print the min to the screen".
I cannot seem to get the code to calculate the minimum number.
Here is my progress thus far:
import java.util.*;
public class Lab11q3
{
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
int number;
[code]....
View Replies
View Related
Jun 30, 2014
I am making a 2d game engine and i was wonder is it better to use BufferedImages and subImages to store/render sprites from sprite sheets or use BufferedImages and store it in a pixel array and then manipulate the pixel array to do what you want.
Basically is loading in BufferedImage and getting the tile of the sprite sheet with subImages better than loading in a BufferedImage and then putting the data in a pixel array and making a new array with the part of the BufferedImage you want.what i have been told the BufferedImage and subImage use more of the graphics card and the pixel array method uses more of the processor.
View Replies
View Related
Mar 26, 2015
how to loop my arrays, so when I sort them (highest/lowest) the 2nd array corresponds to the sorted array
import java.util.Scanner;
import java.util.Arrays;
public class ArraysArrays {
public static void main (String[] args){
[code]....
View Replies
View Related
Sep 18, 2014
For basic arrays, we can directly combine declaration and initialization as E.g: int[] num= new int[2]
But can we do this do for forming class object arrays? like
class stu
{
.....
}
stu[] s=new stu[2]
????
View Replies
View Related
Feb 19, 2015
I have created an object of arrays, and i need to find the max value...
View Replies
View Related