Results 1 to 9 of 9

Thread: Read a file

  1. #1
    Join Date
    Jul 2006
    Posts
    249
    Rep Power
    0

    Default Read a file

    The red lines I shoved in there to test something... to see why it printing out the other statements....

    FACT:
    1. The file does exist in the directory there (but the value of "dir.exists()" comes out False.....)

    please help

    Code:
    package proj;
    
    import java.lang.*;
    import java.util.*;
    import java.io.*;
    import proj.Store;
    
    /**
     *
     * @author gmanr26
     */
    public class Main /*extends Store*/{
        
       // public Main() {
       // }
        public static String file = "c:\test.txt";
        public static File dir = new File (file);
        
        public static void main(String[] args) throws IOException, ArrayIndexOutOfBoundsException {
            System.out.print ("here");
            System.out.print (dir.exists());
            if (dir.exists()){
                Scanner stuff = new Scanner (dir);
                while (stuff.hasNext()){
                    if (stuff.next()=="NEWCUST"){
                        String cust_num = stuff.next();
                        stuff.next();
                        String dob = stuff.next() + "/" + stuff.next() + "/" + stuff.next();
                        stuff.next();
                        String sex = stuff.next();
                        stuff.next();
                        String name = null;
                        while (stuff.next()!= "HOME#") {
                            name = name + " " + stuff.next(); 
                        }
                        String home_num = stuff.next();
                        stuff.next();
                        String cell_num = stuff.next();
                        stuff.next();
                        String work_num = stuff.next();
                        stuff.next();
                        String email = stuff.next();
                        stuff.next();
                        Integer ln = Integer.parseInt(stuff.next());
                        String addr= null;
                        while (ln >0){
                            addr = addr + " " + stuff.nextLine();
                        }
                        Store inst = new Store();
                        inst.new_cust (cust_num,dob,sex,name,home_num,cell_num,work_num,
                                       email,addr);
                        System.out.print ("New Customer " + cust_num + 
                                           "/n"+  " Date of Birth " + dob + 
                                           "/n" + " Gender" + sex + 
                                           "/n" + name + 
                                           "/n" + " Home# " + home_num + 
                                           "/n" + " Cell# " + cell_num + 
                                           "/n" + " Work# " + work_num + 
                                           "/n" + " E-Mail " + email + 
                                           "/n" + " Address " + addr);                        
                        }
                    else {
                        if (stuff.next()=="OPEN"){
                            String acc_type = stuff.next();
                            String acc_num = stuff.next();
                            stuff.next();
                            String cust_num = stuff.next();
                            stuff.next();
                            String date_opened = stuff.next() + "/" + stuff.next() + 
                                                 "/" + stuff.next();
                            stuff.next();
                            String bal = stuff.next();
                            Store inst = new Store();
                            inst.new_acc (acc_type,acc_num,cust_num,date_opened,bal);
                            System.out.print ("New Account Opened: " + 
                                               "/n" + acc_type + " " + 
                                               "/n" + "Account Number " + acc_num + 
                                               "/n" + "Customer Number " + cust_num + 
                                               "/n" + "Date Opened " + date_opened + 
                                               "/n" + "Opening Balance " + bal);
                        }
                        else {
                            if (stuff.next()=="GETBAL"){
                                String acc_num = stuff.next();
                                Store inst = new Store();
                                String bal = inst.get_bal (acc_num);
                                if (bal == null) {
                                    System.out.print ("Account does not exist");
                                }
                                else {
                                    System.out.print("The balance of account # " + 
                                                      acc_num + " is " + bal);
                                }                            
                            }
                            else {
                                if (stuff.next()=="DEP"){
                                    String acc_num = stuff.next();
                                    String dollars = stuff.next();
                                    Store inst = new Store();
                                    inst.deposit(acc_num,dollars);
                                    Store inst2 = new Store();
                                    String bal = inst.get_bal(acc_num);
                                    System.out.print("Account " + acc_num + " has been credited " + 
                                                     "with " + dollars + " and it now has a balance of " +
                                                      bal);
                                }
                                else {
                                    if (stuff.next()=="WDRAW"){
                                        String acc_num = stuff.next();
                                        String draw = stuff.next();
                                        Store inst = new Store();
                                        inst.withdraw(acc_num,draw);
                                        Store inst2 = new Store();
                                        String bal = inst2.get_bal(acc_num);
                                        System.out.print("Account " + acc_num + " has been debited " + 
                                                         "with " + draw + " and it now has a balance of " +
                                                         bal);
                                    }
                                    else {
                                        if (stuff.next()=="CLOSE"){
                                            String acc_num = stuff.next();
                                            Store inst = new Store();
                                            inst.end_acc(acc_num);
                                            System.out.print("The account "+ acc_num + 
                                                             " has been officially closed.");
                                        }
                                        else {
                                            stuff.next();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }  
        }
        
    }
    "And what's the real lesson? Don't leave things in the fridge"

  2. #2
    Join Date
    Sep 2005
    Posts
    55
    Rep Power
    0

    Default

    In Java Strings, the back slash is an escape character which gives the following character special meaning.

    If you need a back slash in a String (like in the file system path you have), you have to use two back slashes!

    Therefore

    "c:\test.txt"

    has to be changed to

    "c:\\test.txt"

  3. #3
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    this is an aside...

    Code:
    package proj;
    
    import java.lang.*;
    import java.util.*;
    import java.io.*;
    import proj.Store;
    you don't have to / should not import things that you have in the same package.
    Code:
                        System.out.print ("New Customer " + cust_num + 
                                           "/n"+  " Date of Birth " + dob + 
                                           "/n" + " Gender" + sex + 
                                           "/n" + name + 
                                           "/n" + " Home# " + home_num + 
                                           "/n" + " Cell# " + cell_num + 
                                           "/n" + " Work# " + work_num + 
                                           "/n" + " E-Mail " + email + 
                                           "/n" + " Address " + addr);
    you should use back slash instead of forward slash for line breaks.
    Last edited by icymint3; Dec 19, 2006 at 10:50 AM.
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  4. #4
    Join Date
    Jul 2006
    Posts
    249
    Rep Power
    0

    Default

    Quote Originally Posted by icymint3 View Post
    you should use back slash instead of forward slash for line breaks.

    whoops.............

    thanks guys
    "And what's the real lesson? Don't leave things in the fridge"

  5. #5
    Join Date
    Jul 2006
    Posts
    249
    Rep Power
    0

    Default

    ok new problem... for some reason the code wont enter the if statements even when i know its true.... any suggestions

    here's the code

    Code:
    package proj;
    
    import java.lang.*;
    import java.util.*;
    import java.io.*;
    import proj.Store;
    
    /**
     *
     * @author gmanr26
     */
    public class Main {
        
        public static String file = "c:\\test.txt";
        public static File dir = new File (file);
        
        public static void main(String[] args) throws IOException, ArrayIndexOutOfBoundsException {
            //System.out.print ("here");
            //System.out.print (dir.exists());
            if (dir.exists()){
                Scanner stuff = new Scanner (dir);
                //System.out.print (stuff.hasNext());
                while (stuff.hasNext()){
                    if ((stuff.next())=="NEWCUST"){
                        String cust_num = stuff.next();
                        stuff.next();
                        String dob = stuff.next() + "/" + stuff.next() + "/" + stuff.next();
                        stuff.next();
                        String sex = stuff.next();
                        stuff.next();
                        String name = null;
                        while (stuff.next()!= "HOME#") {
                            name = name + " " + stuff.next(); 
                        }
                        String home_num = stuff.next();
                        stuff.next();
                        String cell_num = stuff.next();
                        stuff.next();
                        String work_num = stuff.next();
                        stuff.next();
                        String email = stuff.next();
                        stuff.next();
                        Integer ln = Integer.parseInt(stuff.next());
                        String addr= null;
                        while (ln >0){
                            addr = addr + " " + stuff.nextLine();
                        }
                        Store inst = new Store();
                        inst.new_cust (cust_num,dob,sex,name,home_num,cell_num,work_num,
                                       email,addr);
                        System.out.print ("New Customer " + cust_num + 
                                           "\n"+  " Date of Birth " + dob + 
                                           "\n" + " Gender" + sex + 
                                           "\n" + name + 
                                           "\n" + " Home# " + home_num + 
                                           "\n" + " Cell# " + cell_num + 
                                           "\n" + " Work# " + work_num + 
                                           "\n" + " E-Mail " + email + 
                                           "\n" + " Address " + addr);                        
                        }
                    else {
                        if (stuff.next()=="OPEN"){
                            String acc_type = stuff.next();
                            String acc_num = stuff.next();
                            stuff.next();
                            String cust_num = stuff.next();
                            stuff.next();
                            String date_opened = stuff.next() + "/" + stuff.next() + 
                                                 "/" + stuff.next();
                            stuff.next();
                            String bal = stuff.next();
                            Store inst = new Store();
                            inst.new_acc (acc_type,acc_num,cust_num,date_opened,bal);
                            System.out.print ("New Account Opened: " + 
                                               "\n" + acc_type + " " + 
                                               "\n" + "Account Number " + acc_num + 
                                               "\n" + "Customer Number " + cust_num + 
                                               "\n" + "Date Opened " + date_opened + 
                                               "\n" + "Opening Balance " + bal);
                        }
                        else {
                            if (stuff.next()=="GETBAL"){
                                String acc_num = stuff.next();
                                Store inst = new Store();
                                String bal = inst.get_bal (acc_num);
                                if (bal == null) {
                                    System.out.print ("Account does not exist");
                                }
                                else {
                                    System.out.print("The balance of account # " + 
                                                      acc_num + " is " + bal);
                                }                            
                            }
                            else {
                                if (stuff.next()=="DEP"){
                                    String acc_num = stuff.next();
                                    String dollars = stuff.next();
                                    Store inst = new Store();
                                    inst.deposit(acc_num,dollars);
                                    Store inst2 = new Store();
                                    String bal = inst.get_bal(acc_num);
                                    System.out.print("Account " + acc_num + " has been credited " + 
                                                     "with " + dollars + " and it now has a balance of " +
                                                      bal);
                                }
                                else {
                                    if (stuff.next()=="WDRAW"){
                                        String acc_num = stuff.next();
                                        String draw = stuff.next();
                                        Store inst = new Store();
                                        inst.withdraw(acc_num,draw);
                                        Store inst2 = new Store();
                                        String bal = inst2.get_bal(acc_num);
                                        System.out.print("Account " + acc_num + " has been debited " + 
                                                         "with " + draw + " and it now has a balance of " +
                                                         bal);
                                    }
                                    else {
                                        if (stuff.next()=="CLOSE"){
                                            String acc_num = stuff.next();
                                            Store inst = new Store();
                                            inst.end_acc(acc_num);
                                            System.out.print("The account "+ acc_num + 
                                                             " has been officially closed.");
                                        }
                                        else {
                                            stuff.next();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }  
        }
        
    }
    and here is the text document its reading

    Code:
    NEWCUST 1977 DOB 17 07 1887 GENDER M TITLE Hon NAME Marcus Mosiah Garvey
    HOME# 927-7777
    CELL# N/A
    WORK# 977-7777
    EMAIL N/A
    ADDR 2
    1 Lady Musgrave Ave
    Kingston 5 Jamaica
    OPEN SAVINGSACCT 5676 CUST 1977 OPENED 12 02 2006 BAL 500.00
    GETBAL 5676
    DEP 5767 250.00
    WDRAW 5676 100.00
    NEWCUST 1675 DOB 10 12 1988 GENDER F TITLE Ms NAME Mary Q Studious
    HOME# N/A
    CELL# N/A
    WORK# N/A
    EMAIL N/A
    ADDR 4
    Hackers Computer Club
    c/o Dept of Computer Science
    UWI Mona
    Kingston 7 Jamaica
    OPEN CHECKINGACCT 2035 CUST 1675 OPENED 11 15 2006 BAL 50.00
    DEP 5676 500.00
    DEP 2035 400.00
    GETBAL 2035
    WDRAW 2035 700.50
    GETBAL 2035
    CLOSE 2035
    DEP 2035 700.00
    WDRAW 2035 200.00
    GETBAL 2035
    NEWCUST 1598 DOB 23 01 1981 GENDER F TITLE Mrs NAME June S Johnson
    HOME# 989-1364
    CELL# 844-9056
    WORK# 998-5422
    EMAIL wilma@yahoo.com
    ADDR 3
    23 Wildman Avenue 
    Linstead
    St Catherine Jamaica
    OPEN SAVINGSACCT 4354 CUST 1598 OPENED 25 09 2006 BAL 10500.00
    DEP 4354 289
    GETBAL 4354
    DEP 5676 200.00
    WDRAW 4354 950.00
    DEP 4354 1.00
    NEWCUST 1188 DOB 18 11 1983 GENDER M TITLE Mr NAME Brian J McDonald
    HOME# N/A
    CELL# 254-1527
    WORK# 928-7734
    EMAIL fredfilmstone@gmail.com
    ADDR 2
    Stanton Terrace
    Kingston 5
    OPEN SAVINGSACCT 5441 CUST 1188 OPENED 25 09 2006 BAL 500.00
    OPEN CHECKINGACCT 7888 CUST 1188 OPENED 26 09 2006 BAL 2000.00
    WDRAW 5441 885.00
    DEP 7888 1200.00
    GETBAL 7888
    GETBAL 5441
    NEWCUST 1122 DOB 13 10 1982 GENDER F TITLE Mrs NAME Cova L Sinclair
    HOME# 705-1143
    CELL# N/A
    WORK# N/A
    EMAIL girl2000@cwja.com
    ADDR 1
    Kingston CSO
    OPEN CHECKINGACCT 7123 CUST 1122 OPENED 16 01 2006 BAL 2354.00
    GETBAL 7123
    WDRAW 7123 23.00
    CLOSE 5441
    NEWCUST 1099 DOB 15 08 1973 GENDER F TITLE Miss NAME Eve Michelle Thomas
    HOME# 453-7890
    CELL# 677-9087
    WORK# 978-8908
    EMAIL N/A
    ADDR 2
    16 August Town Rd
    Kingston 3
    OPEN SAVINGSACCT 5444 CUST 1099 OPENED 25 09 2006 BAL 3459.00
    GETBAL 5444
    WDRAW 5444 459.00
    WDRAW 5444 3500.00
    WDRAW 5444 3000.00
    GETBAL 5444
    DEP 5444 200.00
    GETBAL 7000
    DEP 7000 100.00
    WDRAW 7000 100.00
    OPEN SAVINGSACCT 8000 CUST 1500 OPENED 14 02 2006 BAL 200.00
    any suggestions.... please help me....
    "And what's the real lesson? Don't leave things in the fridge"

  6. #6
    Join Date
    Sep 2005
    Posts
    55
    Rep Power
    0

    Default

    You need to use oneString.equals(otherString) to compare Strings.

    == won't work!

  7. #7
    Join Date
    May 2004
    Posts
    168
    Rep Power
    0

    Default

    You also have the option of using :
    ("String".compareTo("String") == 0)

  8. #8
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    Code:
     
    	public static String file;
    	public static File inFile;
    
    	static { file = "c:\\test.txt"; inFile  = new File (file); }
    
    	public static void main(String[] args) throws IOException, ArrayIndexOutOfBoundsException {
    		if ( !inFile.exists()  || !inFile.isFile() ) return; // i'm guessing this is the if statement you talking about
    
    		Scanner stuff = new Scanner (inFile);
    
    		while (stuff.hasNext()){
    Last edited by icymint3; Dec 20, 2006 at 12:56 PM.
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  9. #9
    Join Date
    Apr 2005
    Posts
    11
    Rep Power
    0

    Default Try This

    File keyFile = new File(argv[0]+File.separator+"aacs"+File.separator+"VTKF000.AAC S");

    where you want the '/' you can put for eg file = "c"+File.separator+"test.txt";

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •