Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Ready to Program: Help Please!

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Ready to Program: Help Please!

    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


  2. #2
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Ready to Program: Help Please!

    Hello.
    Do you want to update the database table or just the gui table in which you are displaying the data?
    If it is just the display table then directly you can use the following statement:
    data [cnt] [2] = (Math.random () * 5001) + 1;
    If the value you want is integer just do the typecast.

    Thanks,
    Syed.

  3. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Ready to Program: Help Please!

    Hello, Syed

    When I Implement the code:
    data [cnt] [2] = (Math.random () * 5001) + 1;

    It throws out the following error:

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

    now, I need to cast this using the "" double.toString() ""-workaround right?
    however I'm unsure how to do this.

    When I try:

    data [cnt] [2] = double.toString (HighestScore);

    Error Occurs: DoubleLiteral expected instead of this token

    Please advise.
    Shanse
    Thanks

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Ready to Program: Help Please!

    Quote Originally Posted by Shanse View Post
    double y = (Math.random () * 5001) + 1; (which generates random numbers between 1-5000)
    I don't think so.

    How did you set the values in the table the first time? Do the same thing using the value y (from your example) instead of what ever value you used the last time.
    If this is of no help I am afraid I do not understand the question

  5. #5
    Junior Member
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Ready to Program: Help Please!

    Edit: I fixed the issue

    However I'm getting decimal answers now with the coding:

    double HighestScore = (Math.random () * 5001) + 1;
    data [cnt] [2] = Double.toString (HighestScore);

    Question:

    Is there a way to round-off the answers (i.e if 2560.81 to 2561)
    or
    not show the decimals at all?

    Thanks in advance,
    Shanse

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Ready to Program: Help Please!

    1. See java.util.Random.nextInt(int n)
    2. I do not understand what trouble you are having in setting a variable to some value, in this case the random number. Please post a specific question

  7. #7
    Junior Member
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Ready to Program: Help Please!

    Nevermind, I managed to fix my issue by using math.round.

    Thanks alot for the quick response & answers to my query.



    Later,
    Shanse

Similar Threads

  1. Ready To Program Helpp!!!!!!!
    By valavan in forum Java IDEs
    Replies: 1
    Last Post: October 3rd, 2012, 09:55 PM
  2. got all .java files ready. how to make them into jar?
    By sibbe in forum Java Theory & Questions
    Replies: 22
    Last Post: December 6th, 2010, 06:09 PM
  3. Replies: 0
    Last Post: June 18th, 2010, 07:18 AM
  4. Getting ready for an interview
    By Charlie in forum The Cafe
    Replies: 5
    Last Post: June 4th, 2010, 07:25 PM