• Beginner Java Question
    15 replies, posted
I have two classes(?) that I am trying to combine into one. I am basically trying to pull sentences from an array randomly, and place them into the JOptionPane. I have been trying to do this for a few hours, and it's late and I'd like to finish soon. Here is the code, if this helps answer my question. Sentence gen part: [code] import java.util.Random; public class SentenceGenerator { private String[] sentenceArray = {"The sentences go here, I just removed them for posting. :D"}; private Random randGen = new Random(); public String generateRandomSentence() { String sentence = sentenceArray[randGen.nextInt(sentenceArray.length)] + " "; return sentence; } public static void main(String[] args) { SentenceGenerator sentenceGen = new SentenceGenerator(); for(int i = 0; i < 1; i++) } } [/code] And the Other part: [code] import javax.swing.JOptionPane; public class CompliantBox { public static void main ( String args[] ) { String enterCompliant; String SentenceGenerator; enterCompliant= JOptionPane.showInputDialog( "Enter The Compliant" ); JOptionPane.showMessageDialog(SetenceGenerator.JOptionPane.PLAIN_MESSAGE ); } } [/code] OptionPane.showMessageDialog(SetenceGenerator.JOptionPane.PLAIN_MESSAGE ); //see here I tried to incorporate the sentence gen, hoping that having it in the main method along with this part of the code it would work. It did not. Please Facepunch, any help that you can contribute would be amazing!
Well, I tried a few ways to combine the two. Once in Dr.Java, and a couple more times in Eclipse. But, I just can't seem to find the right way to pull the two together. I should have stated this in the OP. :/ But, my question is: How can I make this work? Does one have to be a class, and the other something else? Do they both have to be classes? One a method the other a class? I'm still new at this, but I'm learning.
"Compliant" is an adjective. Did you mean "complaint"? Anyway, did you want something like this? [cpp] public static void main ( String args[] ) { SentenceGenerator responseGenerator = new SentenceGenerator(); String compliant = JOptionPane.showInputDialog( "Enter The Compliant" ); // JOptionPane.showMessageDialog require three arguments before the message type can be passed. Replace "Response" with whatever title you were looking for. JOptionPane.showMessageDialog(null, responseGenerator.generateRandomSentence(), "Response", JOptionPane.PLAIN_MESSAGE); } [/cpp] There's a multitude of things wrong with your code, so the above code is just a wild guess. We can help you if you tell us exactly what you want to achieve.
Well, I am going for a Compliant box for my friend's forum website. And every time you enter a compliant, it randomly chooses a smart ass insult. :/ I made an array with all the insults that he wants [code]private String[] sentenceArray = {"Hahahahaha! You call THAT a compliant?? Here is a REAL compliant: Fuck you.", "Got Milk? No, you don't because you're too fucking busy complaining to shop.", "Attempting to give a shit................................. Process failed. Shit is not given.", "AAAAAAAAAAAAAANNNNNNNNNNNNNNNNNNDDDDDDDDDDDDDD no one fucking cares! :D"}[/code] I'm doing this for him for a few reasons, mainly because he's done a lot to his site for me, and because I think it is good coding practice.
That's what I thought. Then my code will do exactly what you wanted, except you mean "complaint", not "compliant". Those are two very different words.
Ah, where do I enter the array of stupid insults? And I have TERRIBLE spelling. Google isn't the best spell check if you don't pay attention. :/
[QUOTE=Werem00se;25824495]Ah, where do I enter the array of stupid insults? And I have TERRIBLE spelling. Google isn't the best spell check if you don't pay attention. :/[/QUOTE] As someone else said, you need to put your SentenceGenerator class in a different file and include it in your project. Put the insults in your array in that class. edit: But since your sentence generator is not parameterized at all, I think "InsultGenerator" is a more suitable class name.
So, all I need to do is make a new class with [code] import java.util.Random; public class SentenceGenerator { private String[] sentenceArray = {"Hahahahaha! You call THAT a compliant?? Here is a REAL compliant: Fuck you.", "Got Milk? No, you don't because you're too fucking busy complaining to shop.", "Attempting to give a shit................................. Process failed. Shit is not given.", "AAAAAAAAAAAAAANNNNNNNNNNNNNNNNNNDDDDDDDDDDDDDD no one fucking cares! :D"}; private Random randGen = new Random(); public String generateRandomSentence() { String sentence = sentenceArray[randGen.nextInt(sentenceArray.length)] + " "; return sentence; } public static void main(String[] args) { SentenceGenerator sentenceGen = new SentenceGenerator(); for(int i = 0; i < 1; i++) } } [/code] in it and copy and paste [code]public static void main ( String args[] ) { SentenceGenerator responseGenerator = new SentenceGenerator(); String compliant = JOptionPane.showInputDialog( "Enter The Compliant" ); // JOptionPane.showMessageDialog require three arguments before the message type can be passed. Replace "Response" with whatever title you were looking for. JOptionPane.showMessageDialog(null, responseGenerator.generateRandomSentence(), "Response", JOptionPane.PLAIN_MESSAGE); [/code] into it's own class and it should work?
-snip-
Okay, I think I have put it together right. I only have one error. [code]import javax.swing.JOptionPane; import java.util.Random; public class bitchBox { public class SentenceGenerator { private String[] sentenceArray = {"Hahahahaha! You call THAT a compliant?? Here is a REAL compliant: Fuck you.", "Got Milk? No, you don't because you're too fucking busy complaining to shop.", "Attempting to give a shit................................. Process failed. Shit is not given.", "AAAAAAAAAAAAAANNNNNNNNNNNNNNNNNNDDDDDDDDDDDDDD no one fucking cares! :D"}; private Random randGen = new Random(); public String generateRandomSentence() { String sentence = sentenceArray[randGen.nextInt(sentenceArray.length)] + " "; return sentence; } public void main(String[] args) { for(int i = 0; i < 1; i++) { } } } public static void main ( String args[] ) { SentenceGenerator sentenceGen = new SentenceGenerator(); { for(int i = 0; i < 1; i++) { String complaint =JOptionPane.showInputDialog( "Enter The Compliant" ); // JOptionPane.showMessageDialog require three arguments before the message type can be passed. Replace "Response" with whatever title you were looking for. JOptionPane.showMessageDialog(null, sentenceGen.generateRandomSentence(), "Response", JOptionPane.PLAIN_MESSAGE); } } } } [/code] The error is 1 error found: File: E:\bitchBox.java [line: 33] Error: E:\bitchBox.java:33: non-static variable this cannot be referenced from a static context
Yay! It is working! [url]http://www.mediafire.com/?tr0pzrshs1nunv9[/url]
cant download it D: damn mediafire! [quote] Invalid or Deleted File The key you provided for file download was invalid. This is usually caused because the file is no longer stored on Mediafire. This occurs when the file is removed by the originating user or Mediafire.[/quote] use filesmelt
Hold on, shit! Sorry Facepunch! I uploaded it, and my friend wanted to add more insults to it, so I removed it, and re-uploaded it. Here it is: [url]http://www.mediafire.com/?hsdbf9wwytu46xp[/url] It's not exactly 'funny' though, at least not in my opinion.
mediafire? ugh
Sorry, you need to Log In to post a reply to this thread.