| View previous topic :: View next topic |
| Author |
Message |
privatemoon How do I cheat?
Reputation: 0
Joined: 01 Jan 2010 Posts: 5
|
Posted: Fri Jan 01, 2010 6:41 pm Post subject: Need some on Java |
|
|
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 |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Fri Jan 01, 2010 7:26 pm Post subject: |
|
|
| 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 |
|
 |
Caelestis Expert Cheater
Reputation: 0
Joined: 03 May 2007 Posts: 153
|
Posted: Sat Jan 02, 2010 1:24 am Post subject: |
|
|
| I don't understand the purpose of StringTest so I can't help you.
|
|
| Back to top |
|
 |
privatemoon How do I cheat?
Reputation: 0
Joined: 01 Jan 2010 Posts: 5
|
Posted: Sat Jan 02, 2010 4:28 am Post subject: |
|
|
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 |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Sat Jan 02, 2010 9:59 am Post subject: |
|
|
| if you can paste your current full code including array implementations i'd be able to help more
|
|
| Back to top |
|
 |
Caelestis Expert Cheater
Reputation: 0
Joined: 03 May 2007 Posts: 153
|
Posted: Sat Jan 02, 2010 12:22 pm Post subject: |
|
|
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 |
|
 |
privatemoon How do I cheat?
Reputation: 0
Joined: 01 Jan 2010 Posts: 5
|
Posted: Sat Jan 02, 2010 6:42 pm Post subject: |
|
|
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 |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Sat Jan 02, 2010 6:50 pm Post subject: |
|
|
| uhhhh so what HAVE you learnt and are allowed to use ?
|
|
| Back to top |
|
 |
&Vage Grandmaster Cheater Supreme
Reputation: 0
Joined: 25 Jul 2008 Posts: 1053
|
|
| Back to top |
|
 |
Caelestis Expert Cheater
Reputation: 0
Joined: 03 May 2007 Posts: 153
|
Posted: Sat Jan 02, 2010 10:29 pm Post subject: |
|
|
| 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.
|
|
| Back to top |
|
 |
privatemoon How do I cheat?
Reputation: 0
Joined: 01 Jan 2010 Posts: 5
|
Posted: Sat Jan 02, 2010 10:34 pm Post subject: |
|
|
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 |
|
 |
Caelestis Expert Cheater
Reputation: 0
Joined: 03 May 2007 Posts: 153
|
Posted: Sun Jan 03, 2010 12:31 am Post subject: |
|
|
| 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 |
|
 |
privatemoon How do I cheat?
Reputation: 0
Joined: 01 Jan 2010 Posts: 5
|
Posted: Mon Jan 04, 2010 10:47 pm Post subject: |
|
|
| Thanks for all the help, I finally got most of it down.
|
|
| Back to top |
|
 |
|