The Artima Developer Community
Sponsored Link

Java Answers Forum
help with a statement

1 reply on 1 page. Most recent reply: Dec 31, 2002 3:22 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
ben

Posts: 57
Nickname: loftty
Registered: Oct, 2002

help with a statement Posted: Dec 31, 2002 7:02 AM
Reply to this message Reply
Advertisement
hello everyone i hope someone can help me
I have this code which prints out a line from the csv file but if you look at my code. Where it says if (i%10 == ((Integer)arr2.get(arr2.size()-1)).intValue())
this will read in the last item that is in the arraylist (arr2) by getting the integer value.
but i want it to read out all the values that are inside the arraylist arr2 how could i do this?
please could some one help me
thanks
ben

import java.io.*;
import java.util.*;
import java.lang.*;
import java.util.StringTokenizer.*;
import java.sql.*;

public class RT2 {

public static void main(String[] args) {
String token = null;
String currentToken = "";
Hashtable hh = new Hashtable();
ArrayList arr2 = new ArrayList();
String st1 = "ben";
String st2 = "ben"; //table names
String st3 = "alex";
String st4 = "alex";
String st5 = "ben";
String st6 = "matt";
String st7 = "mum";
String st8 = "mum";
String st9 = "dad";
String st10 = "ben";
try {
Vector v2 = new Vector(100);
Vector v = new Vector(100);
FileReader file = new FileReader("Work.csv");
BufferedReader buff = new BufferedReader(file);
String line = buff.readLine();
StringTokenizer s = null;
boolean eof = false;
while (!eof){
s = new StringTokenizer(line.trim(),",",true);
boolean lastToken = true;
while (s.hasMoreTokens()) {
token = s.nextToken();
if (lastToken){
if (token.equals(",")) {
v.add("");
}
lastToken = false;
}
if (! token.equals(",")) {
v.add(token);
}
else { if (currentToken.equals(",")) {
v.add("");

}
}
currentToken = token;
}
if (token.equals(",")) {
v.add("");
}

line = buff.readLine();
if (line == null)
eof = true;
}

hh.put(new Integer(1), st1);
hh.put(new Integer(2), st2);
hh.put(new Integer(3), st3);
hh.put(new Integer(4), st4);
hh.put(new Integer(5), st5);
hh.put(new Integer(6), st6);
hh.put(new Integer(7), st7);
hh.put(new Integer(8), st8);
hh.put(new Integer(9), st9);
hh.put(new Integer(10), st10);
if (hh.containsValue(st1)){
Enumeration e11 = hh.keys();
while (e11.hasMoreElements()) {
Object IT = e11.nextElement();
Object IT1 = hh.get(IT);
if (IT1.equals(st1)){
arr2.add(IT);
/*System.out.println(arr2);

Iterator e111 = arr2.iterator();
while (e111.hasNext()) {
Object IT11 = e111.next();*/

//}
}
}
}

Enumeration e = v.elements();
Object o;
for(int i = 1;e.hasMoreElements();i++) {
o = e.nextElement();

if (i%10 == ((Integer)arr2.get(arr2.size()-1)).intValue()){

System.out.println(o);
}

}



buff.close();
}catch (FileNotFoundException fe) {
System.out.println("Error - - " + fe.toString());
}catch (NumberFormatException ne) {
System.out.println("Error - - " + ne.toString());
} catch (IOException e) {
System.out.println("Error - - " + e.toString());



}
}
}


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: help with a statement Posted: Dec 31, 2002 3:22 PM
Reply to this message Reply
Then instead of getting the one item from the list, you should iterate through the list.

By the way, why are you using this hodgepodge of Vectors, ArrayLists and Hashtables? Your code will be much simpler and cleaner if you consolidate it to use just Lists and Maps. I would also stick with using Iterators instead of Enumerations.

Flat View: This topic has 1 reply on 1 page
Topic: Reading flat files from java which has no delimiters Previous Topic   Next Topic Topic: Problem with Applet

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use