Hi everyone...

My boyfriend's birthday is on Saturday, August 13...and i wanted to do something really different this time...since we are 1,000 miles apart...i was thinking if i could write a program which would generate random messages which I would already have written in the code... i was thinking of writing about 100 messages...or more..and ofcourse i only want him to have the application. I was thinking to use the GUI method...and convert the .java code file to .exe and name the application "Surprise" and have him install it in his computer.


The thing is that I am a beginner in programming and i have no idea where to start...and I realllly reallly reallly want this to happen...i know he is going to love it...

I am planning to write the program in java..and i am using netbeans IDE 7.0.1

Any help is greatly appreciated....thanks

well ... if you know nothing about programming at all, it might be a bit late to start learning if you want it to be finished within a few days.

you might pull it off, but ...

you'll need to have a GUI, if you want it to be a GUI app. I'm going totally against my principles here, but since you're not familiar with developing, are not doing this for professional reasons and are on a short 'deadline', so to say, you might want to use NetBeans drag&drop to create that.

It wouldn't help you learn to write a GUI, or help to understand how it works, but for the purpose you described above, it would work just fine.

then, either you add a new class, or you put it in the class you 've just made (the one extending JFrame), here you add an array which is filled with the messages you want to send:

String messages = {"message1","message2","message3","messageX"};

where messageX is the last message you want to put in, so in your case, it would count up to 100 or more.

then, you add a small method, which, when it is called, calculates a random number that would be a correct index for your array.
for instance, if you have an array with 100 messages, that index could be 0 to 99 (those two included), lets say that you return N as the random index.

at this point, you print the Nth element of the array in a label in your GUI application.

when you compile&build in NetBeans, a <ProjectName>.jar file is created in the 'dist' folder of your project, which does everything an exe file would do, so there's no real need to create an .exe for it. actually, if your boyfriend has a computer which does not run a Windows OS, you don't want to have an .exe file at all, since it wouldn't run thereon.

hope this has given you an idea of where (and how) to start.

I understood the last part...where netbeans automatically has the .exe file...

but honestly i am kind of confused....

i tried implementing the GUI method...but thats just one message...is it possible..to write like 100 messages like this and then write the random method?

import javax.swing.JOptionPane;
public class GUI {
public static void main(String[]args) {
//Display Message
    JOptionPane.showMessageDialog(null, "Message1 "
    JOptionPane.showMessageDialog(null, "Message2 "

Edited 5 Years Ago by lashbandi: n/a

thanks alot for the help...i wish i had started on this earlier but this idea just hit me like 2 hours ago...and i am willing to stay up nights to finish this...

you have no idea how much i appreciate you help..

I was a bit confused by your idea of a GUI. did you mean an actual GUI, or showing the message by a JOptionPane.showMessageDialog(null, "message");
?

Edited 5 Years Ago by stultuske: corrected typo

showing the message by JOptionPane.showMessageDialog(null, "messaege");

i am sorry for the confusion..

well .. a program like that, I think you can finish in a matter of minutes rather than hours, even without much experience or knowledge of the language.

can you show me what code you have so far?

niice...

umm thats all i have...i was researching on how to implement the random method..

import javax.swing.JOptionPane;

public class GUI {
public static void main(String[]args) {
//Display Message
    JOptionPane.showMessageDialog(null, "Message1 ");
    JOptionPane.showMessageDialog(null, "Message2 ");
    JOptionPane.showMessageDialog(null, "Message3 ");
    
    
}

}

well ... you would have to do something like this:

import javax.swing.JOptionPane;


public class ShowRandomMessage {
    /**
     * This is the main method, which will show a random message every time you run the application.
     * @param args 
     */
    public static void main(String[] args){
        String[] messages = {"message1","message2","message3"};
        JOptionPane.showMessageDialog(null,messages[getRandomIndex(messages.length)]);
        System.exit(0);        
    }
    /**
     * This is a method that generates a random index for the message array.
     * You will still need to add the logic to get the random index though :)
     * @param max the length of the array
     * @return an index for the array, [0 <> length-1]
     */
    public static int getRandomIndex(int max){
        return 1;
    }    
}

Thanks alooot....

i wanted to see the .jar file...but i dont have the dist folder...the project folder only has build, nbproject, src, manifest and build (xml document)?? where did i mess up?

well, go to the 'RUN' menu and choose the option 'Clean and build main project'.
If you do this, NetBeans will generate the dist folder with the .jar file inside.

do you know how to find the logic to generate the random index?

well, you can do the entire 'logic' in a single line :) so it's not hours work.

the example most people look at to find out how it works is 'Rolling dice'.
it gets a random value from 1 to 6 every time you roll the dice.

if you google for random + 'Rolling dice' you should find it easily. if you don't understand it, just post here what you've found and what questions you have about it.

no, it's the Math.random(); method you'll need to use. it returns a double between 0 and 1, so you'll need to do two things:

1. cast the result to an integer
since you'll need to give an integer as the index of the array

2. set the maximum value for the result
otherwise you would risk getting an arrayIndexOutOfBoundsException

Well ... I'll show you in a few examples:

System.out.println("random number = " + Math.random());

will print out a double value between 0 and 1
now, let's say you want to mimic the roll of a dice, getting values from the list
1,2,3,4,5,6
to do this, you'll get

System.out.println("random number = " + (6*Math.random()));

but, if you do this, you'll get results like:
4.267663905278624
because you still are returning double values.
so, you'll need to convert it to an integer value. You can do this as follows:

System.out.println("random number = " + (int)(6*Math.random()));

off course, there is still a flaw, if you want to get the correct number for a dice. remember, you do not need this if you're looking for a random index in your array, I'm just completing the example here :)

System.out.println("random number = " + (int)(6*Math.random()+1));

now, in the code I've shown you, you don't need to add 1, you'll just need to replace '6' by the value of the max parameter passed to the method.

i have this so far....but i am getting an error on line 17

package try1;


import javax.swing.JOptionPane; 
 
public class Try1 {
    
    public static void main(String[] args){
        String[] messages = {"message1","message2","message3", 
            "message4", "message5"};
        JOptionPane.showMessageDialog(null,messages[getRandomIndex(messages.length)]);
        System.exit(0);        
    }
 
    public static int getRandomIndex(int max){
        return 1;}
  System.out.println("Message " + (int)(5*Math.random()));
    }

if i understand this right...i replaced 6 with 5 because so far i have 5 messages right? so if i have about 100 messages...i would have 100 instead of 5? is that correct?

Edited 5 Years Ago by lashbandi: corrected typo

The messages.length returns an int. It tells you how many messages you have. If you replace the 5 here

System.out.println("Message " + (int)(5*Math.random()));

with your max value passed into the method you will not have to constantly update that number, as it will always be passed in with the new number of messages.

You would need to replace 5 with 100 if you had 100 messages, or you could pass in the value like I suggested, and then you could write more or less messages.

@stultuske doesn't casting as an int round the value?

Edited 5 Years Ago by sirlink99: n/a

hmmm...

but why do i do keep getting error for that line

System.out.println("Message " + (int)(5*Math.random()));

It is because you put your print statement after you returned a value. When you return it ignores everything after it.

i tried this..but i am still getting error on the same line..

package try1;


import javax.swing.JOptionPane; 
 
public class Try1 {
    
    public static void main(String[] args){
        String[] messages = {"message1","message2","message3", 
            "message4", "message5"};
        JOptionPane.showMessageDialog(null,messages[getRandomIndex(messages.length)]);
        System.exit(0);        
    }
    System.out.println("Message " + (int)(5*Math.random()));
    public static int getRandomIndex(int max){
        return 1;}
    
  
    }

this time on line 14...i wrote the system.print statement before the return statement..but still..same error.

Edited 5 Years Ago by lashbandi: n/a

you want to keep the print statement inside of the method, just move it above the return. I have modified your code a bit. I have also added in comments to help you.

package try1;


import javax.swing.JOptionPane; 
 
public class Try1 {
    
    public static void main(String[] args){
        String[] messages = {"message1","message2","message3","message4", "message5"}; // stores messages
        JOptionPane.showMessageDialog(null,messages[getRandomIndex(messages.length)]); // prints out one of the messages
        System.exit(0);        
    }
 
    public static int getRandomIndex(int max){	// gets which message to print out
    	int messagePrintNumber = (int)(max*Math.random()); // generates a random number in the array
    	
    	System.out.println("Message " + (messagePrintNumber + 1)); // tells you which message it is printing. the +1 is to account for the array beginning at 0
    	
    	return messagePrintNumber; // returns which message it should print out
    }
}

I have made it so that it uses the max value of the array in the random calculation.

Note that the random message generation is separate from the print statement. This way you can return the generated number.

Edited 5 Years Ago by sirlink99: n/a

i see...the comments are verry helpful...thanks a bunch...

is there a way to design the the message...i mean instead of the light blue color message...can i have red color?

i tried the GUI ok/cancel template in netbeans...but i was only successful in changing the OK button to a smiley face and delete the cancel button..

I really appreciate your help..

You would need to change the Look and Feel. I have not yet figured out how to do this, but here are some starting points:

http://www.java2s.com/Tutorial/Java/0240__Swing/CustomizingaJOptionPaneLookandFeel.htm
http://www.java2s.com/Tutorial/Java/0240__Swing/CustomizingaJOptionPaneLookandFeel.htm


I will see if I can find more, or figure it out. There are basic ones you can change it to, there are also ones you can download, but I do not know how you can implement those.

Maybe you could use something like this:
http://www.java.net/external?url=http://www.javootoo.com/

Have a look around that site. you can download quite a few of them.

awesome..thanks alot...i am gonna work on this now...

if you do figure something out....can you please let me know too...if you dont mind..

I have figured out how to make the inside change. Using this reference

http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html#available

and here is a list of all the possible areas to change (I did not find one for the external frame)

http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/_nimbusDefaults.html#primary

Also when it says new color(....) you can replace all of that with Color.red, or you can write the Hexadecimal for the color you want. You can get the codes from a chart like this

http://www.google.ca/imgres?imgurl=http://www.theodora.com/gif4/html_colors.gif&imgrefurl=http://immigration-usa.com/html_colors.html&h=842&w=599&sz=72&tbnid=br4guGs9dA9QNM:&tbnh=90&tbnw=64&prev=/search%3Fq%3Dhex%2Bcolor%2Bcodes%26tbm%3Disch%26tbo%3Du&zoom=1&q=hex+color+codes&hl=en&usg=__Q_d0E_EpxdvRWxzV9mf1Ck2wehU=&sa=X&ei=n0c_Ttv-HcSu0AH4mNG9Bw&ved=0CFsQ9QEwCQ&dur=134

You also want to put the codes with the UI before you make the JOptionPane

Edited 5 Years Ago by sirlink99: n/a

how would you apply this in the actual code?
for example if i want the text to be red...i will be using F70000 as the code for the color...where exactly do i need to start writing all the code??? is it going to be written within the code? or is there any other place where i need to do that?

another problem =(...since the first file was a try....i created a new project and copy pasted the code in it. I deleted the "Try1" file so i dont run into problems...The program compiles fine and i see the dist folder too..but the application wont run. Everytime I click on it..it doesnt do anything...=( what am i doing wrong here?

This question has already been answered. Start a new discussion instead.