class array { public static void main(String[] args) { int a[] = {1, 2, 3}; int b[] = {4, 5, 6}; int c[] = {7, 8, 9}; Object arr[] = new Object[4]; arr[0] = a; arr[1] = b; arr[2] = c; int d[] = (int[]) arr[0]; for ( int i=0;i |
I want to create several arrays to hold different pieces of data. The data will be number related and it would be convenient to make each number in the array be appropriate to the string held there. for instance month[1] = "January" month[2] = "February" *assume I don't need these months* month[10] = "October" month[11] = "November" Can I just omit ... |
In an IF loop, I want to evaluate the entire contents of a char[] array to see if any of the characters in it match and take action accordingly. I'll post what I have so far. Any help on this would be extremely appreciated, and please note I cannot use regular expressions in this (yes it is an assignment, I'm not ... |
I have a program with three classes the first `Employee class` introduces a hashIndex calculated from empName the second `EmployeeSort` takes the hashIndex and places the employee class instance into an array of which will be eventually be Employee class objects.I have the Last class passing the name and job using the JOptionPane unfortunately each pass overwrites the last entry in ... |
Hello folks, I am currently working on a problem that inputs 20 numbers into an array and the numbers being in the range of 0 to 100. I had to calculate smallest, largest, mean and average of all the numbers in this array which i was able to do without any problem. But the second half of the problem is to ... |
6. Arrays coderanch.comIf you need to use arrays, you have two choices: 1. Create a class to hold both fields, then add objects of that class to the array; 2. Create two parallel arrays, one for each field. When you save the data or retrieve the data, you use the same index for both arrays. Hope that helps. |
Hello, I am working on an array program. I have some array based questions. How can I limit the user input to 80? When I run the program it allows me to enter in as many as I want. Is there a way to know if the user has input symbols instead of letters or numbers? I am going to break ... |
|
8. Array coderanch.comHi, Welcome to JavaRanch! We don't like to do people's work for them, but we do like to help people learn. This is a simple sort of program, with three parts: 1) Collect some input 2) Compute some numbers 3) Print a report Tackle each part separately. So: go get started. Come back to this thread and show us what you ... |
import java.awt.*; import java.applet.Applet; import java.awt.event.*; public class Array extends Applet implements ActionListener { private Button throwDice; private boolean thrown = false; public void init() { throwDice = new Button("Price List"); add (throwDice); throwDice.addActionListener(this); throwDice = new Button("UB 40"); add (throwDice); throwDice.addActionListener(this); throwDice = new Button("WD 40"); add (throwDice); throwDice.addActionListener(this); throwDice = new Button("R2D2"); add (throwDice); throwDice.addActionListener(this); throwDice = new Button("C3P0"); ... |
Hi all, I was asked by someone to give comments on some java statements. The statements and my comments are as under. I thought well before answering and also verified by making a small program. But I was told that I was wrong. I cant figure out which comment is incorrect. No 1: -------- char string [50]; This is an invalid ... |
Is there existing function or easy way to check if the object is included in array, like contains of arraylist? I know for loop can do it, but tired to insert the for loop in so many places. Hope to get some quick method or existing one? or if there is quick way to convert array to arraylist without using the ... |
For built-in types, like char, int, float, etc., this is tough. You will have to define what "empty" means. Since you are using a char array, are there specific characters you are using? What is the purpose of the array and what are the valid characters that can be stored in it? You can choose a different character to represent "empty". ... |
13. Array coderanch.comDear all, Suppose I have an array whereby Singer has members like title,year,price; am I rite to do it in the following manner? Singer CD[] = new Singer [30]; Singer tmpCD = new Singer(); for(i=0; i<10; i++) { tmpCD.title = 'retrieve info from file'; tmpCD.year = 'retrieve info from file'; tmpCD.price = 'retrieve info from file' CD[i] = tmpCD; } or ... |
|
ganesh -- I don't think Mr. Stoddard is answering the question you asked, so I'll give it a try. Java "2D" arrays aren't really 2D, as you seem to know: a 2D array is a 1D array of column arrays. The number of columns is thus fixed, but the number of "rows" in each column can vary. You're asking, I think, ... |
Hi When i run this program it only prints to the screen the last name and course enterd, it all compiles ok, could somebody have a look at the code for me and if possible point out where i have gone wrong. public class Student { // Private members private String name; private String course; // constructor for new student object ... |
|
|
Sorry about the lack of info any way here is the error if i set the array size 10. Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 at arrays.Storage.addStudent(Student.java:69) at arrays.StudentTest.main(StudentTest.java:19) this happens after i have added five new names to the array here is the add method i have: public void addStudent(Student studentObj, int count) { int hash = studentObj.hashCode(numOfStudents); int index ... |
Marilyn means that Arrays are special cases in java - they work a little differently than other classes. so when you say int[] houses=new int[20]; Java knows you are making an array of ints (or whatever you're making an array of, and knows how to make that array. i THINK it is just grabbing a contiguous block of memory large enough ... |
I'm used to using if (thisString != null && thisString.length() > 0) I'm getting back an empty array (from within a serialized object over Sockets), my question is this - how do I check for an empty array? Example... public static void main(String[] args) { String[][] x = new String[0][0]; if ( x == null ) { System.out.println("x is null"); } ... |
|
|
i want to use an array to store the item from database instead of hard coding the item which is shown at the last part of the code. When i do a System.out.println(itemName + serialNumber+ carModel + listPrice); , it did show the item from database but when i use an array to store it, it does not show the correct ... |
Hi again. Is it possible to increment an array? I need to determine what amount of 12 has a certain characteristic, with the others not having that characteristic. For instance, consider the following code snippet: int z = 0; if (myMonth [z].Days == 31){ myCount z ; //myCount being declared as a new array for the counter System.out.println("These months have 31 ... |
I'm guessing you're getting a null pointer exception? what i think is happening is that while you declare your array to hold three elements, you have never CREATED those elements. In other words, your array is all set and waiting to hold three Values. when you call UPDATE_VALUES[i].setFieldValue(fvalue); you're saying "call the method setFieldValue() on the object in position i". There ... |
Hello everyone first off let me say that this assignment is for school but I'm not trying to mooch I want to learn. My assignment is to write 8 different methods that do vaious things to arrays. I have done a couple of the methods no problem but on this one I don't even know where to start. The Idea is ... |
Hi, I'm in a newbie Java class and I'm stuck on a really annoying problem. Ok, here is my code. The code is receiving an array from DataWharehouse class (weight[]) and I have to calculate the elements into kilograms and send them to another class file (HealthRiskCalculatorTester). public class HealthRiskCalculator { public double kilosFromPounds() { DataWharehouse data = new DataWharehouse(); double[] ... |
hi all i have created 2 int array both have the same size of 15... i don't have to fill it all in ... so it can be half of the array filt in with number and the rest will be zero's by default... so each element can have only one digit example: int[] arr1 = {1,2,3,4,6,8,5} int[] arr2 ={1,0,0,2,3,2} int[] ... |
|
Hello, My professor wants me to "swap" the values of the first and the last array object and I am having a hard time figuring this out. I don't know if I should create another "for" loop to code this swap or if I can just code one line and make the first value equal the last value. Here is the ... |
1st up here's your code indented, use the code tag to preserve this. public BigInteger getUserLimit(User user){ String userid = user.getUserID(); String[] userlimit = user.getNsklmt(); String entity = getRequisitionID().getEntity(); String[] hospcodes = user.getHospcodes(); int i = 0; while (userlimit == null && i < hospcodes.length){ BigInteger nsklmt = new BigInteger("0"); i++; return nsklmt; } while (userlimit != null && i < ... |
Good nite all, I am somewhat stuck on an assignment problem;which will probably require me seeing the prof again. But, I though maybe I could try to solve as much as I could before seeing him; so it doesn't think I didn't give it my all. Anyways, the problem basically requires input to be read from the console, and each string ... |
Hi, I have two arrays that contain integers. I want to create a new array with only integers that exist in two other arrays. Is there a simple way to do this? I can either create a new array or erase items from one of the arrays if it does not exist in the other. Thanks! |
ok I'm doing a color table array, it displays 100x20 rectangles(256 of them) with 256 shades of green, blue, or red. My name is printed in the very top left had rect. and the rest are also numbered. im having a problem getting all the numbers to show up... here is my code.... import java.awt.*; import javax.swing.*; import java.awt.event.*; public class ... |
I have a program that looks something like this, but longer, of course. public class MixPict { int r = 2; int[] order = {0,1,2,3}; } public class TesttheMixPict { public static void main (String [] args) { MixPict m = new MixPict(); m.r = 3; m.order = {1,0,2,3,4,5,6,7,8}; } } The MixPict class compiled OK and the m.r=3 line compiled, ... |
You've neglected to put braces around the body of this "for" loop: for(int i = 0; i < a2.length; i++) System.out.println (a2.length); a2[i]++; So that the body is actually just the "println" statement, and at the third line, "i" is undefined. If you comment out the "println", then the assignment alone becomes the body of the loop. |
Hi all, first of all nice forum. I have a task that i need to perform. I need to create and array which will store medicine when entered in by a user. the code i have so far is: import javax.swing.JOptionPane; public class ArrayDemo { public static void main(String[] args) { String[] anArray; // declare an array of integers anArray = ... |
|
40. arrays? coderanch.comhello all. i am working on a program that decides if a word or a sentence is a palindrome. this is my first time using JOption. here is my code: import javax.swing.*; public class swain1Palindrome { public static void main (String[ ] args) { String inputSentence; int leftCharInputSentence, rightCharInputSentence; inputSentence = JOptionPane.showInputDialog(null, "Enter a sentence or word: "); getDecision(); getResponce(); private ... |
41. arrays? coderanch.comhello all. i am working on a program that decides if a word or a sentence is a palindrome. this is my first time using JOption. here is my code: import javax.swing.*; public class Palindrome { public static void main (String[ ] args) { String inputSentence; int leftCharInputSentence, rightCharInputSentence; inputSentence = JOptionPane.showInputDialog(null, "Enter a sentence or word: "); getDecision(); getResponce(); private ... |
|
remove(0) in ArrayList is implemented by shifting all the remaining elements, so that may be what your coworker was pointing out. A LinkedList wouldn't have this problem, but I don't know what other operations you are doing on your list -- random access if O(N) on a linked list. In a dimly-lit corner of my brain, I seem to have the ... |
I have created a multidimensional array of class Squares and am assigning some members of the array to a subclass (ie Hotel) of Squares. I need to be able to see what subclass, if any, has been assigned to the Square and then to perform operations on the square depending on which subclass it contains. Here is some of the code: ... |
|
Hello, I am a java newbie and I couldn't find answer to this situation in any of the books available in my college. Please help understand this. How is it that I am not able to assign values to arrays outside of a method? class TestArrays { int classid; int studentids[]= int[10]; classid = 10; // I can assign values to ... |
I have an class that consists of a main method, one other method and an array. It does exactly what I want if I structure it like so: public class MyClass { static String Name[]; public static void main( String[] args ) { name = new String[ 3 ]; name[ 0 ] = "Sam"; name[ 1 ] = "Bob"; name[ 2 ... |
The size of an array is fixed. Your only option is to copy the values into a larger array using System.arraycopy and then assign the original reference to the larger array. Like this double[] values = new double[10]; //fill the array for (int i = 0; i < values.length; i++) { values[i] = i * i; } //oops now we need ... |
|
|
|
private String status=null; public void checkEntryUpload(String lotnum) { try { con = new DBConnection().getConnection(); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM T_UPLOAD where LOTNUM='"+lotnum+"'"); while (rs.next()) { String acclevel = rs.getString("lotnum"); if(acclevel.equals(lotnum)) status = "found"; else status = "null"; } con.close(); rs.close(); stmt.close(); } catch (SQLException se) { System.out.println("SQL Exception: " + se.getMessage() + "not connected"); se.printStackTrace(System.out); } ... |
I have an array which contains the names of days read from a line in a test file which contains other information, such as time etc. For example, com1402, Dr Andy Gordon, Monday, 0900 (The purpose being to record when a lectuer is to take place) The code is written below for (counter=0; counter |
When trying to output an array with System.out.println(DHand1[]); i get an error message '.class' expected import java.io.*; public class Cards { static int DHand1[] = new int[13]; //Creates the array /** Creates a new instance of Cards */ public static void main() { DHand1[2] = 15; //Assigns position 2 of the array with a value of 15 System.out.println(DHand1[]); } } Any ... |
I have an array called chromosomes of 20 Strings: String chromosomes = [chromosome0,chromosome1,chromosome3... chromosome19] I wish to loop through this array creating integer arrays of each String. The following code isn't valid due to incompatible types: for (int i = 0; i < 20; i ++) { String current = chromosomes[0]; current = new int[500]; } How can i name the ... |
OK, I'm writing a Hangman program and it has three classes, Hangman, HangmanFileReader (it gets the words out of a file) and HangmanManager (which acts as a driver) The program runs 100% the way I want it, except for one thing: If the user guesses a letter they have guessed correctly or incorrectly before, it still subtracts from their chances left. ... |
I have a problem and would like to know how best to approach solving it. I have an array of strings that need to be put in reverse order. So I need to read the array the process it and reverse the strings.. any help would be great. Please let me know if there is anything that needs to be cleared ... |
And think about the difference between the memory the array takes up and the memory the objects in the array take up. LargeObject[] array = new LargeObject[ONE_MILLION] The memory used by that is one million references or pointers which are only a few bytes each. Every one of them is null and points to nothing. That might well succeed without blowing ... |
|
You're trying to create an array of ints. For each position in the array you will need 32 bits (not 16) to store the int because the JLS defines the exact size of an int. So, for each int in your array you'll need four bytes. 2147483647 * 4 is 8589934588. Does your system have 8589934588 bytes of memory? No, of ... |
import java.util.*; class MyObject { int x; MyObject(int x) { this.x = x; } public String toString() { return "MyObject_" + x; } } class UnMod { public static void main(String[] args) { LinkedList origList = new LinkedList(); MyObject my1 = new MyObject(1); MyObject my2 = new MyObject(2); origList.add(my1); origList.add(my2); System.out.println("Original List: " + origList); List unModList = Collections.unmodifiableList(origList); System.out.println("unMod List: ... |
hi i need some help with an array it says Hit uncaught exception java.lang.ArrayIndexOutOfBoundsException but im sure the index is ok package Chapter5; // Exercise 5.21 public class TotalHours { public static void main(String[] args) { int [][] hours = {{2,4,3,4,5,8,8},{7,3,4,3,4,4},{3,3,4,3,3,2,2}, {9,3,4,7,3,4,1},{3,5,4,3,6,3,8},{3,4,4,6,3,4,4}, {3,7,4,8,3,8,4},{6,3,5,9,2,7,9}}; int [][] totalhours = new int [8][2]; int x=0, y=0, max=0, maxIndex=0, number=0; for(x=0; x<=7; x++) { for(y=0; ... |
I want to select members of an array that lie between certain values. i.e. i have the array DHand1[0] = 3; DHand1[1] = 4; DHand1[2] = 15; DHand1[3] = 18; DHand1[4] = 25; DHand1[5] = 30; DHand1[6] = 31; DHand1[7] = 35; DHand1[8] = 37; DHand1[9] = 39; DHand1[10] = 42; DHand1[11] = 45; DHand1[12] = 51; |
I want to select members of an array that lie between certain values. i.e. i have the array here. how would i select only the members that lie between two values. For instance I want to select all elements whose value is between 1 and 13. DHand1[0] = 3; DHand1[1] = 4; DHand1[2] = 15; DHand1[3] = 18; DHand1[4] = 25; ... |
hey im just having trouble with this array, its like it entirely misses the part where i swap the elements if one is bigger than the other and i cant figure it out package Chapter5; // Exercise 5.21 // This program adds the total hours from an array and organizes them from // smallest to biggest and then prints the employee ... |
well heres some more code with an array and im trying to multiply the matrixes of two arrays, i think its when i try to assign values to the array d and pass it back. it says the array is out of bounds but ive made sure to check everything and it seems just fine all the array have 5 elements ... |
I am struggling a little with a few ideas and concepts, and so my assignment is getting very behind. I cant seem to connect the right ways of accessing the data of one array from another, ie: importing the data from one to another. I want too create a copy of one array as another in a different class. Cant seem ... |
hi, if this is a given array: Integer[] ir = {12, 45678, 90123, 3}; how do i rewrite this loop: for (int i=0; i |
Hello, I am creting a program in which a need to export the creted data in a folder .txt (or .dat). The data are save in an array type double. I need that somebody indicated to me the steps to create this file and the possible problems. You need more information?? It is possible an example code?? thanks!! |
Java does not directly support multidimensional arrays. It does allow us the functionality of multidimensional arrays by allows arrays of arrays. That is we can an array in which each element is another array. When calling a method or assigning a value, you have to make sure that what you acessing in the array is the correct type. I think your ... |
Yes an array is an Object but an Object is not necessarily an array. It doesn't matter whether or not the Object obj is actually an array of floats, if you declare it an Object then you cannot use it as if it were an array without casting it back to an array. |
Hi. I'm currently facing problems with this kind of thing: I need to do different kind of calculations and then return those results in a new array (I've one previous array but it doesn't affect to this?). How should I do this thing. Am I right if I say that I need first create a new array, then put those results ... |
I'm trying to write a programme that'll create an Array, fill it up with elements, and then tell you.... 1. the elements that were entered. 2. how many times each element is contained in the Array. the user is prompted to enter the numbers one by one.... I've been going round in circles, and to be honest i dont know where ... |
I have an array of 5 JRadioButtons. I want a text field to display the integer value of the subscript. For example, if the Zeroth element radio button is selected, the text field will display 0. I'm not sure of the terminology, but is there a field associated with arrays that will yield this value? For example, the array is called ... |
75. arrays coderanch.comHi, I have a simple problem, I am trying to fill a 2d array 12 rows and five columns with unicode characters from 65 to 124 using nested for loops. I understand how the nested loops work but I cannot work out how to put the correct value into each array. I guess this is a simple math problem which is ... |
When you pass an array as an argument to a function then it's a pass by reference. So what ever the changes you'll do in the function are reflected in the caller function also. Now in your code in function "treble" first line is int [] b =a; // assigning the refrence of a to be. Now what ever u'll do ... |
if ( defined(args[5]) // What is defined() a method of? if ( exists(myArray[3]) ) { // What is exists() a method of? } if ( Rey[8] ) { // This doesn't work bcos only booleans are booleans in Java // null doesn't evaluate to false <>0 doesn't evaluate to true } if ( Mee[2] != null ) { System.out.println( "Mee Too!!" ... |
Hi there. I'm trying to do a few simple things with arrays of integers, but I'm having a bit of trouble. First, I'd like to write some code which returns true if two adjacent array elements have the same value, and it returns false otherwise. So as soon as it finds two matching entries (if they exist), it should return true. ... |
I'm having one hell of a time with this problem using arrays. I realize this is long, but if anyone could help me with it I'd be so grateful. It's an assignment for class, and I've already turned it in as is below, but I spent two hours staring into the code and it just never came to me. I *have* ... |
I need to make an array that is subscripted by the letters 'A' through 'Z'. How do I do this? From what I can tell, I need to use the ASCII code for the letters, 65 through 90. The question is how do I make an array *not* start from 0? I want the array to go from 65 to 90. ... |
|
I have the following code and I know there must be a way to simplify it, I just don't know enough about Java to know how. Is there any way that you could create an array of buttons? That way I could just use a for loop to set their values, position, and create them. Thanks in advance! import java.awt.*; import ... |
Hi, I'm stuck on how to archive this....any help would be highly appericated! Thanks in advance! Let's say I have a string array: String[] tmpPattern = {"p1","p2","p3","p4","p5","p6","p7","p8","p9", "p10", "p11"}; and a header: String tmpHeader = "Header 1"; Need to print out ----> Header 1 p1 p2 p3 Header 1 p4 p5 p6 Header 1 p7 p8 p9 Header 1 p10 p11 ... |
I have a following question. Suppose that I initialized an array like this: Object[] params = { new String (ProductName), new String(SupplierName) }; Suppose now I want the same type of array but with different values, though I cannot reuse it like this later on: Object[] params = { new String(SupplierId) }; Looks like now I need to either initialize the ... |
All, I am new to Java and I need to write a program which utilizes a class named video w/ two variables and a applications class named videostore which will sort 5 movies by title and corresponding Ratings. I have so far been able to get the movies sorted by title and displayed in an unsorted state and in alphabetical order. ... |
|
|
Hi, I am currently working on a project which is too create a FreeCell solitaire game. I am working on a class Pile (which is just a pile of cards). It involves making an array and implementing a few methods. This is my code so far public class Pile { private Deck deck; private Card[] pile; private int numCards; private static ... |
Hi, I'm still having problems coding for a multi dimensional array with user input. I have this code for the input: for(int i = 0; i < coffeeArray2.length; i ++) { for(int j = 0; j < coffeeArray2[i].length; j ++) System.out.print("Enter number of lbs of coffee for the first aisle:"); coffeeArray2[i][j] = myInput.nextInt(); But this is only getting me one column ... |
Hi experts, i am playing a little bit around with arrays an I am stuck on this (my intention was to create some code as a framework for sorting experiments): class MyNumbers { double a[] = new double[100]; void fillArray () { for (int i = 0; i < 100; i++) a[i] = Math.random(); } void showMe() { System.out.println(a[1]); } } ... |
What kind of array are you using? If you have declared an array of ints (int[]), then you CANNOT use null as a value. The null value is only valid for object references. I think this is why you are confused. There is no way to "empty" an array of primitive values (such as int, char, etc.). Layne |
Hi all, I was wondering if someone could help. First let me start by saying that this problem is for a class. I have looked through some past posts but I am not sure I am looking for the right thing. The problem is I need to make a code that will swap the first and last characters in an array. ... |
Hi, please look at the code below:- --------------------------------- public class a { // comparing 2 arrays public static void main (String args[]) { int array1 [] = {1,2}; // array1 int array2 [] = {1,2}; // array2 if (array1 == array2) System.out.println("same"); } // ~main() } // ~class --------------------------------- 1. Why doesn't it print same? 2. Also, how do I compare ... |
|
|
First, I'm reading through the Head First book to teach myself Java. There's my disclaimer. Ok, I think I understand arrays as the book explains them (and as a friend once told me). Java treats an array as an object, that holds values in an indexed manner so it's easily referenced? Now, an array of objects would only hold the references ... |
|
I have a program which responds to button presses with a series of sounds and associated pictures, each with it's own pause duration. The problem was that no pictures would be displayed until the code exited the button's action listener thread. As such someone suggested creating an animator control - basically a label with a timer and an associated array of ... |
Note that the first style is only possible if you're declaring and initializing the array in the same statement. This here does not work: int[] info; info = {2000, 100, 40, 60}; The reason is that Java's designers wanted to ensure that it's very clear what type the array has. If the type "int[]" appears on the same line as the ... |
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - ... |