Page 1 of 1 [ 8 posts ] 

lemon
Veteran
Veteran

User avatar

Joined: 27 Aug 2006
Age: 58
Gender: Female
Posts: 4,113
Location: belgium

14 Dec 2006, 6:12 am

yeah, i know i should post on a java-forum, but have tried... it takes so many time and i often don't understand the people

so if it's one chance out of thousand i might as well ask the question here...


i have to put data from a file into an array (everytime a new line is used and every line has to be put into the array), and i can't figure out how that works...






import java.io.*;
import java.io.BufferedReader;
public class lees_zonder_multiarray
{
public static void main(String [] args)throws IOException
{

int a,b,x ;
a=3;
b=100;
x=0;
String lijn;
String schrijf;
FileReader lezer= new FileReader("liedjes.txt");
BufferedReader haal =new BufferedReader(lezer);

try{
while ((lijn = haal.readLine()) != null){
schrijf=haal.write(lijn);
System.out.println ("regel"+ schrijf);

}
}
catch (IOException e) {
System.out.println("Fichier inconnu");
}
}
}


In fact it's with a two-dimensional array, but apparently it's not the array that doesn't function, i make a mistake in the reading (because the first exemple here above doesn't work either)

import java.io.*;


public class Projetdeuxx {
public static void main(String [] args) throws IOException
{


int a,b,n,i,j,k,x,choix,som,duree,nummer,lengte,h,m,v,z;//déclaration des intègres
char s='o';
//déclaration+initialisation charactère pour options 'oui' et 'non'
i=0;
j=3;
z=0;
v=0;
x=0;
k=1000;//initialisation
a=3;
b=1000;
choix=0;
lengte=0;
String lezer;
String lijn;
BufferedReader haal;
String [][] tab_woord;
String [][] tab_zoek; //model tableau pour mettre les données,lignes=artiste,titre,..,colonnes=les entrées
tab_woord = new String[j][k];
tab_zoek = new String[a][b];

//le tableau
{

try
{
for (b=0;b<1000;b++){


FileReader lezer = new FileReader("liedjes.txt");
haal = new BufferedReader(lezer);

while ((lijn = haal.readLine()) != null){
tab_zoek [0][b]= haal.write(lijn);
tab_zoek [1][b]= haal.write(lijn);
tab_zoek [2][b]= haal.write(lijn);
x++;
}
}



}



catch (IOException e) {
System.out.println("Fichier inconnu");
}




while ((s=='o')||(s=='O'))
{ for (k=0;k<1000;k++){
//créer un liste de choix,1e: afficher les choix
System.out.println ("1 Entrer un titre ");
System.out.println("2 Liste des titres");
System.out.println("3 Taille totale");
System.out.println("4 Quitter");
System.out.print("Voulez-vous entrer un nouveau titre (1), Consulter la liste (2), Connaître la taille totale (3) ou Quitter ?(4) ");
//2e: lire le valeur du choix

choix =Lire.i();

//pour les 4 options (choix) possibles, une autre action
switch(choix) {
case 1://pour insérer une nouvelle entrée dans la liste

System.out.print("Entrer le nom de l’artiste: ");
tab_woord[0][k]= Lire.S();
System.out.print("Entrer le titre de l'album ");
tab_woord[1][k]= Lire.S();
System.out.print("Entrer la taille ");
tab_woord[2][k]= Lire.S();
z++;
break;
case 2://pour afficher la liste

for (k=0;k<z;k++){
{
System.out.println("Artiste: "+tab_woord[0][k]);
System.out.println("Titre: "+tab_woord[1][k]);
System.out.println("Taille: "+tab_woord[2][k]);}
}
break;
case 3: //pour calculer et afficher la taille totale
som=0;
while (i<=z){
for (k=0;k<z;k++){
v=Integer.parseInt(tab_woord[2][k]);
som=som+v; //valeur de taille pour le i-ième entrée
i++;
}
duree=som/1024; //la totale en minutes
h=duree/60; // en heure
m=duree-(h*60); //le restant de minutes
System.out.println("Taille totale: "+som+"kb, durée: "+h+"h "+m+"m ");
break;
}
case 4: //pour quitter
s='n';
break;
default: //pour revenir au choix
System.out.print("Voulez-vous continuer ? O/N ");
s=Lire.c();
}
}
}
}
}
}



JJ
Toucan
Toucan

User avatar

Joined: 28 Aug 2006
Age: 40
Gender: Male
Posts: 262

14 Dec 2006, 8:54 am

It has been a long time since I've done Java... but... wouldn't it be something like:


import java.io.*;
import java.io.BufferedReader;
import java.util.Vector;

FileReader lezer= new FileReader("liedjes.txt");
BufferedReader haal =new BufferedReader(lezer);

Vector data;
String s;

do{
s = haal.ReadLine();
if(s != null) data.add(0, s);
} while(s != null);


---

I've used a Vector rather than an array, because arrays are of a set size, which is a pain....

Erm, hope I've been of some help ;) If you had asked how to do it in C++, I would have been ecstatic :)



JJ
Toucan
Toucan

User avatar

Joined: 28 Aug 2006
Age: 40
Gender: Male
Posts: 262

14 Dec 2006, 8:56 am

If it's not working, maybe it isn't finding the text file? the .txt has got to be in the same directory as the program. Or you could put the txt file at "C:\liedjes.txt" to be 100% certain.



lemon
Veteran
Veteran

User avatar

Joined: 27 Aug 2006
Age: 58
Gender: Female
Posts: 4,113
Location: belgium

14 Dec 2006, 9:35 am

i'm only a beginner you know, i don't know what vector is...


after a few hours i found out (and throwing away a few things) that this seems to work,
so i can load from a file and to proof it to myself i wrote the println , but i do not have to
println it , i have to store it in a two dimensional array...


import java.io.*;
import java.io.BufferedReader;
public class lees_zonder_array
{
public static void main(String [] args)throws IOException
{

int a,b,x ;
a=3;
b=100;
x=0;
String lijn;

{
System.out.print("Entrez le nom du fichier :");
String naam = Lire.S();
FileReader lezer= new FileReader(naam);
BufferedReader haal =new BufferedReader(lezer);

try{
while ((lijn = haal.readLine()) != null){

System.out.println (""+ lijn);


}
}
catch (IOException e) {
System.out.println("Fichier inconnu");
}
}
}
}



alex
Developer
Developer

User avatar

Joined: 13 Jun 2004
Age: 40
Gender: Male
Posts: 10,224
Location: Beverly Hills, CA

14 Dec 2006, 9:59 am

Code:
import java.io.*;
import java.io.BufferedReader;


You don't need both of those lines. The first line imports everything in io so the 2nd line is redundant. Not that it matters or anything....


_________________
I'm Alex Plank, the founder of Wrong Planet. Follow me (Alex Plank) on Blue Sky: https://bsky.app/profile/alexplank.bsky.social


lemon
Veteran
Veteran

User avatar

Joined: 27 Aug 2006
Age: 58
Gender: Female
Posts: 4,113
Location: belgium

14 Dec 2006, 10:46 am

that's what i had understood too but strangely it only worked with and not without,
so i had just let it be (maybe it would have helped if i saved, closed and re-openend it)

now going to see the professor and will have to admit that i haven't found it ...
but i don't really mind because i like it and can surely make my marks better with the final exam.



JJ
Toucan
Toucan

User avatar

Joined: 28 Aug 2006
Age: 40
Gender: Male
Posts: 262

14 Dec 2006, 12:08 pm

I like your art lemon :) especially "0-profond" .. I was looking at your website.

Anyway.. umm

Yeah sorry, with arrays, you set them with a length, but if you want to put another line of data in them then you have to ReDim I think. And multi-dimension array ReDim'ing gets a bit much, it's nasty.

Let us know how it goes



lemon
Veteran
Veteran

User avatar

Joined: 27 Aug 2006
Age: 58
Gender: Female
Posts: 4,113
Location: belgium

14 Dec 2006, 5:23 pm

thanks! (you can vote for one of my paintings in the art,music thread...)

well, i went to class today (i almost never do, because i'm a mum too and we live more than 100 km from university)

the bad news is i got it all wrong (or the conception of the whole thing is wrong, apparentely i have to write methods and call them) , the good news is that they gave me a few more days and an exemple of these methods...
and i could ask questions by e-mail (and i now really want to know how to put data from a file into an array). so i'm saved !

thanks for passing by anyway ! !