AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problemi con Liste e Iteratori

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java (Italian)
View previous topic :: View next topic  
Author Message
Jeffo
Guest





PostPosted: Mon Feb 27, 2006 9:12 pm    Post subject: Problemi con Liste e Iteratori Reply with quote



Salve, posto il seguente codice dal quale non riesco a venirne fuori.

Le riga incriminate sono segnate con --->.

public class CSVSimple {
public static void main(String[] args) {
CSV parser = new CSV(';');
//...checks on aFile are elided
StringBuffer contents = new StringBuffer();

//declared here only to make visible to finally clause
BufferedReader input = null;
try {
//use buffering
//this implementation reads one line at a time
//FileReader always assumes default encoding is OK!
input = new BufferedReader( new FileReader("c:/test.txt") );
String line = null; //not declared within while loop
List content= new ArrayList();
List list = new ArrayList();

content.clear();
list.clear();
while (( line = input.readLine()) != null){
list = parser.parse(line);
(1)---> content.add(list);
}

Iterator it = content.iterator();

while (it.hasNext()) {
list =(ArrayList)it.next();
Iterator it1 = list.iterator();
while (it1.hasNext()) {
(2) --> System.out.print(it1.next());
}

}
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (IOException ex){
ex.printStackTrace();
}
finally {
try {
if (input!= null) {
//flush and close both "input" and its underlying FileReader
input.close();
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}

}
}

class CSV {

public static final char DEFAULT_SEP = ',';

/** Construct a CSV parser, with the default separator (`,'). */
public CSV() {
this(DEFAULT_SEP);
}

/** Construct a CSV parser with a given separator.
* @param sep The single char for the separator (not a list of
* separator characters)
*/
public CSV(char sep) {
fieldSep = sep;
}

/** The fields in the current String */
protected List list = new ArrayList();

/** the separator char for this parser */
protected char fieldSep;

/** parse: break the input String into fields
* @return java.util.Iterator containing each field
* from the original as a String, in order.
*/
public List parse(String line)
{
StringBuffer sb = new StringBuffer();
list.clear(); // recycle to initial state
int i = 0;

if (line.length() == 0) {
list.add(line);
return list;
}

do {
sb.setLength(0);
if (i < line.length() && line.charAt(i) == '"')
i = advQuoted(line, sb, ++i); // skip quote
else
i = advPlain(line, sb, i);
list.add(sb.toString());
i++;
} while (i < line.length());

return list;
}

/** advQuoted: quoted field; return index of next separator */
protected int advQuoted(String s, StringBuffer sb, int i)
{
int j;
int len= s.length();
for (j=i; j<len; j++) {
if (s.charAt(j) == '"' && j+1 < len) {
if (s.charAt(j+1) == '"') {
j++; // skip escape char
} else if (s.charAt(j+1) == fieldSep) { //next delimeter
j++; // skip end quotes
break;
}
} else if (s.charAt(j) == '"' && j+1 == len) { // end
quotes at end of line
break; //done
}
sb.append(s.charAt(j)); // regular character.
}
return j;
}

/** advPlain: unquoted field; return index of next separator */
protected int advPlain(String s, StringBuffer sb, int i)
{
int j;

j = s.indexOf(fieldSep, i); // look for separator
if (j == -1) { // none found
sb.append(s.substring(i));
return s.length();
} else {
sb.append(s.substring(i, j));
return j;
}
}
}
Scusate se ho postato tutto il codice, e' solo per fornire tutta la
classe che tra l'altro puo' tornare utile anche ad altri.

Si tratta di un parser CSV customizzabile rispetto al carattere separatore.
Dato per assodato che la (1) funziona correttamente non capisco come mai
quella doppia iterazione (2) mi stampi sempre la stessa riga.


Grazie,
Jeffo
Back to top
pb
Guest





PostPosted: Tue Feb 28, 2006 9:12 am    Post subject: Re: Problemi con Liste e Iteratori Reply with quote



Direi che infili nella "lista di liste" sempre lo stesso oggetto (la lista
che ti dà il parser, che è sempre la stessa). solo che la prima volta ha un
certop contenuto, poi ogni volta lo cambi... quindi nella lista di liste
avrai n volte il contenuto dell'ultima riga analizzata... chiaro?
(se fosse un set ordinato di liste invece di una lista di liste per esempio
avresti solo un elemento... )


"Jeffo" <JeffoNOSPAM (AT) email (DOT) it> ha scritto nel messaggio
news:440366fd$0$12593$4fafbaef (AT) reader3 (DOT) news.tin.it...
Quote:
Salve, posto il seguente codice dal quale non riesco a venirne fuori.

Le riga incriminate sono segnate con --->.
Back to top
Jeffo
Guest





PostPosted: Tue Feb 28, 2006 7:12 pm    Post subject: Re: Problemi con Liste e Iteratori Reply with quote



pb ha scritto:
Quote:
Direi che infili nella "lista di liste" sempre lo stesso oggetto (la lista
che ti dà il parser, che è sempre la stessa). solo che la prima volta ha un
certop contenuto, poi ogni volta lo cambi... quindi nella lista di liste
avrai n volte il contenuto dell'ultima riga analizzata... chiaro?
(se fosse un set ordinato di liste invece di una lista di liste per esempio
avresti solo un elemento... )


"Jeffo" <JeffoNOSPAM (AT) email (DOT) it> ha scritto nel messaggio
news:440366fd$0$12593$4fafbaef (AT) reader3 (DOT) news.tin.it...
Salve, posto il seguente codice dal quale non riesco a venirne fuori.

Le riga incriminate sono segnate con --->.


Non era quello comunque grazie ho risolto.

Il problema era la variabile dichiarata protected.

Grazie lo stesso.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java (Italian) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.