04-13-2010 12:27 PM - edited 04-13-2010 03:31 PM
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;
}
}
04-13-2010 02:03 PM
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?
04-13-2010 03:21 PM
I just clarified my question better, and added the code. Please re-read it.
04-13-2010 03:45 PM
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());
}
04-13-2010 05:49 PM
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.
04-14-2010 03:50 PM - edited 04-14-2010 03:52 PM
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
}
}
}
04-14-2010 04:02 PM
@kanerm92 what is the import used for your code? _filedManagerMiddle and _line keep causing errors
04-14-2010 04:31 PM
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. ![]()
04-14-2010 04:42 PM
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
}
}
}
04-14-2010 06:02 PM
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.)