4 Replies - 474 Views - Last Post: 18 January 2012 - 08:09 PM Rate Topic: -----

#1 generic_user_1234  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 18-January 12

how to get information from a arraylist to a text file and vice versa?

Posted 18 January 2012 - 07:45 PM

Hey guys hows its going?
Aight so i was asked to make a assigment for class, in which i am developing an library application. The thing is we are asked to use arrays! So basically we need to be able to add new users and delete them as well. So i got the add part to work by using write.file meathods and saving the information to a file. But i cant understand how do i delete the users and once i created a new user how do i get that user to log in with his new credentials? Guys i really need help in this, please guys! Um you can always email me if you think thats easier. My email is <Removed>, i didnt post my code online cause of the fact that its multiple classes rather than just one. Um if you have a blackberry add me on it if you think that way would be easier to help me, my bb pin is: <Removed>.
Please and thanks guys,
RJay

This post has been edited by macosxnerd101: 18 January 2012 - 08:01 PM
Reason for edit:: Removed personal contact information


Is This A Good Question/Topic? 1
  • +

Replies To: how to get information from a arraylist to a text file and vice versa?

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1275
  • View blog
  • Posts: 2,837
  • Joined: 05-April 11

Re: how to get information from a arraylist to a text file and vice versa?

Posted 18 January 2012 - 07:49 PM

You can post your code. Remember code tags.

Are you having a User class or something like that?
If you do, then you can always use ObjectOutputStream to write your objects to a file, and use an ObjectInputStream to read them in again :)
Was This Post Helpful? 0
  • +
  • -

#3 generic_user_1234  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 18-January 12

Re: how to get information from a arraylist to a text file and vice versa?

Posted 18 January 2012 - 07:58 PM

View PostCasiOo, on 18 January 2012 - 07:49 PM, said:

You can post your code. Remember code tags.

Are you having a User class or something like that?
If you do, then you can always use ObjectOutputStream to write your objects to a file, and use an ObjectInputStream to read them in again :)

ye actually i am. Here is all my code. The main method is in main.java

//**
 * @(#)addAccount.java
 *
 *
 * @author 
 * @version 1.00 2012/1/15
 */
import java.io.*;
import java.util.*;

public class addAccount {

    public void addAccount() {
    String firstName;
  	String lastName;
  	String userName;
  	String libraryCard;
   	Scanner input = new Scanner (System.in);
    System.out.print ("\nAdd a account page");
  	System.out.print ("\nEnter first name: ");
  	firstName = input.nextLine();
  	System.out.print ("\nEnter last name: ");
  	lastName = input.nextLine();
  	System.out.print ("\nEnter user name: ");
  	userName = input.nextLine();
  	System.out.print ("\nEnter library card number: ");
  	libraryCard = input.nextLine();
  	System.out.println ("\nUser succesfully created");
  try{	
  // Create file 
  File file =new File("C:\\Users\\RJay\\Narr\\Accounts.txt");
   FileWriter fileWritter = new FileWriter(file.getName(),true);
    BufferedWriter out = new BufferedWriter(fileWritter);
    out.write("\n" + firstName);
    out.newLine();
  out.write("\n" + lastName);
  out.newLine();
  out.write("\n" + userName);
  out.newLine();
  out.write ("\n" + libraryCard);
  //Close the output stream
  out.close();
  }catch (Exception e){//Catch exception if any
  System.out.println("Error: " + e.getMessage());
  }
  }
}



/ /**
 * @(#)addAccount.java
 *
 *
 * @author 
 * @version 1.00 2012/1/15
 */
import java.io.*;
import java.util.*;

public class addBooks {
 static int quantity = 0;
    public void addBooks() {
    String bookName;
  	String genre;
  	String summary;
  	String authors;
    
  	
   	Scanner input = new Scanner (System.in);
    System.out.print ("\nAdd a new book page");
  	System.out.print ("\nEnter book name: ");
  	bookName = input.nextLine();
  	System.out.print ("\nEnter genre: ");
  	genre = input.nextLine();
  	System.out.print ("\nEnter brief summary of the book: ");
  	summary = input.nextLine();
  	System.out.print ("\nEnter author(s): ");
    authors = input.nextLine();
    System.out.print ("\nEnter quantity: ");
    quantity = input.nextInt();
  	System.out.println ("\nBook succesfully added");
  try{	
  // Create file 
  File file =new File("C:\\Users\\RJay\\Narr\\Search.txt");
   FileWriter fileWritter = new FileWriter(file.getName(),true);
    BufferedWriter out = new BufferedWriter(fileWritter);
  out.write("\nBook Name: " + bookName);
  out.newLine();
  out.write("\nGenre: " + genre);
  out.newLine();
  out.write("\nSummary: " + summary);
  out.newLine();
  out.write ("\nAuthor(s): " + authors);
  out.newLine();
  out.write ("\nQuantity: " + quantity);
  out.newLine();
  //Close the output stream
  out.close();
  }catch (Exception e){//Catch exception if any
  System.out.println("Error: " + e.getMessage());
  }
  }
  public void removeBook(){
  	 	quantity--;
  	}

}



//**
 * @(#)adminLogin.java
 *
 *
 * @author 
 * @version 1.00 2012/1/15
 */

import java.util.*;

public class adminLogin {
Scanner input = new Scanner (System.in);
addAccount account = new addAccount ();
addBooks books = new addBooks ();
    public void  Login() {
        String userName;
    	String password;
    	System.out.println ("\nWelcome to the admin page");
    	System.out.println ("\n Please log in");
    	System.out.println ("\n Username: ");
     	userName = input.nextLine();
    	System.out.println (" Password: ");
    	password = input.nextLine();
    	if (userName.equalsIgnoreCase("Rahul") && password.equals("admin")){
    		System.out.println ("\nWelcome" );
    		options();
    	}else{
    		System.out.println ("\nSorry please try again");
    	}
} 
        public void options (){
        	String choice;
        	String addBook;
        	String removeBook;
        	String addAccount;
        	String removeAccount;
        	System.out.println ("\n Delete Account \\ Add Account \\ Remove Books \\ Add Books");
        	System.out.println ("\nPlease choose: ");
         	choice = input.nextLine();
        	if (choice.equalsIgnoreCase("Delete")){
        	     System.out.println ("What account do you want to remove?");
        	     addAccount = input.nextLine();
        	}else if (choice.equalsIgnoreCase("Add")){
                 System.out.println ("What account do you want to add?");
                // removeAccount = input.nextLine();
                account.addAccount();
        	}else if (choice.equalsIgnoreCase("Remove"))
        	{
        		System.out.println ("What book do you want to remove?");
        		removeBook = input.nextLine();
        	}else if (choice.equalsIgnoreCase("AddBooks"))
        	{
        	  books.addBooks();
                 
        	}
        }
    

}




/import java.io.*;
  public class fileRead
 {
  public void  search ()
  {
  
   try{
   // Open the file that is the first 
   // command line parameter
   FileInputStream fstream = new FileInputStream("C:\\Users\\RJay\\Desktop\\Narr\\Search.txt");
   // Get the object of DataInputStream
   DataInputStream in = new DataInputStream(fstream);
   BufferedReader br = new BufferedReader(new InputStreamReader(in));
   String strLine;
   //Read File Line By Line
   while ((strLine = br.readLine()) != null)   {
   // Print the content on the console
   System.out.println (strLine);
   }
   //Close the input stream
   in.close();
     }catch (Exception e){//Catch exception if any
   System.err.println("Error: " + e.getMessage());
   }
   }
 }



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

class filereader
{


public static void main(String args[]) throws Exception
   {
  
   Scanner input = new Scanner (System.in);
   FileReader fr=new FileReader("C:\\Users\\RJay\\Desktop\\Narr\\Search.txt");
   BufferedReader br=new BufferedReader(fr);
   String s;
  
   String userInput;
   int Counter =0;
   System.out.println ("What book would you like to search?");
   userInput = input.nextLine();
   while ((s=br.readLine())!=null)
{
	int indexfound=s.indexOf(userInput);
 	if (indexfound>-1)
{
	System.out.println("Book Found: ");
 	System.out.println(s);
}
}
 

   }
}



//**
 * @(#)Main.java
 *
 *
 * @author 
 * @version 1.00 2012/1/15
 */
import java.util.*;
import java.io.*;
public class Main {
        
        
        
    
    public static void main(String[] args) {
        String action;        
        Scanner input = new Scanner (System.in);
        adminLogin method = new adminLogin();
        userLogin login = new userLogin();
        fileRead search  = new fileRead();
        System.out.println ("Welcome to the NARR application");
        System.out.println ("\n Admin Login \\ User Login \\ Search a book");
        System.out.println ("\nPlease choose: \n ");
        action = input.nextLine();
        if (action.equalsIgnoreCase("admin")){
            method.Login();
            }else if (action.equalsIgnoreCase("user")) {
        login.Login();
       }else if (action.equalsIgnoreCase("search")){
       	search.search();
       }
       
        
        
        
    }
}



//**
 * @(#)takeOutBook.java
 *
 *
 * @author 
 * @version 1.00 2012/1/15
 */
import java.util.*;
import java.io.*;

public class takeOutBook {
Scanner input = new Scanner (System.in);
    
    
    public void takeOutBook() {
    	String choice;
    	addBooks name = new addBooks ();
    	System.out.println ("\nWelcome, we have a variety of books in our library");
     	totalBooks();
     	System.out.println ("So, which book would you like to take out?");
     	choice = input.nextLine();
     //	if (choice.equalsIgnoreCase(name.getBookName())){
     	//	System.out.println ("Ok we do, have this book");
     	//}else{
     	//	System.out.println ("Sorry we dont have this book");
     	}
     
     	
     	//if (){}//book availible
     //	name.removeBook();
     	
    
     public void totalBooks(){
    
    	try{	
   FileInputStream fstream = new FileInputStream("C:\\Users\\RJay\\Desktop\\Narr\\Search.txt");
   DataInputStream in = new DataInputStream(fstream);
   BufferedReader br = new BufferedReader(new InputStreamReader(in));
   String strLine;
   while ((strLine = br.readLine()) != null)   {
   System.out.println (strLine);
   }in.close();
    	}catch (Exception e){//Catch exception if any
   System.err.println("Error: " + e.getMessage());
   }
   
    }
    
}



//**
 * @(#)userLogin.java
 *
 *
 * @author 
 * @version 1.00 2012/1/15
 */

import java.util.Scanner;
public class userLogin {
	Scanner input = new Scanner (System.in);
    public void Login() {
    	String userName;
    	String password;
        
    	System.out.println ("\nWelcome user, please log in");
    	System.out.println ("\n Username: ");
     	userName = input.nextLine();
    	System.out.println (" Password: ");
    	password = input.nextLine();
    	if (userName.equalsIgnoreCase("user")&& password.equals("user")){
    		System.out.println ("\nWelcome");
    		options();
    	}else{
    		System.out.println ("\nTry again please");
    	}
    }
    public void options (){
    	takeOutBook book = new takeOutBook ();
    	String choice="";
    	System.out.println ("\n Add book \\ View account information \\ Search a book");
    	System.out.println ("\n Please choose");
    	choice = input.nextLine();
     	if (choice.equalsIgnoreCase("Add")){
     		book.takeOutBook();
    	}else if (choice.equalsIgnoreCase("View")){
    		System.out.println ("Your info");
    	}else if (choice.equalsIgnoreCase("Search")){
    		System.out.println ("Search");
    	}
    	
    }
    
}


This post has been edited by smohd: 18 January 2012 - 08:06 PM
Reason for edit:: Code tags added. Please use [code] tags when posting codes

Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1275
  • View blog
  • Posts: 2,837
  • Joined: 05-April 11

Re: how to get information from a arraylist to a text file and vice versa?

Posted 18 January 2012 - 08:07 PM

I don't see you using ObjectInputStream or ObjectOutputStream. Also your model class(es) have to be serializeable for it to work.

And why the offensive username ?
Was This Post Helpful? 0
  • +
  • -

#5 generic_user_1234  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 18-January 12

Re: how to get information from a arraylist to a text file and vice versa?

Posted 18 January 2012 - 08:09 PM

View PostCasiOo, on 18 January 2012 - 08:07 PM, said:

I don't see you using ObjectInputStream or ObjectOutputStream. Also your model class(es) have to be serializeable for it to work.

And why the offensive username ?


whats thats anyway man? like whats objectinputstream or objectoutputstream? like can you show me a example?

oh man i didnt know whatelse to put?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1