1
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class gTextFile {

    static LinkedList<gText> list = new LinkedList<gText>();

    public static void main(String[] args) throws FileNotFoundException {
        Scanner scnOne = new Scanner(System.in);

        String eCode;

        System.out.print("Employee Code: ");
        eCode = scnOne.nextLine();

        readFile();
        gEmpName("eCode");
    }

    public static void readFile() throws FileNotFoundException {
        File nFile = new File("C:\\JAVAP\\GetTextFile\\employee.txt");
        Scanner scnTwo = new Scanner(nFile);
        String oTemp;
            while(scnTwo.hasNext()) {
            oTemp = scnTwo.next();
            String EmCode[] = oTemp.split(" ");
            String Name[] = EmCode[1].split(",");
            String idCode = EmCode[0];
            String lastname = Name[0];
            String firstname = Name[1];
            //System.out.println("FName " + firstname);
            //System.out.println("LName " + lastname);
            gText gT = new gText(firstname, lastname, idCode);
            list.add(gT);
            }
        scnTwo.close();
    }

    public static void gEmpName(String EmpCode) {
        Iterator<gText> itr = list.iterator();
        while(itr.hasNext()) {
            gText gT = itr.next();
            if(gT.id.equals(EmpCode)){
                System.out.println("Employee Name: " + gT.Fname + " " + gT.Lname);
            }

        }

    }

}

public class gText {
String Fname;
String Lname;
String id;

    gText(String First, String Last, String ID) {
        this.Fname = First;
        this.Lname = Last;
        this.id = ID;

    }

    public String gFName() {
        return Fname;
    }

    public String gLName() {
        return Lname;
    }

    public String gId() {
        return id;
    }

}

TextFile

What are the problem in my code?? the specific code doesn't show out when I run my program. It always say that there are problem. This always come out in the console, when I put the Employee Code. Employee Code: A11-0001 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at gTextFile.readFile(gTextFile.java:28) at gTextFile.main(gTextFile.java:17)

BetaRide
  • 14,756
  • 26
  • 84
  • 153
Sethoy
  • 35
  • 3

1 Answers1

1

Use oTemp = scnTwo.nextLine(); instead of oTemp = scnTwo.next(); in your readFile() method. When using next() you are just getting what comes before first space, in your case only employee code, so use nextLine() to fetch complete line. For more clarification on difference between next() and nextLine(), refer below link

What's the difference between next() and nextLine() methods from Scanner class?

Also, you may want to use gEmpName(eCode); instead of gEmpName("eCode");

Following is the code:

public class gTextFile {

static LinkedList<gText> list = new LinkedList<gText>();

public static void main(String[] args) throws FileNotFoundException {
    Scanner scnOne = new Scanner(System.in);

    String eCode;

    System.out.print("Employee Code: ");
    eCode = scnOne.nextLine();

    readFile();
    gEmpName(eCode);
}

public static void readFile() throws FileNotFoundException {
    File nFile = new File("/home/path/abc.txt");
    Scanner scnTwo = new Scanner(nFile);
    String oTemp;
        while(scnTwo.hasNext()) {
        oTemp = scnTwo.nextLine();
        String EmCode[] = oTemp.split(" ");
        String Name[] = EmCode[1].split(",");
        String idCode = EmCode[0];
        String lastname = Name[0];
        String firstname = Name[1];
        gText gT = new gText(firstname, lastname, idCode);
        list.add(gT);
        }
    scnTwo.close();
}

public static void gEmpName(String EmpCode) {
    Iterator<gText> itr = list.iterator();
    while(itr.hasNext()) {
        gText gT = itr.next();
        if(gT.id.equals(EmpCode)){
            System.out.println("Employee Name: " + gT.Fname + " " + gT.Lname);
        }

    }

}

}

Community
  • 1
  • 1
prateek tomar
  • 362
  • 1
  • 10
  • I tried to use the nextLine() but when I run it, it only put the code to the eCode and and nothing shows out in the console. and when I use next(), there's a problem with the code String Name[] = EmCode[1].split(","); in the readFile() method. this comes out always,Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at gTextFile.readFile(gTextFile.java:28) at gTextFile.main(gTextFile.java:17) – Sethoy Jan 16 '17 at 03:53
  • @sethoy updated answer and included the code. Try running it. – prateek tomar Jan 16 '17 at 06:31