• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Can Someone Help with Reading from a text file in J2ME

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need help in doing the following - i must have a log in screen and when the user name and password is entered (hey are always same length) 10 for username and 2 for password i connect to a text file on the net and then check the username against the password. At the minute all i can do is read the entire text file in. Would someone please give me some help in this ?? I have searched the net all day and cant find a thing. The code so far is:
/*
* ProjectMIDlet.java
*
* Created on 20 November 2003, 14:33
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.io.*;
/**
*
* @author Diane McKeaveney
* @version
*/
public class ProjectMIDlet extends MIDlet implements CommandListener{

private Display display;
String url="http:www.cs.qub.ac.uk/~A.Bouridane/marks.txt";
private TextField username;
private TextField password;
private Form form;
private Command cancel;
private Command login;

public ProjectMIDlet(){

username=new TextField("LoginID: ", "",10,TextField.ANY);
password=new TextField("Password: ", "",10,TextField.PASSWORD);
form=new Form("Log In");
cancel=new Command("Cancel", Command.CANCEL,2);
login=new Command("Login",Command.OK,2);


}

public void startApp() {
display=Display.getDisplay(this);
form.append(username);
form.append(password);
form.addCommand(cancel);
form.addCommand(login);
form.setCommandListener(this);
display.setCurrent(form);


}

void gettheinput()
{
try{
getViaStreamConnection(url);
}
catch(IOException e){
System.out.println("IOException "+e);
e.printStackTrace();
}
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

void getViaStreamConnection(String url) throws IOException{
StreamConnection c=null;
InputStream s=null;
StringBuffer b=new StringBuffer();
TextBox t=null;
try{
c=(StreamConnection)Connector.open(url);
s=c.openInputStream();
int ch;
while ((ch=s.read())!=-1){
b.append((char) ch);
}

// System.out.println(b.getChars(1,10,char[]name, 0));

System.out.println(b.toString());
t=new TextBox("Assignment 1 Student Marks", b.toString(), 1024, 0);
}finally{
if(s!=null){
s.close();
}
if(c!=null){
c.close();
}
}
display.setCurrent(t);

}

public void commandAction(Command c, Displayable d){
String label=c.getLabel();
if(label.equals("Cancel")){
destroyApp(true);
}else if (label.equals("Login")){
gettheinput();
}
}
}
Please email me at [email protected]
 
author
Posts: 1436
6
Python TypeScript Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instead of reading the entire password file from the server, try sending the user input values to the server and validate it on the server using a servlet.
 
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Michael is right you are trying to do it all on client side which will create performance problems
Use a servlet to validate the user input then pass back a boolean type of response back to client..
Make sure to thread on the client side as you will have verying degree of latency depending upon which handset series and operator network you are dealign with..
The thread article is at wireless.java.sun.com
the other thing I saw is increase the apssword beyond 2 characters..
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Fred Grott:
Michael is right you are trying to do it all on client side which will create performance problems

Not to forget the lack of security...
 
Diane McKeaveney
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im in university and servlets are not something we have been taught 0- we just have to use this using the 3 classes StremCOnnection InputStream and StringBuffer
Can you guys help me in a way that doesn;t use servlets ??
Thanks
 
Michael Yuan
author
Posts: 1436
6
Python TypeScript Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Diane McKeaveney:
Im in university and servlets are not something we have been taught 0- we just have to use this using the 3 classes StremCOnnection InputStream and StringBuffer
Can you guys help me in a way that doesn;t use servlets ??
Thanks


If this is what your professor asked you to do, then your read-in-the-entire-password-file approach is the only way. But if I were you, I'd call the professor to make sure I understand the requirements correctly.
 
Diane McKeaveney
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes i have checked with him he wants us to read in the whole file then work from there
i am currently using substrings but i cant seem to convert a string to an integer to get a student mark into an int so i can perform a calcluation on it can u guys help ???
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Diane,
you could pass the substrings to
public static int parseInt(String s)
method of java.lang.Integer)
Hope this helps
 
Michael Yuan
author
Posts: 1436
6
Python TypeScript Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pavlin Mihalev:
Diane,
you could pass the substrings to
public static int parseInt(String s)
method of java.lang.Integer)


I do not think this is the solution Diane asked for. The parseInt() method only parses strings that represents integers (like "2", "10" etc.) I do not think the "password file" necessarily takes that format. I think Diane's problem is to "encode" an arbitary string into a byte representation and then use binary/hex masks to do fast look up. There are several ways to do that. I'd recommend checking a good basic computer science algorithm text book.
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Michael, you can actually check Diane's password file, the URL is in the source code she provided. It looks like this:
ID|StudentName|Pw|Subject1|Subject2|Subject3|�
1|smithjohn|sj|54|56|67|�
2|rankinian|ri|22|43|67|�
3|holmesjon|jh|99|99|99|�
#
So Pavliv's answer was right: use String's method 'substring' to get the right part of the line (e.g. "54") then use Integer.parseInt(String) to turn that into an int.
 
Diane McKeaveney
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a million for ur help guys i think im semi sorted!!!
Cheers
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic