Click here to Skip to main content
15,886,056 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is a Java ME question. I am using Netbeans 7.0. The setting is CLDC 1.1, MIDP 2.0, DefaultCldcJtwiPhone1, my cell is a Nokia 5130 Express Music. I did this code without any loops. Still this code not working with any loops, AND when the Random function is call, all display the same numbers ( except in my cell, the "lotoMax" show different numbers but not the "6/49". Go ahead, try it this code, and "select" 6/49 or lotoMax, place a loop and try again, still not working right. the code will compile & run , BUT not run well. I know I am a NooB here, I need some help and tell me Why the loop is not executed and the numbers not display right. I getting frustrated here.

My question : Why it did not work, How can I fix it and any help.

Here the code :

Java
package Lotteryselect;

import javax.microedition.lcdui.*;
import java.io.*;
import java.lang.*;
import java.util.*;
import javax.microedition.midlet.*;

/**
 * @author Serge J Desjardins
 */
public class LotteryselectMidlet extends MIDlet implements CommandListener 
{
      // display manager
    private Display display,display649,displaylotomax;
    
    // a menu with items
    private List menu; // main menu

    // textbox
    private TextBox input;
    
    // Form
     private Form form649,formlotomax;
    
    // variables
    int set649,setlotomax;
    int bal1,bal2,bal3,bal4,bal5,bal6;
    
    

    // command
    static final Command backCommand = new Command("Back", Command.BACK, 0);
    static final Command mainMenuCommand = new Command("Main", Command.SCREEN, 1);
    static final Command exitCommand = new Command("Exit", Command.STOP, 2);
    private String currentMenu;

    /*
     * Start the MIDlet by creating a list of items and associating the
     * exit command with it.
     */
    public void startApp() throws MIDletStateChangeException  

    {
    
        
      display = Display.getDisplay(this);

      menu = new List("Lottery Selection", Choice.IMPLICIT);
      menu.append("Lotto 6/49", null);
      menu.append("Lotto Max", null);
      menu.append("Item3", null);
      menu.append("Item4", null);
      menu.addCommand(exitCommand);
      menu.setCommandListener(this);

      mainMenu();    
    }
    
    public void pauseApp()
    {
        
    }
    
    public void destroyApp(boolean unconditional)
    {
       notifyDestroyed();  
    }
    
    // main menu
    void mainMenu() {
      display.setCurrent(menu);
      currentMenu = "Main"; 
    }

    /**
     * a generic method that will be called when selected any of
     * the items on the list.
     */
    public void prepare() {
       input = new TextBox("Enter some text: ", "", 5, TextField.ANY);
       input.addCommand(backCommand);
       input.setCommandListener(this);
       input.setString("");
       display.setCurrent(input);
    }

    /**
     * Test item1.
     */
    public void lotto649 ()
    { 
      
      int a,b,c,d,e,f,i,count;
      
      // int[] bal649 = new int[49];
      
      /*
      count=1;
      for (i=0;i>49;i++)
      {
        bal649[i]=count;
        count++;
      }
       */
   

      int[] bal649 = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,
                      21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,
                      38,39,40,41,42,43,44,45,46,47,48,49};
      
      a = new Random().nextInt(49);
      bal1=bal649[a];
       b = new Random().nextInt(49);
      bal2=bal649[b];
       c = new Random().nextInt(49);
      bal3=bal649[c];
       d = new Random().nextInt(49);
      bal4=bal649[d];
       e = new Random().nextInt(49);
      bal5=bal649[e];
       f = new Random().nextInt(49);
      bal6=bal649[f];
       
      form649 = new Form("6/49 Numbers");
      form649.append("\n\n\n");
      form649.append(bal1+" "+bal2+" "+bal3+" "+bal4+" "+bal5+" "+bal6);
           
      display649 = Display.getDisplay(this);
      display649.setCurrent(form649);
      form649.addCommand(backCommand);
      form649.setCommandListener(this);
           
      currentMenu = "Lotto 6/49";
    }
   
    /**
     * Test item2.
     */
    public void lottomax ()
    {
       
       formlotomax = new Form("Loto Max Numbers");
       formlotomax.append("\n\n\n");
       /*
       for (int i=0;i>6;i++)
       {
         setlotomax = new Random().nextInt(48);
         formlotomax.append(setlotamax+" ";
       }
       */

         setlotomax = new Random().nextInt(48);
         formlotomax.append(setlotomax+" ");
         setlotomax = new Random().nextInt(48);
         formlotomax.append(setlotomax+" ");
         setlotomax = new Random().nextInt(48);
         formlotomax.append(setlotomax+" ");
         setlotomax = new Random().nextInt(48);
         formlotomax.append(setlotomax+" ");
         setlotomax = new Random().nextInt(48);
         formlotomax.append(setlotomax+" ");
         setlotomax = new Random().nextInt(48);
         formlotomax.append(setlotomax+" ");
         
      displaylotomax = Display.getDisplay(this);
      displaylotomax.setCurrent(formlotomax);
      formlotomax.addCommand(backCommand);
      formlotomax.setCommandListener(this);
      
      currentMenu = "Lotto Max"; 
    }

    /**
     * Test item3.
     */
    public void testItem3()
    {
       prepare();
       currentMenu = "item3"; 
    }

    /**
     * Test item4.
     */
    public void testItem4()
    {
       prepare();
       currentMenu = "item4"; 
    }
    
   /**
    * Handle events.
    */  
   public void commandAction(Command c, Displayable d)
   {
      String label = c.getLabel();
      if (label.equals("Exit")) {
         destroyApp(true);
      } else if (label.equals("Back")) {
          if(currentMenu.equals("Lotto 6/49") || currentMenu.equals("Lotto Max") ||
             currentMenu.equals("item3") || currentMenu.equals("item4"))  {
            // go back to menu
            mainMenu();
          } 

      } else {
         List down = (List)display.getCurrent();
         switch(down.getSelectedIndex()) {
           case 0: lotto649 ();break;
           case 1: lottomax ();break;
           case 2: testItem3();break;
           case 3: testItem4();break;
         }
            
      }
  }
}
Posted
Updated 5-Oct-11 18:27pm
v2

Hm. Never saw such artistry before. You messed up all your loops to execute zero times.

Look what you tried to do:
Java
for (i=0; i>49; i++) {...}


Should be:
Java
for (i=0; i<49; i++) {...}


Now the body of the loop will be executed 48 times.

By the way, "49" is called immediate constant — very unsupportable. Imagine what happens if you need to change all numbers in all code. If you cannot make all things variable, makes them constants but explicit.

Now, you also had while loop? My imagination fails to guess what could you screw up with them, maybe something similar to for loop.

—SA
 
Share this answer
 
Adding to SA's answer, your biggest crime is that you create a new random object every time you want a random number.
In J2ME CLDC1.1:
Java
new Random().nextInt(48);
//is the same as
new Random(System.currentTimeMillis()).nextInt(48);

Hence, you got a random class with the same seed (since NetBeans's simulator run incredibly fast for this kind of operation) resulting same results. Whilst in your phone, the operation is carried in a slower manner (Opposite of graphics operation which run faster in phone) resulting different seeds and different results.

Now, hammer this into your head: doesn't matter what kind of application you make, create only one Random class and use it thorough the whole application.

More reading on random numbers: http://en.wikipedia.org/wiki/Pseudorandom_number_generator[^]

[Edit]
Just create a static public variable in your MIDlet, initialize in static constructor and use it every time you want a random number
Example code :
Java
public class LotteryselectMidlet extends MIDlet implements CommandListener 
{
    public static Random rand;

    static {
        rand = new Random();
    }

    //Example in the same class
    public void startApp() throws MIDletStateChangeException  
    {
        int i = rand.nextInt(48);
        int j = rand.nextInt(48);
    }
}
//Example in another class (should be in different file since declared public)
public class AnotherClass{
    public void MethodRandom(){
        int k = LotteryselectMidlet.rand.nextInt(48);
    }
}

The above code should give random results.
 
Share this answer
 
v3
@SAKryukov

Yep, I just notice my "rooky mistake" the i>49 to i<49. I hope it that simple. Sorry for the messy code.

@Firo Atrum Ventus

Thank for your insight and the link. Look like I have to re-design my code. The question is : How can I code it so it pick 6 different numbers ?
 
Share this answer
 
Comments
André Kraak 6-Oct-11 16:21pm    
If you have a question about or comment on the given solution use the "Have a Question or Comment?" option beneath the solution. When using this option the person who gave the solution gets an e-mail message and knows you placed a comment and can respond if he/she wants.

Please move the content of this solution to the solution you are commenting on and remove the solution. Thank you.
Firo Atrum Ventus 6-Oct-11 20:56pm    
See my updated answer, if you have any question please use comment (see André's comment)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900