Increase Size Of Array But Getting Null Values

Dec 3, 2014

I'm working on a program to create a blackjack game using objects (one for card, deck. and hand). Withing my hand object I am trying to add cards to the hand but it is only adding the last card i try to add and giving null values for the the ones before.

class BlackJackHand {
private BlackJackCard [] hand;
public void addToHand(BlackJackCard c) {
if (hand == null) {
BlackJackCard [] tempHand = new BlackJackCard[1];
tempHand[0] = c;
hand = tempHand;

[Code] ....

What I want this section to do is add cards to the current hand. I was intending for it the hand to be null at first and the if(hand == null) piece to add the card the first time and then the else piece would be used when the hand already has at leas one card. I want the else section to create a temporary array that is one larger than my current hand, copy the cards from the old hand to the new hand, and then add a new card to the last space before rewriting the old hand as what the temporary hand is.

The code I am using to test if the addToHand() is working is

class BlackJackTest
{
public static void main (String[]args) {
BlackJackCard c1= new BlackJackCard(1,0);
BlackJackCard c2= new BlackJackCard(1,4);
BlackJackCard c3= new BlackJackCard(1,5);
BlackJackHand h1 = new BlackJackHand();

[Code] .....

BlackJackCard has the parameters (int suit, int value)

This should print:
ace of clubs
4 of clubs
5 of clubs

but instead prints:
null
null
5 of clubs

View Replies


ADVERTISEMENT

How To Increase The Heap Size More Than 1024m

Sep 29, 2014

When I try to write the .xlsx file using apache POI,  XSSFWorkbook API and run this program in Eclipse STS, I am getting the java.lang.OutOfMemoryError: Java heap space error. Then I searched the net and add these -Xms512m -Xmx1024m in the eclipse VM arguments. Even though I am getting the same error. Again i increase heap size but i am getting the different error like "occurred during initialization of VM, Could not reserve enough space for object heap". how to increase the heap size or any other API to read, delete and write the .xlsx file. I am having 4GB ram in my machine. Apache POI is very good for .xls but if it is .xlsx performance wise it is very slow.

View Replies View Related

Better Way To Remove Null Values From Array

Dec 1, 2014

Is there a better way to remove null values from an array than what I have tried? This works just fine, but I just get the feeling that there is a better way to do this, without using the JCF.

private static String[] removeNullValues(String[] list){
int count = 0;
for(int i = 0; i < list.length; i++){
if(list[i] == null)
count++;

[Code] ....

I technically dont need to remove the null values for the project that I'm working on (since I just print it out and I can avoid null values with a simple statement like

if(update[i] != null) SOP(update[i])
),

but I'm just curious.

View Replies View Related

Replacing Null Values Within Multidimensional Array

Feb 18, 2014

I have a file which contains certain positions ([a][b]) that require to be placed in a certain multi-dimensional array. For example I might have an array String[][] that is a size of 6x6 but only have values in positions [2][1] and [3][2]. Because it is important for me to maintain the given array size and also do certain actions with the given positions I cannot modify the size. In addition I need to count the surrounding neighbors each element has (including elements that are null). However because some of my further code cant process with null elements I need to remove all null elements with " " (blank).

I am not sure how this is done or if it's even possible. If it is not possible how can I do something as close as possible to my needs?

View Replies View Related

Java Array Update Without Using ArrayList - Add Values And Update Size?

Feb 9, 2015

I am trying to create an array list without using the built in arrayList class that java has. I need to start the array being empty with a length of 5. I then add objects to the array. I want to check if the array is full and if not add a value to the next empty space. Once the array is full I then need to create a second array that starts empty and with the length of the first array + 1.

The contents of the first array are then copied into the new array without using the built in copy classes java has and then the new array ends up containing 5 elements and one null space the first time round. The first array is then overwritten with the new array containing the 5 elements and the null space. The process then starts again adding values until the array is full which will be 1 more value then recreating the second array empty and the length of the first array + 1. This is my attempt at creating this but I have run into multiple problems where I get only the last value I added and the rest of the values are null:

public class MyArrayList {
public Object arrayList[];
public MyArrayList(Object[] arrayList) {
this.arrayList = arrayList;

[code]...

View Replies View Related

Create Circular Array Which Prints Out 8 Numbers That Increase By One And Don't Exceed 9

Jan 31, 2015

I'm trying to create a circular array which prints out 8 numbers that increase by one and don't exceed 9. If they do, the remaining numbers are printed from 0 on-wards. I have code below which does this job, but it doesn't really use an array to loop back.

Java Code:

package Practice;

public class Practice
{
public static void main(String[] test)
{
number(7);
number(9);

[code]...

View Replies View Related

JSP :: How To Print Null Values In EL

Dec 18, 2014

The value is: ${object1.property1} If the value of the property is null, I want "null" to be printed, like this:

The value is: null But I get an empty string, like: The value is:

I know I can use a ternary operator, but isn't there a simpler/shorter solution? A function (like NVL() in SQL)?

The value is: ${empty object1.property1 ? "null" : object1.property1 }

Not only is this long, but the value expression is typed twice, which is a magnet for bugs.

View Replies View Related

Compact Test For Any Of 4 Values Being Null

Apr 7, 2014

Any compact if statement or any type of test for checking if atleast ONE out of 4 integers is null. So 1 of all, 2 of all, 3 of all, or all of them being null will give a specific result ELSE do something else.

View Replies View Related

Objects Keep Printing Null And Zero Values?

Nov 3, 2014

Everything is written up and looks good and i get no compile errors but every student object other than the default constructor has null and zero values when printed.
 
import java.text.DecimalFormat;
import java.lang.Math;
public class Student
{
//instantiate variables
 
[Code] .....

View Replies View Related

String Comparison When Both Values Are Null

Aug 5, 2014

In Java is there  a method which  I can use to compare two Strings where in if both the values are null, they should be considered as equal?

View Replies View Related

JSF :: Getting Null Values From XHTML Page To Java Code

Nov 13, 2014

My project consists of a web app where a user can select a area from a picture and f.e. if it is a office layout he can input the worker name and any peace of hardware that the area might have. In this case there are two categories: Hardware and Computer. Hardware - it has 5 dropdown lists consisting of printer, scanner and etc. Computer - like Hardware consists of many dropdown lists which add up to components such as processor, motherboard and etc. For me, considering this is my first ever web app project, is a huge step towards web development, I have used various mixes of Java, javascript and primefaces code.

My current problem: When a user selects an area he gets a dialog box where he is prompted to select his desired input, afterwards the user clicks the 'save' button and get's another dialogbox which has a resume of what he has selected so he could check out his input and save it by clicking the 'save' button in the resume box. My problem is that when the user clicks the save button the button calls a method which takes all the input and creates an Area object (Area object consists of various objects such as: Coordinates, Dimensions, Employee, ComputerList and HardwareList) and sends a query to the database, but all the values I get is null.

XHTML code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui">

[Code] ....

View Replies View Related

Create 2D Array Out Of CSV File And Find Number Of Elements To Determine Array Size

Mar 24, 2015

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];

[Code] ....

View Replies View Related

How To Get Size Of Array From Textfile

Feb 15, 2015

I have the textfile which has lines:

A B 2 midterm 100

C D 1 final 90

C D 1 midterm 90

A B 2 final 80

**NO ARRAYLIST IS ALLOWED!** And the textfile is passed into the method. How to get the size for the array non-randomly inside the method from the passed Scanner file?? What if you have lots of numbers of lines, so how could that be done?

I have doubts about this line Exam[] object = new Exam[12];

Java Code:

public static Exam[] readAllExams(Scanner s) throws ArrayIndexOutOfBoundsException
{
String firstName = "";
String lastName = "";

[Code].....

View Replies View Related

Increasing Array Size Automatically

Dec 8, 2014

The array size is fixed improve it to automatically increase the array size by creating a new larger array and copying the contents of the current array into it .I am using a course class and here is the code for the class

public class Course {
private String courseName;
private int numberOfStudents;
private String[] students = new String[100];
public Course(String courseName)

[Code] ....

As you can see I have the array set to size 100, how do i make so it increments each time the user adds a student.

View Replies View Related

Multidimensional Array With Size Only In The First Square

Jan 14, 2014

I come to the point: I just started to learn java through various manuals and in one of them I came across a declaration of an array that I do not understand:

int[][] multiArr = new int[2][];

the manual says that you can allocate the multidimensional array multiArr by defining size in only the first square bracket but I can't undestand how you can use this array. Seems to be no way to store data with it!

View Replies View Related

Automatically Increasing Array Size?

Feb 5, 2014

I'm working on an assignment that says the following.

" The array size is fixed in Listing 10.6. Improve it to automatically increase the array size by creating a new larger array and copying the contents of the current array to it.Implement the dropStudent method.Add a new method named clear() that removes all students from the course.

Write a test program that creates a course, adds three students, removes one, and displays the students in the course."

10.6 Listing

public class Course {
private String courseName;
private String[] students = new String[100];
private int numberOfStudents;
 } 
public Course(String courseName) {
this.courseName= courseName;

[Code]...

My Test Code based off of book

public static void main(String[] args) {
Course course1= new Course("Business");
course1.addStudent("Jay");
course1.addStudent("Silent Bob");
course1.addStudent("Dante");
course1.dropStudent("Jay");
 
[Code]....

My adjusted 10.6

public class Course {
private String courseName;
private String[] students = new String[100];
private int numberOfStudents;
 } 
public Course(String courseName) {
this.courseName= courseName;

[Code]...

The problem I'm having is, for the first part of the question where I need to automatically increase the array size. I'm really not great at this stuff. I have tried breaking it down, but can't "get it", I guess.

I assume, it'd be a loop that checks to see if the student array is full and if so, do the increaseArray() part, by maybe multiplying the student array and then assigning it. I just don't know how to do it haha.

My *best* attempt at the loop so far has been

if (students == students.length){
int bigArray = 2*students.length;
String increaseArray()= new String[students];
System.arraycopy(students, 0, increaseArray, 0, students.length);
students= increaseArray;

but,yeah... Doesn't seem to be right.

View Replies View Related

User Input For Array Size

Oct 28, 2014

I am having trouble with an assignment. I need the user to input the size of the array and print when asked. In my program, it prints 100 numbers instead of the user input number, such as 15.

import java.util.Arrays;
import java.util.Scanner;
import java.util.Random;
public class Lab9 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int [] values = new int [100];

[Code] .....

View Replies View Related

Increasing / Decreasing Size Of Array

Jul 11, 2014

I am trying to make a code that copies the users String to a char array. However, I am in a predicament: since I would not know the exact size of the users String I am left with the options of either making my array large by default, filled in with, lets say 25, empty spaces per index OR starting out with a default size of 1, with an empty space, and then some how increase the size from there.

At this moment I am leaning on the first option, creating a large default array and then deleting the unused values. However, this brings me to my actual question: if I set the non used indexes to null, if that wont give me an error, would that change the size of my array?

Ex:
//lets say i finally copied all of the values and this is the result
char[] word = {'b', 'o', 'b', ' ', ' '};
for(int i = word.length(); i > 0; i--){
if(word[i] == ' ')//delete the value so the size decreases
word[i] = null;//if possible
}

View Replies View Related

Array Is Not The Right Size - Causing Errors

Oct 17, 2014

protected void randomise() {
int[] copy = new int[];
// used to indicate if elements have been used
boolean[] used = new boolean[array().length];
Arrays.fill(used,false);
for (int index = 0; index < array().length {

[Code] .....

View Replies View Related

Pass Predefined Size Array As Argument

Jan 9, 2014

Is this possible in Java?

public void doSomething(int[4] year){
}

What I am trying to do is to get the person who is using the method to send a year in the format 1998 and so on.

What would be the best way to accomplish this?

View Replies View Related

Field Size In Array List Class

Jan 29, 2015

I'm just putting together a little 'horse racing' program while I'm learning Java, and I have a class called Race that creates an array list of thorougbred horses called field, and asks the user to enter then names of the each horse in the field, with a maximum of 14 horses. The problem occurs with the current code that I have:

import java.util.*;
public class Race {
RaceHelper helper = new RaceHelper();
ArrayList<thoroughbred> field = new ArrayList<thoroughbred>();
public void setField() { //enter the horses in the race and determine the size of the field

[code]....

the statement of the index position and current size was for me, so I could see what was going on.What I don't understand is the while loop. not that the class doesn't compile and run (you can see that it does), it's the output. Why does the <= sign allow one more entry and increase the size of the field to 15?

Less than or equal to 14 should give a maximum field size of 14, right, With the starting object at index position at zero and going up to 13, for a total size of 14 thoroughbred objects if I just use while (field.size()<14) or a for loop, then the output is fine; it allows a max of 14 entries and prints the results. I thought it had something to do with the size being zero based, but that doesn't seem to matter -- unless it does matter and I'm missing it. why the comparison I'm using produces this output? a field of 14 horses shouldn't matter whether it's zero or 1 based, as long as the size of the field is 14, so why the extra entry with this while condition?

View Replies View Related

Write A Program To Create Integer Array Of Size 20

Aug 2, 2014

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.

A sample run of the program is shown below:

Sample output #1:
Array:1 2 2 3 1 5 4 2 3 4 4 2 1 1 3 2 1 4 3 2 1
Random position: 5
Final Array:1 2 0 0 0 5 0 0 0 4 4 2 1 1 3 2 1 4 3 2 1

View Replies View Related

Program To Initialize And Display Variable Size Array

Aug 18, 2014

Write a program to initialize and display variable size array.

View Replies View Related

What Is The Length Of A Null Array Object

Feb 21, 2014

I tried the following and it gives me null pointer exception. I just want to verify if what is the actual value of a length of an array?

Object [] obj = null;
System.out.println("length:" + obj.length);

View Replies View Related

Check If Array Elements Are Null?

Jan 6, 2015

B is an array Object, as you can see if an element is null or not?

View Replies View Related

Method Is Not Returning Array - Null Error

May 18, 2015

I am trying to return an array and I keep getting a null error. The below class sets the material numbers into an array and should return that array if called :

public class Jobs {
private int[] materialsNumber;
//change to parts and create another class that gets the materials for the parts
 public int[] job1() {
materialsNumber[0] = 11960120;

[Code] ....

I later try to call the method. The program executes but stops after I println "test in loop"

public class PurchaseOrdersToParts {
 private Jobs job = new Jobs();
 int[] getPartsForPurchaseOrder(BigDecimal purchaseOrder) {
 System.out.println("inside getparts");
 BigDecimal testNum = new BigDecimal(123.0);
 
[Code] ....

This is the method that is calling the method in the GenerateOrdersToParts class

private PurchaseOrdersToParts purchaseOrdersToParts = new PurchaseOrdersToParts();
 @Inject
PoRepository poRepository;
public GenerateShopJobTickets() {
 
[Code] .....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved