The infamous object bug - java

I discovered what i think is a bug whilst using netbeans. When i call up my method to sort an array containing names(ob.sort) in alphabetical order it automatically sorts another array which contains the original names when it isn't supposed to as the original names is not assigned to anything after it has been populated with input at the beginning(ob.input).
I experienced this problem whilst writing larger programs(encountered more than once), but i made a simpler one to demonstrate this problem. It looks like much as i copied the class methods an pasted it below the main class making it easier for you to trace the variables in the program.
public static void main(String args[]){
ObjectTest ob = new ObjectTest();
ob.input();
String x[] = ob.getNames();
System.out.println(x[0]);
ob = new ObjectTest(x);
System.out.println(x[0]);
ob.sort();
System.out.println(x[0]);
String y[] = ob.getNamesrt();
System.out.println(x[0]);
}
}
/*import java.io.*;
import javax.swing.*;
public class ObjectTest {
String name[];
String namesrt[];
public ObjectTest(){
name = new String[3];
namesrt = new String[3];
}
public ObjectTest(String j[]){
namesrt = j;
}
public void input(){
for(int i = 0; i < name.length; i++){
name[i] = JOptionPane.showInputDialog("Enter name");
}
}
public void sort(){
if(!(namesrt == null)){
for(int i = 0; i < namesrt.length; i++){
for(int c = i + 1; c < namesrt.length; c++){
if(namesrt[i].compareToIgnoreCase(namesrt[c]) > 0){
String n = namesrt[i];
namesrt[i] = namesrt[c];
namesrt[c] = n;
}
}
}
}
else{JOptionPane.showMessageDialog(null,"Names not received");}
}
public String[] getNames(){
return name;
}
public String[] getNamesrt(){
return namesrt;
}
public void setNames(String j[]){
name = j;
}
public void setNamesrt(String j[]){
namesrt = j;
}
}*/

I discovered what i think is a bug whilst using netbeans.
Well, it may be a bug in your code. It's not a bug in Java or in Netbeans. It's just demonstrating the fact that arrays are reference types in Java, and the way that objects work.
Here's a short but complete program demonstrating the same effect:
public class Test {
public static void main(String[] args) {
String[] x = { "hello" };
// Copy the *reference*
String[] y = x;
System.out.println(y[0]); // Prints "hello"
x[0] = "new value";
System.out.println(y[0]); // Prints "new value"
}
}
The values of x and y here are references to the same array object... so if the array is changed "through" x, that change is still visible as y[0].
If you want to make your code create independent objects, you'll want to change this:
public ObjectTest(String j[]){
namesrt = j;
}
to:
public ObjectTest(String j[]){
namesrt = j.clone();
}
(Ideally change it to declare the parameter as String[] j, or better yet fix all your variable names to be more meaningful, but that's a different matter.)

Related

Array Method issue

having a problem with my java program. I am a newbie to Java and just can't figure out what is exactly the issue with it. In short I've declared an array and a variable in main, I've created my method call and would like my array be passed into my method with the variable. I would then like the method to take my array and count the number of times my variable "8" occurs, get rid of the 8 out of the array and return a new smaller array back to main. Here is my code below. I feel as if I am just missing one block code any suggestions?
public class Harrison7b
{
public static void main(String [] args)
{
int[] arrayA = {2,4,8,19,32,17,17,18,25,17,8,3,4,8};
int varB = 8;
// Call with the array and variable you need to find.
int[] result = newSmallerArray(arrayA, varB);
for(int x = 0; x < arrayA.length; x++)
{
System.out.print(arrayA[x] + " ");
}
}
public static int[] newSmallerArray( int[] arrayA, int varB)
{
int count = 0;
for(int x = 0; x < arrayA.length; x++)
{
if(arrayA[x] == varB)
{
count++;
}
}
int [] arrayX = new int[arrayA.length - count];
for(int B = 0; B < arrayA.length; B++)
{
if(arrayA[B] != varB)
{
}
}
return arrayX;
}
}
you do not actually need to return the array because when you pass an array to a method you also pass its memory address meaning its the same address that you change so, it will also change the arraysA of main method because you are just changing the values of the same memory adress
import java.util.*;
public class Help
{
public static void main(String[] args)
{
ArrayList<Integer> arraysA = new ArrayList<Integer>();
arraysA.add(Integer.valueOf(2));
arraysA.add(Integer.valueOf(4));
arraysA.add(Integer.valueOf(8));
arraysA.add(Integer.valueOf(19));
arraysA.add(Integer.valueOf(32));
arraysA.add(Integer.valueOf(17));
arraysA.add(Integer.valueOf(17));
arraysA.add(Integer.valueOf(18));
arraysA.add(Integer.valueOf(25));
arraysA.add(Integer.valueOf(17));
arraysA.add(Integer.valueOf(8));
arraysA.add(Integer.valueOf(3));
arraysA.add(Integer.valueOf(4));
arraysA.add(Integer.valueOf(8));
int varB=8;
newSmallerArray(arraysA,varB);
for(Integer i:arraysA)
{
System.out.println(i);
}
}
public static void newSmallerArray(ArrayList<Integer> arraysA,int varB)
{
for(int i=0;i<arraysA.size();++i)
{
if(Integer.valueOf(arraysA.get(i))==varB)
{
arraysA.remove(i);
}
}
}
}
Try this code it will not require for loop:
List<Integer> list = new ArrayList<Integer>(Arrays.asList(arrayA));
list.removeAll(Arrays.asList(8));
arrayA = list.toArray(array);

Increased cohesion in simple java program

I have a small java program that collects 10 words written by a user and prints them in specified orders. As it stands, the program works, but it is not cohesive.
My issue stems from not knowing enough about the concept of cohesion to work on fixing this, as well as being new to Java/OO languages.
I believe that the class Entry is way way way too cluttered, and that another class should take on some of this class' functions.
Any hint or clue, cryptic or otherwise would be greatly appreciated!
The lack of a input reader in Dialogue.java is intentional, as the original code uses proprietary code.
These are the three classes: entry, dialogue and printer.
Entry.java
public class Entry {
public static void main(String[] args){
String[] wordArray = new String[10];
Dialogue d = new Dialogue();
wordArray = d.read(wordArray);
Printer p = new Printer();
p.printForwards(wordArray);
p.printBackwards(wordArray);
p.printEveryOther(wordArray);
}
}
Dialogue.java
public class Dialogue {
public String[] read(String[] s){
String[] temp;
temp = new String[s.length];
for(int i=0;i<s.length;i++){
String str = anything that reads input("Enter word number" + " " + (i+1));
temp[i] = str;
}
return temp;
}
}
Printer.java
public class Printer {
public void printForwards(String[] s){
System.out.println("Forwards:");
for(int i=0;i<s.length;i++){
System.out.print(s[i] + " ");
if(i==s.length-1){
System.out.println("");
}
}
}
public void printBackwards(String[] s){
System.out.println("Backwards:");
for(int i=s.length-1;i>=0;i--){
System.out.print(s[i]+ " ");
if(i==0){
System.out.println("");
}
}
}
public void printEveryOther(String[] s){
System.out.println("Every other:");
for(int i = 0; i < s.length; i++){
if(i % 2 == 0){
System.out.print(s[i] + " ");
}
}
}
}// /class
It looks okay overall, the truth is it is a very simple task where as OOP is better suited for more complex programs. That being said, here are a few pointers/examples.
You can also do your printing more OOP style.
The purpose of this is build reusable, modular code. We do this by abstracting String array manipulations (which previously existed in the Printer class) to it's own class.
This is also very similar/also known as loose-coupling. We achieve loose-coupling by splitting the string processing functionality and the printing functionality.
Change you Printer class to StringOrderer or something along those lines:
public class StringOrderer {
private String[] array;
public class StringOrderer(String[] array) {
this.array = array;
}
public String[] getArray() {
return array;
}
public String[] everyOther(){
String[] eos = new String[array.length];
for(int i = 0; i < s.length; i++){
if(i % 2 == 0){
eos[eos.length] = s[i];
}
return eos;
}
public String[] backwards() {
...
And then in your main class add a method like such:
private static void printStringArray(String[] array) {
for (int i=0; i<array.length; i++) {
System.out.print(array[i]);
}
}
Then call it in your main method:
StringOrderer s = new StringOrderer(wordArray);
System.out.println('Forward:');
printStringArray(s.getArray());
System.out.println('Every other:');
printStringArray(s.everyOther());
System.out.println('Backwards:');
...
Extra tip - You can also add methods in your main class like so:
public class Entry {
public static void main(String[] args){
String[] wordArray = readWordArray()
Printer p = new Printer();
p.printForwards(wordArray);
p.printBackwards(wordArray);
p.printEveryOther(wordArray);
}
private static String[] readWordArray() {
Dialogue d = new Dialogue();
return d.read(new String[10]);
}
}
to make it more readable.

How to read copy the array with the array from another class? [In loading file]

I want to create a game and I need to read file from the notepad
when I use my loadfile.java alone, it work very well. Then, I would like to copy my data into datafile.java as it will be easier for me to do the fighting scene. However, I can't copy the array in my loadfile.java to the datafile.java and I don't understand why.
import javax.swing.*;
import java.io.*;
import java.util.Scanner;
public class loadfile
{
static String filename = "Save.txt";
static int size = 4;
static int s;
static int[] number;
static String[] line;
private static void load() throws IOException
{
BufferedReader reader = new BufferedReader(new FileReader(filename));
while (reader.readLine()!= null)
{
size++;
}
size -= 4;
reader.close();
line = new String[size];
number = new int[size];
BufferedReader reader2 = new BufferedReader(new FileReader(filename));
for (int i = 0; i < size; i++)
{
line[i] = reader2.readLine();
}
reader2.close();
for (int i = 4; i < size; i++)
{
number[i] = Integer.parseInt(line[i]);
}
}
public static String[] getData()
{
return line;
}
public static int[] getNumber()
{
s = size - 4;
int[] num = new int[s];
for (int i = 0; i < s; i++)
{
num[i] = number[i+4];
}
return num;
}
public static int getDataSize()
{
return size;
}
public static int getNumberSize()
{
return size - 4;
}
This is my loadfile.java
I use the file with 4 names and 9 * n int in the notepad as I want to check whether I have the character first before I read the file. However, before I can handle this problem, I got another problem that I can't copy the array into my datafile.java
The datafile.java is separate with two constructor. One is for Starting the game and one is for loading the data. The constructor with the (int num) is the problem I have. First, I would like to show the java first:
import java.util.Arrays;
import java.io.*;
public class datafile
{
private static String[] data;
private static int[] number;
private static String[] name;
private static int[] a, d, s;
private static int[] hp, maxhp;
private static int[] mp, maxmp;
private static int[] lv, exp;
public datafile()
{
initialization();
name[0] = "Pet";
a[0] = 100;
d[0] = 100;
s[0] = 100;
hp[0] = 500;
mp[0] = 500;
maxhp[0] = 500;
maxmp[0] = 500;
exp[0] = 100;
lv[0] = 1;
}
public datafile(int num) throws IOException
{
initialization();
loadfile l = new loadfile();
for (int i = 0; i < l.getNumberSize(); i++)
{
number[i] = l.getNumber()[i];
}
for (int i = 0; i < l.getDataSize(); i++)
{
data[i] = l.getData()[i];
}
for(int i = 0; i < 4; i++)
{
name[i] = data[i];
}
for(int i = 0; i < 4; i++)
{
a[i] = number[1+(i*9)];
d[i] = number[2+(i*9)];
s[i] = number[3+(i*9)];
hp[i] = number[4+(i*9)];
mp[i] = number[5+(i*9)];
maxhp[i] = number[6+(i*9)];
maxmp[i] = number[7+(i*9)];
lv[i] = number[8+(i*9)];
exp[i] = number[9+(i*9)];
}
}
public static String getName(int n)
{
return name[n];
}
public static int getAttack(int n)
{
return a[n];
}
public static int getDefense(int n)
{
return d[n];
}
public void initialization()
{
name = new String[3];
a = new int[3];
d = new int[3];
s = new int[3];
hp = new int[3];
mp = new int[3];
maxhp = new int[3];
maxmp = new int[3];
lv = new int[3];
exp = new int[3];
}
public static void main (String[] args) throws IOException
{
new datafile(1);
}
}
When I run the program, the debugging state this line
data[i] = l.getData()[i];
as an error
I don't know what wrong with this line and I tried so many different ways to change the way the copy the method. However, it didn't work
The error says this:
Exception in thread "main" java.lang.NullPointerException
at datafile.<init>(datafile.java:38)
at datafile.main(datafile.java:92)
I hope you guys can help me with this problem because I don't want to fail with my first work
in your datafile(int num)
you call
loadfile l = new loadfile();
but you never call the load() method on you loadfile
l.load();
Edit: my bad, I didn't see your initialization method, but regardless, I'm going to stick with my recommendation that you radically change your program design. Your code consists of a kludge -- you've got many strangely named static array variables as some kind of data repository, and this suggests that injecting a little object-oriented design could go a long way towards creating classes that are much easier to debug, maintain and enhance:
First I recommend that you get rid of all of the parallel arrays and instead create a class, or likely classes, to hold the fields that need to be bound together and create an ArrayList of items of this class.
For example
public class Creature {
private String name;
private int attack;
private int defense;
// constructors here
// getters and setters...
}
And elsewhere:
private List<Creature> creatureList = new ArrayList<>();
Note that the Creature class, the repository for some of your data, should not be calling or even have knowledge of the code that loads the data, but rather it should be the other way around. The class that loads data should create MyData objects that can then be placed within the myDataList ArrayList via its add(...) method.
As a side recommendation, to help us now and to help yourself in the future, please edit your code and change your variable names to conform with Java naming conventions: class names all start with an upper-case letter and method/variable names with a lower-case letter.

Method to get the sum of an ArrayList

I am getting the totals of various String ArrayLists such as [1,3,4]...
by parsing them into integers and getting the total. This worked when I coded each individual one, but when I made a method by passing in the total int value and arraylist I always get a value of zero.
A method would save a lot of time.
public class Playing {
static ArrayList<String> list;
static int Vigor;
public static void main(String[] args) {
list = new ArrayList<String>();
vigoroustotal(list,Vigor);
public static void Listtotal(String par, int tt) {
for (String s : par) {
int i = Integer.parseInt(s);
tt += i;
}
}
Any changes you do to tt inside your method won't be visible anywhere else, because Java passes everything by value. Make the method return an int instead.
Your mistake her is passing the function tt and expecting it to be modified. Java doesn't modify parameters passed to functions. The corrected code would be this:
public static int ListTotal(List<String> par) {
int tt = 0;
for (String s : par) {
int i = Integer.parseInt(s);
tt += i;
}
return tt;
}
and would be used like this:
Vigor = ListTotal(list);
The changes that are made to the integer tt in the Listtotal() method will not be visible anywhere else but that method. You can make that method return an integer to solve that!
public static int Listtotal(ArrayList<String> par) {
int tt = 0;
for (String s : par) {
int i = Integer.parseInt(s);
tt += i;
}
return tt;
}
And then you need to change the main method:
public static void main(String[] args) {
list = new ArrayList<String>();
Vigor = Listtotal(list);
}
As, said, you need to return the total since java passes everything by-value, so the int tt you pass in won't hold a reference to the Vigor variable outside the method.
Therefore, when you pass in primitive types such as (int, char, boolean, byte etc.), anything you do to them inside a method won't be visible outside the method.
However, when you pass in a reference type (Objects such as ArrayList), it is still passed-by-value but that value is a copy of the reference to the Object outside the method. So, in the populateList method bellow, I can just call ArrayList.add() on the input because this input, even though it is passed-by-value, still points to the original Object that was put into this method.
public class Playing {
static ArrayList<String> list;
static int Vigor;
public static void main(String[] args) {
list = new ArrayList<String>();
populateList(list);
Vigor = getListTotal(list);
System.out.println("Total is:\t" + Vigor);
}
public static void populateList(ArrayList<String> list) {
String[] sampleData = { "4", "7", "2" };
for(int i = 0; i < sampleData.length; i++) {
list.add(sampleData[i]);
}
}
public static int getListTotal(ArrayList<String> list) {
int tt = 0;
for (String s : list) {
int i = Integer.parseInt(s);
tt += i;
}
return tt;
}
}
What you want is the... let's call it CountingList,
public class CountingList {
private List<Integer> integers = new ArrayList<Integer>();
private int sum;
public add(String s) {
int value = Integer.parseInt(s);
integers.add(value);
sum += value;
}
private void updateSum() {
sum = 0;
for (int i : integers) {
sum += i;
}
}
}
Obviously, you'll want to expose the functionality you need to use outside of the class, but this is (one) way of encapsulating the behavior you're needing.

Null pointer exception for Array of Objects

I am new to using arrays of objects but can't figure out what I am doing wrong and why I keep getting a Null pointer exception. I am trying to create an Theatre class with an array of spotlight objects that are either set to on or off. But - whenever I call on this array I get a null pointer exception.
package theatreLights;
public class TheatreSpotlightApp {
public static void main(String[] args) {
Theatre theTheatre = new Theatre(8);
System.out.println("element 5 " + theTheatre.arrayOfSpotlights[5].toString());
}
}
package theatreLights;
public class Theatre {
spotlight[] arrayOfSpotlights;
public Theatre(int N){
arrayOfSpotlights = new spotlight[N];
for (int i = 0; i < arrayOfSpotlights.length; i++) {
arrayOfSpotlights[i].turnOn();
}
}
}
package theatreLights;
public class spotlight {
int state;
public spotlight(){
state = 0;
}
public void turnOn(){
state = 1;
}
void turnOff(){
state = 0;
}
public String toString(){
String stringState = "";
if(state == 0){
stringState = "is off";
}
else if(state==1){
stringState = "is on";
}
return stringState;
}
}
I must be doing something basic wrong in creating the array but can't figure it out.
replace
arrayOfSpotlights[i].turnOn();
with
arrayOfSpotLights[i] = new Spotlight();
arrayOfSpotlights[i].turnOn();
The line
arrayOfSpotlights = new spotlight[N];
will create an array of spotlights. It will however not populate this array with spotlights.
When you do "arrayOfSpotlights = new spotlight[N];" you init an array of length N, what you need to do is also init each object in it:
for i=0; i<N; i++
arrayOfSpotlights[i] = new spotlight();
arrayOfSpotlights[i].turnOn();
Hope I'm correct :)
You are not creating an spotlight objects.
arrayOfSpotlights = new spotlight[N];
This just creates an array of references to spotlights, not the objects which are referenced.
The simple solution is
for (int i = 0; i < arrayOfSpotlights.length; i++) {
arrayOfSpotlights[i] = new spotlight();
arrayOfSpotlights[i].turnOn();
}
BTW You should use TitleCase for class names.
You could write your class like this, without using cryptic code like 0 and 1
public class Spotlight {
private String state;
public Spotlight() {
turnOff();
}
public void turnOn() {
state = "on";
}
void turnOff() {
state = "off";
}
public String toString() {
return "is " + state;
}
}
You declared the array arrayOfSpotlights, but didn't initialize the members of the array (so they are null - and you get the exception).
Change it to:
public class Theatre {
spotlight[] arrayOfSpotlights;
public Theatre(int N){
arrayOfSpotlights = new spotlight[N];
for (int i = 0; i < arrayOfSpotlights.length; i++) {
arrayOfSpotlights[i]=new spotlight();
arrayOfSpotlights[i].turnOn();
}
}
}
and it should work.

Categories