Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Need some on Java

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
privatemoon
How do I cheat?
Reputation: 0

Joined: 01 Jan 2010
Posts: 5

PostPosted: Fri Jan 01, 2010 6:41 pm    Post subject: Need some on Java Reply with quote

I'm working on the StringTest applet for Java and also a FortuneTeller applet

Here's the part of the StringTest applet code that I need help on:
Code:

public void actionPerformed(ActionEvent e)
{
    String str = input.getText();
   
    // ... code to process str or call a method here
    // str = process(str);

    result.setText(str);
    input.selectAll();
}


The FortuneTeller applet is supposed to use a bunch of arrays (fortunes) and randomly use one

Here's the code:
Code:

public class FortuneTeller extends JApplet
      implements ActionListener
{
   <...fortune arrays goes here...>
    private JTextField display;
    private AudioClip ding;

    private void init()
    {
        ding = getAudioClip(getDocumentBase(), "ding.wav");

        display = new JTextField("  Press \"Next\" to see your fortune...", 30);
        display.setBackground(Color.white);
        display.setEditable(false);
 
        JButton go = new JButton("Next");
        go.addActionListener(this);

        Container c = getContentPane();
        c.setLayout(new FlowLayout());
        c.add(display);
        c.add(go);
    }

    public void actionPerformed(ActionEvent e)
    {
         ding.play();

         // Displays a random fortune using a Math.random() method to choose  a random integer from 0 to the length of the array -1 (need help on this one)
         
         display.setText( . . .);
     }
}


Thanks to anyone who can provide some insight to these problems, I'm just still a beginner at Java, so please don't try to hit me too hard with code I haven't learned yet.
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Fri Jan 01, 2010 7:26 pm    Post subject: Reply with quote

multiply the double returned by math.random by array length - 1, then round it. not as in rounding down but as in math.round. use the resulting value as an index into your array
Back to top
View user's profile Send private message
Caelestis
Expert Cheater
Reputation: 0

Joined: 03 May 2007
Posts: 153

PostPosted: Sat Jan 02, 2010 1:24 am    Post subject: Reply with quote

I don't understand the purpose of StringTest so I can't help you.
Back to top
View user's profile Send private message
privatemoon
How do I cheat?
Reputation: 0

Joined: 01 Jan 2010
Posts: 5

PostPosted: Sat Jan 02, 2010 4:28 am    Post subject: Reply with quote

Sorry Slugsnack, I haven't learned math.round yet, but thanks for replying to this thread. It would be really helpful if you could put suggestions on code, since I'm slow on explanations, sorry.

Edit: StringTest is an applet to enter a name such as 'Micheal Jackson' and turn it around to 'Jackson, Micheal' with a click of a button. Not really useful, but it's something I have to do, so it can't be helped *sigh*.
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Sat Jan 02, 2010 9:59 am    Post subject: Reply with quote

if you can paste your current full code including array implementations i'd be able to help more
Back to top
View user's profile Send private message
Caelestis
Expert Cheater
Reputation: 0

Joined: 03 May 2007
Posts: 153

PostPosted: Sat Jan 02, 2010 12:22 pm    Post subject: Reply with quote

To get a random integer from 0 to length of array -1 you do

int random = (int) (Math.random()*array.length);

and to do the name thing:

String name = "Michael Jackson";
String names[] = name.split(" ");
System.out.print(names[1] + ", " + names[0]);
Back to top
View user's profile Send private message
privatemoon
How do I cheat?
Reputation: 0

Joined: 01 Jan 2010
Posts: 5

PostPosted: Sat Jan 02, 2010 6:42 pm    Post subject: Reply with quote

Here's my current code for the StringTest applet

Code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class StringTest extends JApplet
        implements ActionListener
{
  private JTextField input, result;

  public void init()
  {
    input = new JTextField(20);
    input.setBackground(Color.white);
    input.addActionListener(this);
    input.selectAll();

    result = new JTextField(20);
    result.setBackground(Color.white);
    result.setEditable(false);

    Container c = getContentPane();
    c.setLayout(new FlowLayout());
    c.add(new JLabel("Input:"));
    c.add(input);
    c.add(new JLabel("Result:"));
    c.add(result);

    input.requestFocus();
  }

  public void actionPerformed(ActionEvent e)
  {
    String str = input.getText();
   
    str = firstNameFirst(str); //change code here
   
    result.setText(str);
    input.selectAll();
  }
 
  public String firstNameFirst(String lastFirst)
  {
     int commaPos = lastFirst.indexOf(',');
     String last = lastFirst.subString(0,commaPos);
     String first = lastFirst.subString(commaPos+2);
     String firstLast = "*" + first = " " + last = "*";
     return firstLast;
  }
}


To Caelestis:
I haven't learned what .split means yet and the StringTest applet isn't only limited to just one name, it can be done for as many names as you would like. Thanks for the FortuneTeller help though.
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Sat Jan 02, 2010 6:50 pm    Post subject: Reply with quote

uhhhh so what HAVE you learnt and are allowed to use ?
Back to top
View user's profile Send private message
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Sat Jan 02, 2010 8:43 pm    Post subject: Reply with quote

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html
Back to top
View user's profile Send private message
Caelestis
Expert Cheater
Reputation: 0

Joined: 03 May 2007
Posts: 153

PostPosted: Sat Jan 02, 2010 10:29 pm    Post subject: Reply with quote

privatemoon wrote:

To Caelestis:
I haven't learned what .split means yet and the StringTest applet isn't only limited to just one name, it can be done for as many names as you would like. Thanks for the FortuneTeller help though.


If you haven't learned what a method does, just look at the damn javadocs above that was posted. Every java developer should know about it. Also, the question is very stupid, of course I know it should do it for more than one name, I was demonstrating usage for you. It's still up to you to get the string from the input box or whatever. I have the string declared, but you implement my example on your own.

Mad
Back to top
View user's profile Send private message
privatemoon
How do I cheat?
Reputation: 0

Joined: 01 Jan 2010
Posts: 5

PostPosted: Sat Jan 02, 2010 10:34 pm    Post subject: Reply with quote

Heres a list of the String class methods I can use (or already know of)

length
charAt
substring
concat
compareTo
compareToIgnoreCase
equals
equalsIgnoreCase
indexOf
lastIndexOf
trim
replace
toUpperCase
toLowerCase

Something won't allow me to place the .s in front of the methods, so just add .s to all of them.
Back to top
View user's profile Send private message
Caelestis
Expert Cheater
Reputation: 0

Joined: 03 May 2007
Posts: 153

PostPosted: Sun Jan 03, 2010 12:31 am    Post subject: Reply with quote

I don't see what the problem is. Your code is fine, you shouldn't limit yourself. Java can handle all the methods described in the doc above.
Back to top
View user's profile Send private message
privatemoon
How do I cheat?
Reputation: 0

Joined: 01 Jan 2010
Posts: 5

PostPosted: Mon Jan 04, 2010 10:47 pm    Post subject: Reply with quote

Thanks for all the help, I finally got most of it down.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites