Need help with basic ready to program issue

Shanse

Active Member
Joined
Jul 27, 2013
Messages
33
Reaction score
0
Hello everyone,

I'm a new programmer and thus still suck, I do not even know if I should be posting this here or be able to receive any help.

However, my issue is the following: (Note: I'm using Ready to Program to code)

I've wrote a gaming database and I'm calling them using the following rs statement:

if (eve.getSource () == mybtn)
{
try
{
String sql = "Select * from Games Order by Genre";

int cnt = 0;
rs = stmt.executeQuery (sql);



while (rs.next ())
{


String Genre = rs.getString ("Genre");
String Popularity = rs.getString ("Popularity");
String HighestScore = rs.getString ("HighestScore"); // I would like to modify this value
String Rating = rs.getString ("Rating");
String PlayerName = rs.getString ("PlayerName");
String TournamentAvailable = rs.getString ("TournamentAvailable");
String Game = rs.getString ("Game");


data [cnt] [0] = Genre;
data [cnt] [1] = Popularity;
data [cnt] [2] = HighestScore; // I would like to modify this value
data [cnt] [3] = Rating;
data [cnt] [4] = PlayerName;
data [cnt] [5] = TournamentAvailable;
data [cnt] [6] = Game;

cnt++;

table.updateUI ();


}

}


catch (Exception e)
{
System.out.println ("Vraag 1 onsuksesvol");
e.printStackTrace ();
}
}



Question:

I would like to modify the value of HighestScore by replacing it with a random generated number, using the following code: double y = (Math.random () * 5001) + 1; (which generates random numbers between 1-5000)

Then, I need to take the random generated value and import it back into my JTabel.

Could anyone assist me?
Thanks in advance
Shanse
 
Last edited:
simplest is to put
Code:
String HighestScore = (Math.random () * 5001) + 1;
into your while statement

or do you need to store the value's in your database? Then you need to write an sql statement to Insert.
otherwise you could just have a random field returned from the sql select statement if you don't need to
store the random values.
 
Last edited:
When I insert the code into the while loop:

String HighestScore = (Math.random () * 5001) + 1;

It says: "You cannot assign a "double" expression to a "java.lang.String" variable"

Please advise.
Thanks
 
try
Code:
 double HighestScore = (Math.random () * 5001) + 1;

The problem with that workaround is when I set the String to a Double it gives a error further down:

data [cnt] [2] = HighestScore; <--- error occurs here

error: You cannot assign a "double" expression to a "java.lang.String" variable


Is there a way I can fix this aswell?

Thanks
Shanse
 
you need to cast it, think double.toString(HighestScore) should work.
 
Sorry if I'm being overly stupid, but

Where do I enter the code for:

double.toString(HighestScore)
 
where the error was, data [cnt] [2] = double.toString(HighestScore);

you do realise the program will be kinda useless?
 
Ugh, another bump.

data [cnt] [2] = double.toString (HighestScore); <--- error occurs on (double)

error: DoubleLiteral expected instead of this token
 
Ugh, another bump.

data [cnt] [2] = double.toString (HighestScore); <--- error occurs on (double)

error: DoubleLiteral expected instead of this token

ok you need to learn to fish for yourself now, google java DoubleLiteral expected instead of this token
 
Thanks alot for the support, Drake2007.

I managed to fix the issue by using data [cnt] [2] = Double.toString (HighestScore);

:)
 
Last edited:
I'm not a Java programmer, so excuse syntax errors.

Convert the double to a long via an explicit cast i.e. something like Long.ToString(long(HighestScore)).

Or use a version of ToString() that accepts a format string.
 
Don't know about the Java coding except ja, it's not javascript or php where you can just auto cast variables.

One issue with the logic though
Math.random gives a value between 0 and 1. I assume the *5001 is to get a value between 0.x and 5000.x which is then floored when converting to an integer. But this still has a chance of giving exactly 5001 so you have to check for this. Also adding 1 for the minimum value will also add it to the maximum meaning some of your 5000's will be 5001's with the occasional 5002. You have to use *5000 instead but there are better ways of doing this.
 
As Swa said, you have a logical error. Your random code should look like

String value = "" + ((int)(Math.random ()*5000)+1);

Java truncates the decimal portion, so essentially you will end up with a number between 0 and 1, possibly including 0 but never 1. (That's the Math.random () bit. Multiply by 5000, you end up with a number between 0 and 5000, possibly including 0, but never 5000. Adding 1 then shifts that set to between 1 and 5001, possibly including 1 but never 5001. It may be 5000,9999999, but never 5001. Then when you cast to integer, it truncates the value.

The ""+ portion is a simple way of storing numbers into a String variable. It's starts an empty String, and the + sign activates the String.concatenate() method, which then simply spends the number to the end of a blank string. Or you could cast to int, and then use the Integer.toString method as implied earlier. Neither method is more correct.
 
Thumbs-up for the corrections, Swa + Graviti.

I was wondering if somebody could look at another piece of code which is not working as intended.

It's a access database which is called (Login) with 2 columns (Username and Password)

What happens:

Basically, when I log in with the first account's name and password, it works.
The second username/password and soforth does not work, so I assume it does not loop through the whole database correctly? however I don't know how to fix this as I don't see a problem :(

I'm using this code to retrieve the info:

try
{
String sql = "Select * from Login";
rs = stmt.executeQuery (sql);

String Username = "";
String Password = "";

while (rs.next ())
{
Username = rs.getString ("Username");
Password = rs.getString ("Password");

if (myuser.getText ().equals (Username) && (mypass.getText ().equals (Password)))

{
JOptionPane.showMessageDialog (null, "Redirecting you to profile management.. please wait a while.");
myframe.setVisible (false);
JOptionPane.showMessageDialog (null, "You are now entering your profile.");
JOptionPane.showMessageDialog (null, "Please be patient while stats are loading. . . . .");
myprofile.setVisible (true);
break;
}

else

{
JOptionPane.showMessageDialog (null, "Incorrect. Please retry or contact a game manager.");
break;
}



Also, on a bit of a sidenote, is it possible to "block" using a certain button more than twice? I want a particular button only to be used once.

Thanks in advance
Shanse
 
Firstly, don't ever pull all your usernames and password from the database, and then search through them. Let the database do that. So change your thought process from

1. Get all usernames and passwords
2. While i have passwords to check against loop the following
2.1 Get username from text field
2.2 Get password from text field.
2.3 if they match do X
2.4 if they don't do Y

To

1. Get username and password from text fields
2. "SELECT COUNT(*) FROM LOGIN WHERE Username = " + user + " AND Password = "+pass+";" is the SQL string you would need. This is guaranteed to return a single value from the database.
3. Instead of looping through a resultset, because you know you have one and only one record returned, because it is a count, just call rs.next() and get the first field. Compare that to 1. If it doesn't equal 1 you have a problem. Either the password doesn't match, or the database has more than one user with that detail.

The reason you code doesn't work above, is because of the break in your else statement. Think about that one.
 
Something like

Code:
try
{
   String user = myUser.getText();
   String pass =  myPass.getText();
   String sql = "SELECT COUNT(*) FROM LOGIN WHERE Username = '" + user + "' AND Password = '" + pass + "';";
   //if printed out using System.out.println should view as follows, with sample username and password user:pass
   //SELECT COUNT(*) FROM LOGIN WHERE Username = 'user' AND Password = 'pass';
   rs = stmt.executeQuery(sql);
   rs.next();
   int count = rs.getInt();
   if (count==1)
   {
      JOptionPane.showMessageDialog (null, "Redirecting you to profile management.. please wait a while.");
      myframe.setVisible (false);
      JOptionPane.showMessageDialog (null, "You are now entering your profile.");
      JOptionPane.showMessageDialog (null, "Please be patient while stats are loading. . . . .");
      myprofile.setVisible (true);
   }
   else
   {
      JOptionPane.showMessageDialog (null, "Incorrect. Please retry or contact a game manager.");
   }

Also, I don't recommend using JOptionPanes for showing messages to the user. This basically requires them to click OK three times before progressing to your next window, and this is unnecessary.
 
Also, on a bit of a sidenote, is it possible to "block" using a certain button more than twice? I want a particular button only to be used once.

Of course it's possible :)

when you click the button, it will trigger an ActionListener (I'm assuming, not familiar with your IDE environment, but either way you have some form of listener on the button that will trigger an event).

In the method you create as a result of this Action, just merely set the enabled flag on the button to false.

i.e

myButton.setEnabled(false);

This disables the button and it can no longer be clicked. Hence, used once.
 
Hey Graviti,

First of all, thanks alot for taking the time to help me out :)

the disabled button seems to work, thumbs-up for that!

The program seems to work 99% now, the only issue is when the username or password is wrong, It does not throw the error box

try
{
String user = myuser.getText ();
String pass = mypass.getText ();

String sql = "SELECT COUNT(*) FROM LOGIN WHERE Username = '" + user + "' AND Password = '" + pass + "';";

rs = stmt.executeQuery (sql);
while (rs.next ())
{
int count = rs.getInt (1);
if (count == 1)
{
myframe.setVisible (false);
myprofile.setVisible (true);
break;
}
else
{
JOptionPane.showMessageDialog (null, "Incorrect. Please retry or contact a game manager.");
}



Please advise
Thx
Shanse
 
Last edited:
Top
Sign up to the MyBroadband newsletter
X