Hi all,
Can some please have a look at the codes below and tell how I can make the switch statement accept the value from the variable named answer. I think the switch statement only accepts certain data type but I don't know to make this work.
Please help me with this problem. Thank you.
package game;
import java.util.*;
import java.io.*;
public class GameInfo
{
private ArrayList<Game> games = new ArrayList<Game>();//Array to store game Info, Name and Type
private GameInfo()//game array
{
games.add( new Game ("RPG", "EVE Online",
"EVE is a sci-fi massively multiplayer online game. " +
"Players take the role of spaceship pilots seeking " +
"fame, fortune, and adventure in a huge, complex, " +
"exciting, and sometimes hostile galaxy."));
games.add( new Game ("RPG", "Dark Ages",
"Dark Ages is an online role-playing game set in a " +
"fantasy world of faeries and magic. Customize your " +
"character and go adventuring." ) );
games.add( new Game ("RPG", "City of Heroes",
"City of Heroes brings the world of comic books " +
"alive. Craft your hero's identity and join millions " +
"of Hero characters in a constantly expanding " +
"universe, explore the sprawling online metropolis " +
"of Paragon City, and battle a host of foes " +
"including criminals, villains, and monsters."));
games.add( new Game ("Simulation", "King of Drift",
"Race around the track as you try to win against the " +
"AI. Drift around corners for an advantage" ) );
games.add( new Game("Simulation", "Jet Ski Rush",
"Drive your jet-ski through the wavey waters and " +
"complete all 10 extreme levels! Hop over gators, " +
"hippos, penguins to hit ramps and do tricks! Grab " +
"NOS to speed up through super loops! " ) );
games.add( new Game("FPS", "Hover Tanks",
"Blast away at your opponents to set a highscore in " +
"this awesome 3D first-person shooter." ) );
games.add( new Game("FPS", "Pearl Habour 1941",
"Shoot all incoming Kamikaze pilots and protect " +
"Pearl Habour") );
InputStreamReader istream = new InputStreamReader(System.in) ;
BufferedReader bufRead = new BufferedReader(istream) ;
System.out.println("------------------------\nTypes of Games available\n------------------------");
System.out.println();
try
{
System.out.println("Please enter a choise based on the following available game types");
System.out.println("(1) First Person Shooter");
System.out.println("(2) Role Palaying Games");
System.out.println("(3) Simulation games");
String chooseType = bufRead.readLine();
int cType = Integer.parseInt(chooseType);
int answer = cType;
switch (answer)
{
case '1':
System.out.println("--------------------------\nFirts Person Shooter Games\n--------------------------");
for ( int i = 0; i < games.size(); i++)
{
if ( games.get(i).getType().equals("FPS"))
{
System.out.println(games.get(i).toString());
}
}
break;
case '2':
System.out.println("------------------\nRole Playing Games\n------------------");
for ( int i = 0; i < games.size(); i++)
{
if ( games.get(i).getType().equals("RPG"))
{
System.out.println(games.get(i).toString());
}
}
break;
case '3':
System.out.println("----------------\nSimulation Games\n----------------");
for ( int i = 0; i < games.size(); i++)
{
if ( games.get(i).getType().equals("Simulation"))
{
System.out.println(games.get(i).toString());
}
}
break;
default:
System.out.println("Please press 1, 2 or 3");
}
}
catch (IOException err)
{
System.out.println("Error reading line");
}
}
public static void main(String[] args)// main method to start the program
{
new GameInfo();
}
}