Welcome to Emulationworld

Forum Index | FAQ | New User | Login | Search

Make a New PostPrevious ThreadView All ThreadsNext Thread*Show in Threaded Mode


SubjectOk, a plea for help (Java) new Reply to this message
Posted byLordEvilElmo
Posted on01/02/04 07:59 AM



Right, so now I am working on a java project which is essentially useless (Ah, the joys of a first semester module!), it takes in data from a text file and then, using constructors and methods will output them to a pretty invoicing system (Which works fine, took about an hour). Now, the constructors etc. all work fine, but for the life of me I cannot get my String tokenizer to work correctly. It crashes on running (No IOException, my Dummy points both print out, so it is to do with the actual Variable assignment section, which is marked by a comment)

I have no idea what I've done wrong, but I hope you uber-leet types can help out! ;)

import java.io.*;
import java.util.*;

class Tokenizer {
public static void main (String[] args) {
Tokenizer tt = new Tokenizer();
tt.dbTest();
}

void dbTest() {
DataInputStream dis = null;
String dbRecord = null;

try {
System.out.println("Dummy Test. Error point 1");
File f = new File("Data.db");
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);

while ( (dbRecord = dis.readLine()) != null)
{
System.out.println("Dummy Test. Error point 2");
StringTokenizer st = new StringTokenizer(dbRecord, " ");
//I believe the problem lies in this section.
String one = st.nextToken();
String two = st.nextToken();
String three = st.nextToken();
String four = st.nextToken();

System.out.println("1:" + one);
System.out.println("2:" + two);
System.out.println("3:" + three);
System.out.println("4:" + four + "\n");
}

}

catch (IOException e)

{
System.out.println("Uh oh, got an IOException error: " + e.getMessage());
}

finally
{
if (dis != null) {
try {
dis.close();
}
catch (IOException ioe)
{
System.out.println("IOException error trying to close the file");
}

}

}

}

}


When twilight dims the skies above
Recalling thrills of our love
There's one thing I'm certain of
Return...
I will...
to old...
BRAZIL.



SubjectHere is the data file contents Reply to this message
Posted byLordEvilElmo
Posted on01/02/04 08:01 AM



Not the real one, just a dummy ;)


Data.txt
Homer Simpson Delaware 1982
Poo Poo Ha Plop
I am so smrt


When twilight dims the skies above
Recalling thrills of our love
There's one thing I'm certain of
Return...
I will...
to old...
BRAZIL.



SubjectRe: Ok, a plea for help (Java) new Reply to this message
Posted byMitaine
Posted on01/08/04 03:07 PM



I haven't done any programming in 3 years but according to sun's java API spec (http://java.sun.com/j2se/1.4.2/docs/api/), in the DataInputStream class description (http://java.sun.com/j2se/1.4.2/docs/api/java/io/DataInputStream.html) :

String readLine()
Deprecated. This method does not properly convert bytes to characters. As of JDK 1.1, the preferred way to read lines of text is via the BufferedReader.readLine() method. Programs that use the DataInputStream class to read lines can be converted to use the BufferedReader class by replacing code of the form:
DataInputStream d = new DataInputStream(in);
with:
BufferedReader d
= new BufferedReader(new InputStreamReader(in));

voilą (?)


SubjectThat worked :) Thank you! -nt- new Reply to this message
Posted byLordEvilElmo
Posted on01/08/04 03:47 PM



> I haven't done any programming in 3 years but according to sun's java API spec
> (http://java.sun.com/j2se/1.4.2/docs/api/), in the DataInputStream class
> description
> (http://java.sun.com/j2se/1.4.2/docs/api/java/io/DataInputStream.html) :
>
> String readLine()
> Deprecated. This method does not properly convert bytes to characters.
> As of JDK 1.1, the preferred way to read lines of text is via the
> BufferedReader.readLine() method. Programs that use the DataInputStream class to
> read lines can be converted to use the BufferedReader class by replacing code of
> the form:
> DataInputStream d = new DataInputStream(in);
> with:
> BufferedReader d
> = new BufferedReader(new InputStreamReader(in));
>
> voilą (?)
>


When twilight dims the skies above
Recalling thrills of our love
There's one thing I'm certain of
Return...
I will...
to old...
BRAZIL.



Previous ThreadView All ThreadsNext Thread*Show in Threaded Mode