-1

I need some help reading data from a text file into my ArrayList. The first part with the creating and putting the ArrayList into the text file works perfectly. I just need some help at the end in the "marked" area.

Note that some identifiers are in my native language.

public class ContAngajat {   
   String username;
   String password;
}

public class CreazaCont {

// creating the arraylist and putting it into a file 

public static void  ang(String args[])   { 

    ArrayList<ContAngajat> angajati=new ArrayList<ContAngajat>(50);

 Scanner diskScanner = new Scanner(in);

 Scanner forn = new Scanner(in);


 int n;

     out.print("Introduceti numarul de conturi noi care doriti sa le introduceti: ");
     n=forn.nextInt();
  out.println();

     try{

    FileWriter fw = new FileWriter("ConturiAngajati.txt", true);


    for(int i=0; i<n; i++){
  ContAngajat cont = new ContAngajat();

  out.print("Username: ");
  cont.username=diskScanner.nextLine();


  out.print("Password: ");
  cont.password=diskScanner.nextLine();

  angajati.add(cont);

  fw.write(cont.username + " ");
  fw.write(cont.password +"|");


  }
  fw.close();
     }
     catch(IOException ex){

      System.out.println("Could not write to file");

      System.exit(0);
     }




 for (int i=0; i<n; i++) {
  out.println("username: " + angajati.get(i).username + "  password: " +angajati.get(i).password );

 }


  }


// HERE I'M TRING TO GET THE ARRAYLIST OUT OF THE FILE

public static void  RdAng(String args[])   { 

 ArrayList<ContAngajat> angajati=new ArrayList<ContAngajat>(50);
 ContAngajat cont = new ContAngajat();
 int count,i2,i;

 try{


   FileReader fr = new FileReader("ConturiAngajati.txt");
   BufferedReader br = new BufferedReader(fr);

   String line = "";


   while((line=br.readLine())!=null) {

   String[] theline=line.split("|"); 
   count=theline.length;

   for(i=0;i<theline.length;i++) {

   String[] theword = theline[i].split(" "); 
  }     

   }   

   for(i2=0;i2<count;i2++)  {

   ContAngajat contrd = new ContAngajat();

// "ERROR" OVER HERE 

 for (int ird=0; ird <theword.length; ird++) {  

 cont.username=theword[0];
     cont.password=theword[1];

// they keep telling me "theword cannot be resolved" whenever i try to run this

}
       angajati.add(contrd); 
 }

 } 


 catch(IOException ex){

      System.out.println("Could not read to file");

      System.exit(0);
     }
}


}

The compilation error is theword cannot be resolved.

3
  • 3
    your code looks really bad aesthetically. try to use a code formatter (most IDEs comes with it) to format it. it's a pain to read. Commented May 5, 2010 at 22:09
  • Yeah, you gotta clean up this code... it's too hard to read Commented May 5, 2010 at 22:13
  • 2
    This code is terrible. I think learning to structure your code before your write programs is the best thing I can recommend. Programs need to be structured into different methods, components, etc. There are also usually "good ways to do things" and "bad ways to do things". This code has a lot of "bad ways to do things". Some of is not your fault. Universities are terrible at teaching java. Others are just problems with earlier versions of the language and older books. Try to learn how to structure your code. Commented May 5, 2010 at 22:46

1 Answer 1

2

they keep telling me "theword cannot be resolved" whenever I try to run this

This means that theword is not declared in the scope. You cannot access it to invoke any methods. They have it right. You need to declare theword in a broader scope, or to move the code which depends on theword into the scope where it is been declared. Maybe you've declared it inside an if or while block or so and you're trying to use it outside the block where it is been declared. That ain't going to work.

A more detailed answer may be given whenever you clean up the code so that it's better readable.

Sign up to request clarification or add additional context in comments.

2 Comments

so i declared theword out of the while and it worked . so that's not the problem anymore... :) now it says : Exception in thread at cont.username=theword[0]; thank you for the answear!
You're welcome. If you stucks with the new problem, just ask a new question. Don't forget to format your code properly (Ctrl+Shift+F in for example Eclipse will do) and include the entire stacktrace of the exception and point the line in code where it's been caused.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.