Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
SirSaula
Posts: 30
Registered: ‎04-13-2010
My Device: N/A
My Carrier: T-Mobile

Attaching a text file with program and making the next button generate a new file

[ Edited ]

Instead of having my sexfacts() function is there a way i can have a text file in which it reads and outputs a random fact everytime the Next button is pushed?


Issues

1. Using a textfile instead of the sexfacts() function

2. Making it generate a new fact when the NEXT button is pushed

 

 

import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.MainScreen;
import java.util.Random;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.*;  
 





public class app extends UiApplication
{
public static void main(String[] args)
{
app theApp = new app();
theApp.enterEventDispatcher();



}

public app()
{
    pushScreen(new sexfactsScreen());
}

}





final class sexfactsScreen extends MainScreen
{
public sexfactsScreen()
{
    super();
    
    
    
    
    LabelField title = new LabelField("Facts by Baba Akinlolu",
    LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
    setTitle(title);
    ButtonField jbtNEXT = new ButtonField("NEXT");

    //Random number generator
    Random dice = new Random();
    int num = 0;
    for (int counter=1; counter<=10; counter++){
        num = dice.nextInt(3);
    }
    
    add(new RichTextField(sexfacts(num))); // add the string to the screen
    add(jbtNEXT); // add the next button to the screen
    

    
    FieldChangeListener customListener = new FieldChangeListener() {   
        public void fieldChanged(Field field, int context) {       
    
    
        //Dialog.alert("Success!!! You clicked the Custom Button!!!");  

       }   
        };   
          

        jbtNEXT.setChangeListener(customListener);  
        
    

}







//Sexfacts info

String sexfacts(int x)
{
    String result = "Hey";
    if (x == 0) {
        result = "Neo";
    }
    else if (x == 1){
        result = "Anthony";        
    }
    
    else if(x == 2){
        result = "Baba";
    }
    return result;
    
}


}

 

 

Thanks,
Saula
Your neighborhood programmer.
http://homeworkjava.com
Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: Attaching a text file with program

You mean like a self extract archive in WIndows?  The answer, for BlackBerry, is no. 

 

However tell us what you are trying to achieve, perhaps we can suggest an alternative approach?

Please use plain text.
Contributor
SirSaula
Posts: 30
Registered: ‎04-13-2010
My Device: N/A
My Carrier: T-Mobile

Re: Attaching a text file with program

I just clarified my question better, and added the code. Please re-read it.

Thanks,
Saula
Your neighborhood programmer.
http://homeworkjava.com
Please use plain text.
New Developer
kanerm92
Posts: 18
Registered: ‎02-04-2010
My Device: Blackberry 8330
My Carrier: verizon

Re: Attaching a text file with program and making the next button generate a new file

this is a snippet of some code i use to read in from a txt file saved in the project folder. If you play with the empty for loop a bit you should be able to get your application to read one line of the file at a time based on the user pressing a button. hope this helps

add(_fieldManagerMiddle);
        try {

        	Class classs = Class.forName(this.getClass().getName());

        	InputStream is = classs.getResourceAsStream("flat.txt");

            LineReader lineReader = new LineReader(is);
               
            
            for(;;){
     		   String line = new String(lineReader.readLine());

     		   if(!line.equals("EOF")) {
     			  _line = new RichTextField(line);
              	_fieldManagerMiddle.add(_line);
     		   }
     		   else {
     		      break;
     		   }
     		}
          
            
        } catch(Exception ex) {

            System.out.println("Error: " + ex.toString());

        }

 

Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Attaching a text file with program and making the next button generate a new file

  1. Write a text file (let's call it sexfacts.txt), one fact per line.
  2. Add the file to your project by copying it into the source folder tree.
  3. Code your screen something like this:
final class sexfactsScreen extends MainScreen {
    sexfactsScreen() {
        // ...
    }

    //Sexfacts info
    String sexfacts(int x) {
        String result;
        try {
            result = facts[x];
        } catch (ArrayIndexOutOfBoundsException e) {
            result = "Hey";
        }
        return result;
    }

private static String[] facts;
// initialize facts using a static initializer:
static { Class c = sexfactsScreen.class; InputStream is = c.getResourceAsString("/sexfacts.txt"); if (is != null) { LineReader rdr = new LineReader(is); Vector v = new Vector(); boolean eof = false; while (!eof) { try { byte[] line = rdr.readLine(); v.addElement(new String(line)); } catch (EOFException e) { eof = true; } catch (IOException e) { // something bad happened
 } } try {
is.close();
} catch (IOException ignored) {
}
facts = new String[v.size()]; v.copyInto(facts); } else { // could not find resource file
 } } }

 

The static block will be executed when the class is first loaded (probably when "new sexfactsScreen()" executes). You can (and perhaps should) move this initialization code into a method that you call at a strategic point, instead of using a static initializer like this. You might also want to do this initialization in a separate thread, rather than adding to the start-up work for your app.




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
Contributor
SirSaula
Posts: 30
Registered: ‎04-13-2010
My Device: N/A
My Carrier: T-Mobile

Re: Attaching a text file with program and making the next button generate a new file

[ Edited ]

I use Eclipse 3.4.1

What do I have to import in order for it to work?

 

I tried a few imports but when I ran the file in BlackBerry 8300 Simulator the screen became blank

 

Also the

c.getResourceAsString"\sexfacts.txt"

 was not working for some reason so I changed it to

c.getResourceAsStream"\sexfacts.txt"

 

import net.rim.device.api.io.LineReader;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.MainScreen;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;
import java.util.Vector;
import net.rim.device.api.ui.component.ButtonField; 
import net.rim.device.api.ui.*;  

public class app extends UiApplication
{
public static void main(String[] args)
{
app theApp = new app();
theApp.enterEventDispatcher();



}

public app()
{
    pushScreen(new sexfactsScreen());
}

}



final class sexfactsScreen extends MainScreen {
    sexfactsScreen() {
        // ...
    }

    //Sexfacts info
    String sexfacts(int x) {
        String result;
        try {
            result = facts[x];
        } catch (ArrayIndexOutOfBoundsException e) {
            result = "Hey";
        }
        return result;
    }

    private static String[] facts;

    // initialize facts using a static initializer:
    static {
        Class c = sexfactsScreen.class;
        InputStream is = c.getResourceAsStream("/sexfacts.txt");
        if (is != null) {
            LineReader rdr = new LineReader(is);
            Vector v = new Vector();
            boolean eof = false;
            while (!eof) {
                try {
                    byte[] line = rdr.readLine();
                    v.addElement(new String(line));
                } catch (EOFException e) {
                    eof = true;
                } catch (IOException e) {
                    // something bad happened
                }
            }
            try {
                is.close();
            } catch (IOException ignored) {
            }
            facts = new String[v.size()];
            v.copyInto(facts);
        } else {
            // could not find resource file
        }
    }
}

 

 

Thanks,
Saula
Your neighborhood programmer.
http://homeworkjava.com
Please use plain text.
Contributor
SirSaula
Posts: 30
Registered: ‎04-13-2010
My Device: N/A
My Carrier: T-Mobile

Re: Attaching a text file with program and making the next button generate a new file

@kanerm92 what is the import used for your code? _filedManagerMiddle and _line keep causing errors

Thanks,
Saula
Your neighborhood programmer.
http://homeworkjava.com
Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Attaching a text file with program and making the next button generate a new file

Um, when I wrote the constructor:

 

 

sexfactsScreen() {
    // ...
}

 

I meant that you should keep what you had in your first post. Try that and I think you'll see an improvement. :smileyhappy:

 




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
Contributor
SirSaula
Posts: 30
Registered: ‎04-13-2010
My Device: N/A
My Carrier: T-Mobile

Re: Attaching a text file with program and making the next button generate a new file

oh lol, Well I did just that and  caught a java.lang.NullPointer Exception :-x

 

 

import net.rim.device.api.io.LineReader;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.MainScreen;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;
import java.util.Vector;
import net.rim.device.api.ui.component.ButtonField; 
import net.rim.device.api.ui.*;  
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.Field;


public class app extends UiApplication
{
public static void main(String[] args)
{
app theApp = new app();
theApp.enterEventDispatcher();



}

public app()
{
    pushScreen(new sexfactsScreen());
}

}





final class sexfactsScreen extends MainScreen
{
 sexfactsScreen()
{
    super();
    
    
    
    
    LabelField title = new LabelField("Facts by Baba Akinlolu",
    LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
    setTitle(title);
    ButtonField jbtNEXT = new ButtonField("NEXT");

    //Random number generator
    Random dice = new Random();
    int num = 0; 
    for (int counter=1; counter<=10; counter++){
        num = dice.nextInt(3);
    }
    
    add(new RichTextField(sexfacts(num))); // add the string to the screen
    add(jbtNEXT); // add the next button to the screen
    

    
    FieldChangeListener customListener = new FieldChangeListener() {   
        public void fieldChanged(Field field, int context) {       
    
    
        //Dialog.alert("Success!!! You clicked the Custom Button!!!");  

       }   
        };   
          

        jbtNEXT.setChangeListener(customListener);  
         
    

}







//Sexfacts info
String sexfacts(int x) {
    String result;
    try {
        result = facts[x];
    } catch (ArrayIndexOutOfBoundsException e) {
        result = "Hey";
    }
    return result;
}

private static String[] facts;

// initialize facts using a static initializer:
static {
    Class c = sexfactsScreen.class;
    InputStream is = c.getResourceAsStream("/sexfacts.txt");
    if (is != null) {
        LineReader rdr = new LineReader(is);
        Vector v = new Vector();
        boolean eof = false;
        while (!eof) {
            try {
                byte[] line = rdr.readLine();
                v.addElement(new String(line));
            } catch (EOFException e) {
                eof = true;
            } catch (IOException e) {
                // something bad happened
            }
        }
        try {
            is.close();
        } catch (IOException ignored) {
        }
        facts = new String[v.size()];
        v.copyInto(facts);
    } else {
        // could not find resource file
    }
}
}

 

 

Thanks,
Saula
Your neighborhood programmer.
http://homeworkjava.com
Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Attaching a text file with program and making the next button generate a new file

My guess is that c.getResourceAsStream("/sexfacts.txt") is returning null. Check that the text file is actually being included as a resource. (Look inside the .jar file with something like WinZip or 7-Zip; it should be in the root folder of the archive.)




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.