Reputation:
I am trying to split a string
Here is the items
but if a string was GTA ONE
I want to ignore the ONE
just take the input GTA
print out the whole games that start with GTA
GTA ONE
GTA THREE
GTA FOUR
GTA FIVE
but it cannot fail for "GTA ONE"
The bug is that when I type "GTA".
it does return all the matching "GTA" and null.
I want to eliminate null.
The value null is only supposed to return when there's is no game. Can someone explain to me? Thanks.
class Game{
private String name;
public Game(String name{
this.name = name;
}
public String getName(){return name};
public boolean hasGame(String name){
return name.equalsIgnoreCase(this.name);
}
@Override
public String toString(){
return name + " " + edition;
}
}
class Shop{
private LinkedList<Game> games = new LinkedList<Game>();
public Shop(){
games.add(new Game("GTA ONE");
games.add(new Game("Sonic One");
games.add(new Game("GTA THREE");
games.add(new Game("GTA FOUR");
games.add(new Game("GTA FIVE");
}
public static void main(String args[]){
new Shop().sell();
}
private Game game(String name){
ArrayList<Product> tempGames = new ArrayList<Product>();
boolean fix2 = false;
for(Game game: games)
if(game.hasGame(name)){
return game;
}
else if(game.getName().contains(name)){
tempGames.add(game);
fix2 = true;
}
if(fix2)
for(Game tempGame: tempGames)
System.out.println(tempGame);
return null;
}
}
private void sell() {
String name = "GTA";
Game game = game(name);
if(game != null){
System.out.println("Selling " + name);
}
else{
System.out.println(name + " does exist in the store.");
}
}
Upvotes: 1
Views: 105
Reputation: 79035
Since you are not comparing the full name, you need to use contains
instead of equalsIgnoreCase
in hasGame
.
Apart from this, the methods, sell
and game
also have to be corrected to match your requirement. The method, game
needs to return a List<Game>
and accordingly you need to make changes in the method, sell
.
Do it as follows:
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
class Game {
private String name;
public Game(String name) {
this.name = name;
}
public String getName() {
return name;
}
public boolean hasGame(String name) {
return this.name.contains(name);
}
@Override
public String toString() {
return name;
}
}
public class Shop {
private LinkedList<Game> games = new LinkedList<Game>();
public Shop() {
games.add(new Game("GTA ONE"));
games.add(new Game("Sonic One"));
games.add(new Game("GTA THREE"));
games.add(new Game("GTA FOUR"));
games.add(new Game("GTA FIVE"));
}
public static void main(String args[]) {
Shop shop = new Shop();
shop.sell("GTA");
System.out.println();
shop.sell("XYZ");
}
private List<Game> game(String name) {
List<Game> tempGames = new ArrayList<Game>();
for (Game game : games) {
if (game.hasGame(name)) {
tempGames.add(game);
}
}
return tempGames;
}
private void sell(String name) {
List<Game> games = game(name);
if (!games.isEmpty()) {
System.out.println("Selling: ");
for (Game game : games) {
System.out.println(game);
}
} else {
System.out.println(name + " does not exist in the store.");
}
}
}
Output:
Selling:
GTA ONE
GTA THREE
GTA FOUR
GTA FIVE
XYZ does not exist in the store.
Upvotes: 2