Originally Posted by
yurano
I don't think you can use Switch-Case with Strings. Ints and Chars only.
What you can do is create a 'database' class that handles names and text. You would also index classes via integer.
There are probably more optimal ways of managing this (eg. writing a database file and having the game thread parse it into an array when it starts up)
You can since Java 7.
Originally Posted by
Zhaveros
Sorry for the late reply. Not sure if this is an illegal bump or not. So what I want to do is create a name data base. The only way to do so with my current knowledge is to make 3 classes. I want to make a RNG that will pick a name based on the number it generates. Now yes I know I can do this all in one class, but I'm trying to practice using multiple classes and seeing what I can do with them. So what I have to do:
Make a class with a switch statement and a lot of names.
Make it so I can have a RNG in my main class that will pick one of the names is display it in my main class.
So in order to achieve this with my current knowledge(Still on beginner tutorials.) is to create a name string like so
RNG will determine below
Code:
String nm;
switch (nm) {
case 0:
nm = "You got the cool weapon you've been going for, for the last month!";
break;
case 1:
nm = "Grats, my pally friend, we dropped some priest gear for you";
break;
default:
System.out.println("No valid numbers");
Forget if I put something here, will know when I actually use this code
}
---------- Post added 2012-10-04 at 10:18 AM ----------
So the code works, but how do I set the value of nm via RNG from my main class?
---------- Post added 2012-10-04 at 10:19 AM ----------
Also, I haven't the slightest clue what generics are. I'm only beginning to dwell on arrays.
If you want to generate random numbers, use the static method random() in the Math class.
For example, a random number between 1 and 100:
Code:
System.out.println((int)(Math.random()*100));
But you wanna do it using a database right?
So, first you have to open a connection.
I'll give you an example using Firebird (because this is the only database I have right now).
YourConnection.java
Code:
public class YourConnection{
public Connection getConnection() {
Connection con = null;
try {
Class.forName("org.firebirdsql.jdbc.FBDriver");
con = DriverManager
.getConnection(
"jdbc:firebirdsql:localhost:C:\\somewhere\\anotherplace\\yourdatabase.fdb", //change the C:\\somewhere\\anotherplace\\yourdatabase.fdb to the place where is your database
"SYSDBA", "masterkey"); //"SYSDBA" is the username, and "masterkey" is the password
} catch (ClassNotFoundException | SQLException e) {
System.out.println(e.printStackTrace());
}
return con;
}
}
Game.java
Code:
public class Game {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
ClassThatWillTakeTheGame.java
Code:
public class ClassThatWillTakeTheGame { // sorry for the big name
public Game getGame(int id) throws SQLException {
Connection connection = new YourConnection().getConnection(); // we got the
// connection
PreparedStatement stmt = null; // created the Statement. ALWAYS use
// PreparedStatement, never Statement
ResultSet rs = null; // The ResultSet. Both stmt and rs have to be null
// in this part
String sql = "SELECT name FROM game WHERE id = ?";
Game game = null;
try {
stmt = connection.prepareStatement(sql); // we already sent the sql
// to the database but
// we still have one
// thing to change: the
// "?". If your SQL is
// wrong or if the table
// doesnt exist, it'll
// throw a SQL exception
stmt.setInt(1, id); // replaced the "?" with the variable id
rs = stmt.executeQuery(); // now we execute the query and rs will
// receive the resultset
if (rs.next()) { // since we are expecting only 1 thing, we use if,
// if we are expecting more than one then we use
// while. If in this case, rs.next() returns
// false it means that nothing was found
game = new Game();
game.setId(id);
game.setName(rs.getString("name")); // here we set the name of
// the game. We used
// rs.getString("yourColumnName")
// to get the value of the
// column.
}
} catch (SQLException e) {
// do w/e what you want here
} finally { // this part is important. finally will always run, doesn't
// matter if your try threw a SQLException, it'll always
// execute we have to check if rs is not null, if it's not
// null then we close it. Same for stmt and connection.
// Remember, close everything that you open (in this case,
// ResultSet, PreparedStatement and Connection) and always
// in this order
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (connection != null) {
connection.close();
}
}
return game;
}
}
Main.java
Code:
public class Main {
public static void main(String[] args) throws SQLException {
Game game = new Game();
ClassThatWillTakeTheGame iDontKnow = new ClassThatWillTakeTheGame();
int gamesInDataBase = 5; // change it to w/e you want
game = iDontKnow.getGame((int) (Math.random() * gamesInDataBase));
if (game == null) {
System.out.println("Ops, no game was found");
} else {
System.out.println(new StringBuilder()
.append("The RNG number was ").append(game.getId())
.append(" and the game that was found with that ID was ")
.append(game.getName()));
}
}
}
I didn't test it but I'm sure it'll work.
Edit:
Firebird driver:
http://www.firebirdsql.org/en/jdbc-driver/
Firebird:
http://www.firebirdsql.org/en/firebird-2-5-1/