I have a class called 'Items' to which 'Equips' extends from and 'Helmet' then extends from 'Equips'. I have a method called 'getStats' that loads the item's stats from a .txt file. If I put the 'getStats' method in the 'Items' class, whatever field I try to access in a 'Helmet' object using 'this.' shows up null. The field I'm trying to access in 'Helmet' is initialized when the helmet is created before the text file is loaded. I could very easily just put the 'getStats' method in the 'Equips' class and put a blank 'getStats' method in the 'Items' class, but I was wondering if there was a way to make it work how it is. Thanks in advance!
Items.java:
package com.projects.aoa;
import static com.projects.aoa.Print.*;
import java.util.*;
import java.io.*;
class Items {
String name, type;
int id;
int hp, mp, str, def;
boolean vacent;
static void getAllStats(Items[] e){
for(Items i : e){
getItemStats(i);
}
}
static void getItemStats(Items i){
i.getStats();
}
void getStats(){
try {
//System.out.println(System.getProperty("user.dir"));
print(this.name); //THIS shows up as null as well as those \/below\/
FileInputStream fstream = new FileInputStream(System.getProperty("user.dir")
+ "/src/com/projects/aoa/" + this.type + this.name + ".txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
int counter = 0;
while ((line = br.readLine()) != null) {
if (line.length() == 0){
break;
}
switch (counter) {
case 0:
this.hp = Integer.parseInt(line);
counter++;
break;
case 1:
this.mp = Integer.parseInt(line);
counter++;
break;
case 2:
this.def = Integer.parseInt(line);
counter++;
break;
case 3:
this.str = Integer.parseInt(line);
counter++;
break;
}
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Game.java:
Helmet headBand = new Helmet("HeadBand");
Helmet bronzeHelmet = new Helmet("BronzeHelmet");
Items[] equips = {
headBand, bronzeHelmet
};
getAllStats(equips);
Equips.java:
package com.projects.aoa;
import static com.projects.aoa.Print.print;
import static com.projects.aoa.Print.println;
import java.io.*;
class Equips extends Items{
String name, type;
int hp, mp, str, def;
void printStats(){
println("[" + name + "]");
println("Type: " + type);
println("HP: " + hp);
println("MP: " + mp);
println("Def: " + def);
println("Str: " + str);
}
}
class Helmet extends Equips {
Helmet(String name){
this.name = name;
this.type = "h_";
}
}
You haven't shown us your Helmet class, so it's hard to say what's going on - but my guess is that you're redeclaring fields with the same name in Helmet. Those will hide the fields in Items, whereas you really just want to use the fields from Items.
So here's a short but complete example which demonstrates what I think is going on:
class SuperClass {
String name;
public void setName(String newName) {
// This writes to the field in SuperClass
name = newName;
}
}
class SubClass extends SuperClass {
// This *hides* the field in SuperClass
String name;
public void showName() {
// This reads the field from SubClass, which
// nothing writes to...
System.out.println("My name is " + name);
}
}
public class Test {
public static void main(String[] args) {
SubClass x = new SubClass();
x.setName("Test");
x.showName();
}
}
I would recommend that:
You make all fields private, writing properties to give access to other classes as required
You get rid of the fields in Helmet which hide the ones in Items
You change your class names to avoid the plurality - Item and Equipment instead of Items
Here's a fixed version of the above code:
class SuperClass {
private String name;
public void setName(String newName) {
name = newName;
}
public String getName() {
return name;
}
}
class SubClass extends SuperClass {
public void showName() {
System.out.println("My name is " + getName());
}
}
public class Test {
public static void main(String[] args) {
SubClass x = new SubClass();
x.setName("Test");
x.showName();
}
}
(Obviously you also need to think about what access to put on the properties etc, but that's a separate matter.)
Related
I have an object, Pet, and one of the functions is to retrieve its name.
public class pet{
private String petName;
private int petAge;
public pet(String name, int age){
petName = name;
petAge = age;
}
public String getName(){
return petName;
}
public int getAge(){
return petAge;
}
}
I then have an ArrayList which holds a collection of pets as shown in the code below:
import java.util.ArrayList;
pet Dog = new pet("Orio", 2);
pet Cat = new pet("Kathy", 4);
pet Lion = new pet("Usumba", 6);
ArrayList<pet> pets = new ArrayList<>();
pets.add(Dog);
pets.add(Cat);
pets.add(Lion;
I was wondering how I could retrieve the index in the ArrayList or the object that has the name I need. So if I wanted to find out how old Usumba was, how would I do this?
Note: This is not my actual piece of code, it's just used so that I can better explain my problem.
Edit 1
So far, I have the following but I was wondering if there was a better or more efficient way
public int getPetAge(String petName){
int petAge= 0;
for (pet currentPet : pets) {
if (currentPet.getName() == petName){
petAge = currentPet.getAge();
break;
}
}
return petAge;
}
You can't use indexOf() for this purpose, unless you abuse the purpose of the equals() method.
Use a for loop over an int variable that iterates from 0 to the length of the List.
Inside the loop, compare the name if the ith element, and if it's equal to you search term, you've found it.
Something like this:
int index = -1;
for (int i = 0; i < pets.length; i++) {
if (pets.get(i).getName().equals(searchName)) {
index = i;
break;
}
}
// index now holds the found index, or -1 if not found
If you just want to find the object, you don't need the index:
pet found = null;
for (pet p : pets) {
if (p.getName().equals(searchName)) {
found = p;
break;
}
}
// found is now something or null if not found
As the others already stated, you cannot use indexOf() for this directly. It would be possible in certain situations (lambdas, rewriting hashCode/equals etc), but that is usually a bad idea because it would abuse another concept.
Here's a few examples of how we can do that in modern Java:
(as the index topic has already been answered quite well, this only handles direct Object return)
package stackoverflow.filterstuff;
import java.util.ArrayList;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
public class FilterStuff {
public static void main(final String[] args) {
final Pet dog = new Pet("Orio", 2); // again, naming conventions: variable names start with lowercase letters
final Pet cat = new Pet("Kathy", 4);
final Pet lion = new Pet("Usumba", 6);
final ArrayList<Pet> pets = new ArrayList<>();
pets.add(dog);
pets.add(cat);
pets.add(lion);
try {
simpleOldLoop(pets);
} catch (final Exception e) {
e.printStackTrace(System.out);
}
try {
simpleLoopWithLambda(pets);
} catch (final Exception e) {
e.printStackTrace(System.out);
}
try {
filterStreams(pets);
} catch (final Exception e) {
e.printStackTrace(System.out);
}
try {
filterStreamsWithLambda(pets);
} catch (final Exception e) {
e.printStackTrace(System.out);
}
}
private static void simpleOldLoop(final ArrayList<Pet> pPets) {
System.out.println("\nFilterStuff.simpleOldLoop()");
System.out.println("Pet named 'Kathy': " + filterPet_simpleOldLoop(pPets, "Kathy"));
System.out.println("Pet named 'Hans': " + filterPet_simpleOldLoop(pPets, "Hans"));
}
private static Pet filterPet_simpleOldLoop(final ArrayList<Pet> pPets, final String pName) {
if (pPets == null) return null;
for (final Pet pet : pPets) {
if (pet == null) continue;
if (Objects.equals(pet.getName(), pName)) return pet;
}
return null;
}
private static void simpleLoopWithLambda(final ArrayList<Pet> pPets) {
System.out.println("\nFilterStuff.simpleLoopWithLambda()");
System.out.println("Pet named 'Kathy': " + filterPet_simpleLoopWithLambda(pPets, (pet) -> Boolean.valueOf(Objects.equals(pet.getName(), "Kathy"))));
System.out.println("Pet named 'Hans': " + filterPet_simpleLoopWithLambda(pPets, (pet) -> Boolean.valueOf(Objects.equals(pet.getName(), "Hans"))));
}
private static Pet filterPet_simpleLoopWithLambda(final ArrayList<Pet> pPets, final Function<Pet, Boolean> pLambda) {
if (pPets == null) return null;
for (final Pet pet : pPets) {
if (pet == null) continue;
final Boolean result = pLambda.apply(pet);
if (result == Boolean.TRUE) return pet;
}
return null;
}
private static void filterStreams(final ArrayList<Pet> pPets) {
System.out.println("\nFilterStuff.filterStreams()");
System.out.println("Pet named 'Kathy': " + filterPet_filterStreams(pPets, "Kathy"));
System.out.println("Pet named 'Hans': " + filterPet_filterStreams(pPets, "Hans"));
}
private static Pet filterPet_filterStreams(final ArrayList<Pet> pPets, final String pName) {
return pPets.stream().filter(p -> Objects.equals(p.getName(), pName)).findAny().get();
}
private static void filterStreamsWithLambda(final ArrayList<Pet> pPets) {
System.out.println("\nFilterStuff.filterStreamsWithLambda()");
System.out.println("Pet named 'Kathy': " + filterPet_filterStreams(pPets, p -> Objects.equals(p.getName(), "Kathy")));
final Predicate<Pet> pdctHans = p -> Objects.equals(p.getName(), "Hans"); // we can also have 'lambda expressions' stored in variables
System.out.println("Pet named 'Hans': " + filterPet_filterStreams(pPets, pdctHans));
}
private static Pet filterPet_filterStreams(final ArrayList<Pet> pPets, final Predicate<Pet> pLambdaPredicate) {
return pPets.stream().filter(pLambdaPredicate).findAny().get();
}
}
Along with your Pet class, extended by toString():
package stackoverflow.filterstuff;
public class Pet { // please stick to naming conventions: classes start with uppercase letters!
private final String petName;
private final int petAge;
public Pet(final String name, final int age) {
petName = name;
petAge = age;
}
public String getName() {
return petName;
}
public int getAge() {
return petAge;
}
#Override public String toString() {
return "Pet [Name=" + petName + ", Age=" + petAge + "]";
}
}
I need some help please: I'm making a flight roster simulation in java. The roster will hold 25 passengers, 22 of which come from a text file (PassengerList.txt). For each passenger there are 3 required data points; name, seat class & seat # and 2 optional data points frequent flyer number & frequent flyer points. Each passenger is on its own line and each data point is separated by a comma. For example:
Allen George,Economy Class,8A,#GEO456,10000
Judy Hellman,Economy Class,8B
I have this class, along with constructor so far:
public class Passengers
{
private String name, type, seat, flyernum;
private int points;
//Constructor to intialize the instance data
Passengers(String full_name, String seat_type, String seat_number,
String frequent_flyer_number, int frequent_flyer_points)
{
name=full_name;
type=seat_type;
seat=seat_number;
flyernum=frequent_flyer_number;
points=frequent_flyer_points;
} //end Passengers
What I need to do is to read each line from the text file and create the array, i.e. make the first look line look something like this:
Passenger passenger1 = new Passenger ("Allen George","Economy Class","8A"
,"#GEO456",10000)
Into an array like this:
Passenger[0] = passenger1;
I am obviously a java beginner, but I have been caught up on this for so long and I keep getting different error message after error message when I try something new. I have been using Scanner to read the file. The text file does not need to be overwritten, just read and scanned by the program. Only Arrays can be used as well, ArrayList is a no go. Only two files too, the Passengers class and the main method. Please help! Thank you!
You can do it as bellow :
First you need a Passenger.class. It would look something like this (Note that I have added a toString():
public class Passenger {
private String name, type, seat, flyernum;
private int points;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSeat() {
return seat;
}
public void setSeat(String seat) {
this.seat = seat;
}
public void setFlyernum(String flyernum) {
this.flyernum = flyernum;
}
public int getPoints() {
return points;
}
public void setPoints(int points) {
this.points = points;
}
#Override
public String toString() {
return "Passenger{" +
"name='" + name + '\'' +
", type='" + type + '\'' +
", seat='" + seat + '\'' +
", flyernum='" + flyernum + '\'' +
", points=" + points +
'}';
}
}
Now for getting the passenger details from the file, I have create GetPassengerDetails.class, it handles reading in the data from the CSV file and allocating the right values for each Passenger.
public class GetPassengerDetails{
/** Reads the file one line at a time. Each line will is that split up and translated into a Passenger object */
public List<Passenger> getPassengersFromFile(BufferedReader reader) throws IOException {
List<Passenger> passengers = new ArrayList<>();
String line;
while ((line = reader.readLine()) != null) {
String[] passengerDetails = line.trim().split(",");
Passenger passenger = new Passenger();
for (int i = 0; i < passengerDetails.length; i++) {
SetPassengerName(passengerDetails, passenger, i);
setPassengerFlightType(passengerDetails, passenger, i);
setPassengerSeatNumber(passengerDetails, passenger, i);
SetPassengerFlyerNumber(passengerDetails, passenger, i);
setPassengerPoints(passengerDetails, passenger, i);
}
passengers.add(passenger);
}
return passengers;
}
private void setPassengerPoints(String[] passengerDetails, Passenger passenger, int i) {
if(i< passengerDetails.length && i == 4) {
passenger.setPoints(Integer.parseInt(String.valueOf(passengerDetails[4])));
}
}
private void SetPassengerFlyerNumber(String[] passengerDetails, Passenger passenger, int i) {
if(i< passengerDetails.length && i == 3) {
passenger.setFlyernum(String.valueOf(passengerDetails[3]));
}
}
private void setPassengerSeatNumber(String[] passengerDetails, Passenger passenger, int i) {
if(i< passengerDetails.length && i == 2) {
passenger.setSeat(String.valueOf(passengerDetails[2]));
}
}
private void setPassengerFlightType(String[] passengerDetails, Passenger passenger, int i) {
if(i< passengerDetails.length && i == 1) {
passenger.setType(String.valueOf(passengerDetails[1]));
}
}
private void SetPassengerName(String[] passengerDetails, Passenger passenger, int i) {
if(i< passengerDetails.length & i == 0) {
passenger.setName(String.valueOf(passengerDetails[i]));
}
}
}
Here is the main method to test the above code :
public class Main {
public static void main(String[] args) throws IOException {
String fileName = "resources/passengers.csv";
BufferedReader reader = new BufferedReader(new FileReader(fileName));
GetPassengerDetails passengerDetails = new GetPassengerDetails();
List<Passenger> passengers = passengerDetails.getPassengersFromFile(reader);
// For Testing Purposes lets get the Passengers
for (Passenger passenger : passengers
) {
System.out.println(passenger.toString());
}
}
}
After running the main method, this is the result that you will get :
Use this main method to read data from text files and converge data into the Passengers object. The whole list of passengers in passengersList objec.
public static void main(String[] args) {
List<Passengers> passengersList = new ArrayList<Passengers>();
File file = new File("Your file location..");
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
while ((st = br.readLine()) != null){
String[] data = st.split(",");
String flyNumber = null;
int flyPoints = 0;
switch (data.length){
case 4: flyNumber = data[3];
break;
case 5: flyNumber = data[3];
flyPoints = Integer.valueOf(data[4]);
break;
}
Passengers passenger = new Passengers(data[0], data[1], data[2], flyNumber, flyPoints);
passengersList.add(passenger);
}
System.out.println(passengersList.get(0));
} catch (IOException e) {
e.printStackTrace();
}
}
I have been trying to get an ArrayList to save to a file. I can see it is creating the text file, but nothing is placed inside the text file, just blank.
Here is the main code with the ArrayList,Switch with option to save.
static int input, selection, i = 1;
static ArrayList<Animals> a;
// Main Method
public static void main(String[] args){
// Create an ArrayList that holds different animals
a = new ArrayList<>();
a.add(new Animals(i++, "Bear", "Vertebrate", "Mammal"));
a.add(new Animals(i++, "Snake", "Invertebrate", "Reptile"));
a.add(new Animals(i++, "Dog", "Vertebrate", "Mammal"));
a.add(new Animals(i++, "Starfish", "Invertebrates", "Fish"));
while (true) {
try {
System.out.println("\nWhat would you like to do?");
System.out.println("1: View List\n2: Delete Item\n3: Add Item\n4: Edit Item\n5: Save File\n0: Exit");
selection = scanner.nextInt();
if(selection != 0){
switch (selection) {
case 1:
ViewList.view();
Thread.sleep(4000);
break;
case 2:
Delete.deleteItem();
Thread.sleep(4000);
break;
case 3:
Add.addItem();
Thread.sleep(4000);
break;
case 4:
Edit.editItem();
Thread.sleep(4000);
break;
case 5:
Save.saveToFile("animals.txt", a);
Thread.sleep(4000);
break;
This is what I have written to handle file.
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
public class Save extends ALProgram{
public static void saveToFile(String fileName, ArrayList list){
Path filePath = Paths.get(fileName);
try{
System.out.println("File Saved");
Files.write(filePath, list, Charset.defaultCharset());
}catch(IOException e){
e.printStackTrace();
}
}
}
Here is Animal Class
class Animals {
public int id;
public String type, vertebrate, aclass;
public Animals(int id, String type, String vertebrate, String aclass) {
this.id = id;
this.type = type;
this.vertebrate = vertebrate;
this.aclass = aclass;
}
public int getID() {
return id;
}
public String getType() {
return type;
}
public String getVert() {
return vertebrate;
}
public String getaclass() {
return aclass;
}
}
There are two changes:
Your class need to implement CharSequence to be eligible to be passed to Files.write.
You need to over-ride toString method to specify how your contents look like when saved.
I can see output after above two changes.
class Animals implements CharSequence {
public int id;
public String type, vertebrate, aclass;
public Animals(int id,String type,String vertebrate,String aclass) {
this.id = id;
this.type = type;
this.vertebrate = vertebrate;
this.aclass = aclass;
}
public int getID() {
return id;
}
public String getType() {
return type;
}
public String getVert() {
return vertebrate;
}
public String getaclass() {
return aclass;
}
#Override
public int length() {
return toString().length();
}
#Override
public char charAt(int index) {
return toString().charAt(index);
}
#Override
public CharSequence subSequence(int start, int end) {
return toString().subSequence(start, end);
}
/* (non-Javadoc)
* #see java.lang.Object#toString()
*/
#Override
public String toString() {
return "Animals [id=" + id + ", type=" + type + ", vertebrate=" + vertebrate + ", aclass=" + aclass + "]";
}
}
So, first one, you cannot just save it by casting it to string.
You need to get each element, build a string obly then write it down to file.
Here's an example:
public static void saveToFile(String fileName, ArrayList<Animal> list){
StringBuilder sb = new StringBuilder();
for(int i=0; i<=list.size(); i++) {
Animal lAn = list.get(i);
sb.Append("Animal ID: "+lAn.getID()+"; Animal type: "+lAn.getType()+"; Animal vert: "+lAn.getVert()+"; Animal aclass: "+lAn.getaclass()+"\r\n");
}
try (PrintStream out = new PrintStream(new FileOutputStream(fileName))) { out.print(sb.toString()); }
} catch(IOException e){ e.printStackTrace(); } }
Try it. Theres also could be some mistakes/mispelling because I wrote this code from my phone.
You could write only Iterable<? extends CharSequence>, so change you code like below.
Please make sure to override toString method of Animal class
List<Animal> animals = new ArrayList<>();
Animal a1 = new Animal(1L, "XYZ", "A2B");
Animal a2 = new Animal(2L, "ABC", "IJK");
animals.add(a1);
animals.add(a2);
List<String> strings = new ArrayList<>();
for (Animal animal : animals) {
strings.add(animal.toString());
}
Files.write(Paths.get("output.out"), strings, Charset.defaultCharset());
This program is used for a flash card application. My constructor is using a linked list, but the problem is that when I use a method that list the cards inside a specific box it is not printing the desired result. The system should print "Ryan Hardin". Instead it is printing "Box$NoteCard#68e86f41". Can someone explain why this is happening and what I can do to fix this? I have also attached both my box and note card classes.
import java.util.LinkedList;
import java.util.ListIterator;
public class Box {
public LinkedList<NoteCard> data;
public Box() {
this.data = new LinkedList<NoteCard>();
}
public Box addCard(NoteCard a) {
Box one = this;
one.data.add(a);
return one;
}
public static void listBox(Box a, int index){
ListIterator itr = a.data.listIterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
}
public static void main(String[] args) {
NoteCard test = new NoteCard("Ryan", "Hardin");
Box box1 = new Box();
box1.addCard(test);
listBox(box1,0);
}
}
This is my NoteCard Class
public class NoteCard {
public static String challenge;
public static String response;
public NoteCard(String front, String back) {
double a = Math.random();
if (a > 0.5) {
challenge = front;
} else
challenge = back;
if (a < 0.5) {
response = front;
} else
response = back;
}
public static String getChallenge(NoteCard a) {
String chal = a.challenge;
return chal;
}
public static String getResponse(NoteCard a) {
String resp = response;
return resp;
}
public static void main(String[] args) {
NoteCard test = new NoteCard("Ryan", "Hardin");
System.out.println("The challenge: " + getChallenge(test));
System.out.println("The response: " + getResponse(test));
}
}
Try to override the method toString() in your class NoteCard.
#Override
public String toString()
{
//Format your NoteCard class as an String
return noteCardAsString;
}
In First place you are making too much use of static keyword. I am not sure whether you need that. Anyways create two instance variable front and back and assign value to it in constructor of NoteCard class, Also implement toString method
public class NoteCard {
public static String challenge;
public static String response;
public String front;
public String back;
public NoteCard(String front, String back) {
//your code
this.front = front;
this.back = back;
}
#Override
public String toString()
{
//return "The challenge:" + challenge + " " + "The response: " + response;
return "The Front:" + front + " " + "The Back: " + back;
}
Note: Since the instance method toString() is implicitly inherited
from Object, declaring a method toString() as static in a sub type
causes a compile-time error SO DON'T MAKE THIS METHOD STATIC
I want to make an array of objects and use it in different functions. I wrote this pseudocode
privat stock[] d;
privat stock example;
public void StockCheck(){
d =new stock[2];
d[0]= new stock("a","test1", 22);
d[1]= new stock("b","test2", 34);
}
#Override
public stock getStock(String name) throws StockCheckNotFoundException{
int i;
System.out.println("ok" + name + d.legth); // error
example = new stock("example","example",2);
return example;
}
In class test I make an instance of getStock and I call the function getStock stock.getStock();
I get a NullPointerExeption when I do d.length. d is null but I don't understand why.
Hmmmm. If that is in any way like your real code, then the problem is that your "constructor" isn't really a constructor, as you've declared it to return void, making it an ordinary method instead. Remove tbat "void" and it may fix the problem!
Perhaps this example of code will do what you need, using three classes
Test - the main test code
Stock - the implied code for Stock from your question
StockCheck - the corrected code from your question.
(Note: you may really want to use an ArrayList inside StockQuote so you can add and delete Stocks.)
Test class
package stackJavaExample;
public class Test {
public static void main(String[] args) {
String[] testNames = {"test1","test2","notThere"};
StockCheck mStockCheck = new StockCheck();
for (int i=0; i<testNames.length; i++) {
Stock result = mStockCheck.getStock(testNames[i]);
if (result == null) {
System.out.println("No stock for name: " + testNames[i]);
} else {
System.out.println("Found stock: " + result.getName() + ", " + result.getSymbol() + ", " + result.getValue());
}
}
}
}
Stock class
package stackJavaExample;
public class Stock {
private String symbol;
private String name;
private double value;
public Stock(String symbol, String name, double value) {
this.symbol = symbol;
this.name = name;
this.value = value;
}
public String getSymbol() { return symbol;}
public String getName() { return name;}
public double getValue() {return value;}
}
StockCheck class
package stackJavaExample;
public class StockCheck {
private Stock[] d;
public StockCheck() {
d = new Stock[2];
d[0] = new Stock("a","test1", 22);
d[1] = new Stock("b","test2", 34);
}
public Stock getStock(String name) {
for (int i=0; i < d.length; i++) {
if (d[i].getName().equalsIgnoreCase(name)) {
return d[i];
}
}
return null;
}
}