This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java Micro Edition and the fly likes Can Someone Help with Reading from a text file in J2ME Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "Can Someone Help with Reading from a text file in J2ME" Watch "Can Someone Help with Reading from a text file in J2ME" New topic
Author

Can Someone Help with Reading from a text file in J2ME

Diane McKeaveney
Greenhorn

Joined: Nov 21, 2003
Posts: 8
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 diane.mckeaveney@ntlworld.com
Michael Yuan
author
Ranch Hand

Joined: Mar 07, 2002
Posts: 1427
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.


Seam Framework: http://www.amazon.com/exec/obidos/ASIN/0137129394/mobileenterpr-20/
Ringful: http://www.ringful.com/
Fred Grott
Ranch Hand

Joined: Oct 05, 2002
Posts: 346
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..


MobileBytes blog - Sharing Technology - My Programming Knols
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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...


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Diane McKeaveney
Greenhorn

Joined: Nov 21, 2003
Posts: 8
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
Ranch Hand

Joined: Mar 07, 2002
Posts: 1427
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

Joined: Nov 21, 2003
Posts: 8
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 ???
Pavlin Mihalev
Greenhorn

Joined: Apr 29, 2003
Posts: 28
Diane,
you could pass the substrings to
public static int parseInt(String s)
method of java.lang.Integer)
Hope this helps


------<br />www.beiks.net
Michael Yuan
author
Ranch Hand

Joined: Mar 07, 2002
Posts: 1427
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.
David Price
Ranch Hand

Joined: Jan 22, 2003
Posts: 93
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

Joined: Nov 21, 2003
Posts: 8
Thanks a million for ur help guys i think im semi sorted!!!
Cheers
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can Someone Help with Reading from a text file in J2ME
 
Similar Threads
sending mail using servlet
Access Dbase with Http connection
I have 3 midlets but code to be displayed should be in first midlet .
HOW TO COMMUNICATE JAVA APPLICATION TO SERVLETS
How to send bytearray from Servlet to Midlet application?