JW1996
JW1996

Reputation: 21

Importing from text file into Java Array

Currently I am trying to import data from a text file regarding pets and doctors, and sending it to my "petArray" and "doctorArray".

I am extremely new to Java and as such am having great difficulty. This is what ive attempted to do, but it doesnt seem too work.

Please see attached Java code and text file (screenshot at link).

 public void readFile() {
    String fileName = "VetManagement.txt";
    Scanner inputStream = null;
    try {
        inputStream = new Scanner(new File(fileName));
    } catch (FileNotFoundException e) {
        System.out.println("Error opening file: " + fileName);
        System.exit(0);
    }
    while (inputStream.hasNextLine()) {
        String line = inputStream.nextLine();
        String initName = "";
        String initSize = "";
        String initType = "";
        String initDoctor = "";
        double initWeight = 0.0;
        int initAge = 0;

        if (line.equals("Pets")) {
            inputStream.nextLine();

            if (line.equals("type cat")) {
                initType = "cat";
                System.out.print(initType);

            } else if (line.equals("type dog")) {
                initType = "dog";
                System.out.print(initType);
            }

            inputStream.nextLine();

            if (line.equals("size small")) {
                initSize = "small";

            } else if (line.equals("size medium")) {
                initSize = "medium";

            } else if (line.equals("size large")) {
                initSize = "large";

            } else System.out.println("error");

            inputStream.nextLine();

            if (line.startsWith("name")) {
                initName = inputStream.next();

            } else {
                System.out.println("error");
            }

            inputStream.nextLine();

            if (line.startsWith("weight")) {
                initWeight = inputStream.nextDouble();
            } else {
                System.out.println("error");
            }
            inputStream.nextLine();

            if (line.startsWith("age")) {
                initAge = inputStream.nextInt();
            } else {
                System.out.println("error");
            }
            inputStream.nextLine();

            if (line.startsWith("doctor")) {
                initDoctor = inputStream.toString();
            } else {
                System.out.println("error");
            }

            petArray[sumPets] = new Pet();
            petArray[sumPets].setType(initType);
            petArray[sumPets].setSize(initSize);
            petArray[sumPets].setName(initName);
            petArray[sumPets].setWeight(initWeight);
            petArray[sumPets].setAge(initAge);
            petArray[sumPets].setDoctorName(initDoctor);

        } else if (line.equals("Doctors")) ;

    }

    inputStream.close();
}

TEXT FILE:

Pets
type cat
size small
name Lara
weight 4
age 5
doctor Joao
type dog
size large
name Biro
weight 15
age 12
doctor Maria
type cat
size large
name Benny
weight 7
age 10
doctor no doctor assigned
Doctors
name Joao
specialisation cat
name Maria
specialisation dog

Upvotes: 1

Views: 1736

Answers (2)

Sundeep
Sundeep

Reputation: 472

nextLine can solve the problem easily no need to over-complicate the work

As the TEXT FILE contains information in format like

type cat
size small
name Lara
weight 4
age 5
doctor Joao

You can easily store information in desired variable using

nextLine = inputStream.nextLine().split(" ");

where nextLine[0] represent first column and nextLine[1] represent second column

I Hope this helps let me know if you have any other problem here is FULL CODE (in case you need)

public static void readFile() {
        String fileName = "F:\\document\\eclipse\\JavaAZ\\src\\VetManagement.txt";
        Scanner inputStream = null;
        try {
            inputStream = new Scanner(new File(fileName));
        } catch (FileNotFoundException e) {
            System.out.println("Error opening file: " + fileName);
            System.exit(0);
        }
        String[] name = new String[100];
        String[] size = new String[100];
        String[] type = new String[100];
        String[] doctor = new String[100];
        double[] weight = new double[100];
        int[] age = new int[100];
        if (inputStream.hasNextLine()) {
            String[] nextLine = inputStream.nextLine().split(" ");
            int petCounter = 0;
            int doctorCounter = 0;
            String workingArray = new String(nextLine[0]);

            while(inputStream.hasNextLine()) {
                if(workingArray.equals("Pets")) {
                    nextLine = inputStream.nextLine().split(" ");
                    if (nextLine[0].equals("Doctors")) {
                        workingArray = "Doctors";
                        continue;
                    }

                    if (nextLine[0].equals("type")) {
                        type[petCounter] = nextLine[1];
                        //System.out.println(type);
                    }
                    else System.out.println("type error");

                    nextLine = inputStream.nextLine().split(" ");
                    if (nextLine[0].equals("size")) {
                            size[petCounter] = nextLine[1];
                            //System.out.println(size);
                    } 
                    else System.out.println("size error");

                    nextLine = inputStream.nextLine().split(" ");
                    if (nextLine[0].equals("name")) {
                            name[petCounter] = nextLine[1];
                            //System.out.println(name);
                    } 
                    else System.out.println("name error");

                    nextLine = inputStream.nextLine().split(" ");
                    if (nextLine[0].equals("weight")) {
                            weight[petCounter] = Double.parseDouble(nextLine[1]);
                            //System.out.println(weight);
                    } 
                    else System.out.println("weight error"); 

                    nextLine = inputStream.nextLine().split(" ");
                    if (nextLine[0].equals("age")) {
                            age[petCounter] = Integer.parseInt(nextLine[1]);
                            //System.out.println(age);
                    } 
                    else System.out.println("age error"); 

                    nextLine = inputStream.nextLine().split(" ");
                    if (nextLine[0].equals("doctor")) {
                            doctor[petCounter] = nextLine[1];
                            //System.out.println(doctor);
                    } 
                    else System.out.println("doctor error"); 
                    petCounter++;
                }
                else if(workingArray.equals("Doctors")) {
                    // CODE HERE
                    doctorCounter++;
                    break;
                }
            }
         }
        System.out.println("PET NAME: "+name[0]+" and its Weight: "+weight[0]);
        inputStream.close();
    }

Upvotes: 1

kowsikbabu
kowsikbabu

Reputation: 509

    if (line.equals("Pets")) {
        String nextLine = inputStream.nextLine();
        if (nextLine.equals("type cat")) {
            initType = "cat";
            System.out.print(initType);
        }
        else if (nextLine.equals("type dog")) {
            initType = "dog";
            System.out.print(initType);
        }
        String lineAfterThat=inputStream.nextLine();

You have to store every line before you can do anything with that. You are assuming that inputStream.nextLine() reads the same line again and again, but each time you use inputStream.nextLine() it reads the next line of the file and reaches the end of the file eventually. That's what is wrong.

You're not understanding the way Scanner works Use this:

if (line.equals("Pets")) {
    String nextLine = inputStream.nextLine();
    if (nextLine.equals("type cat")) {
        initType = "cat";
        System.out.print(initType);
    }
    else if (nextLine.equals("type dog")) {
        initType = "dog";
        System.out.print(initType);
    }
    String lineAfterThat=inputStream.nextLine();
    if (lineAfterThat.equals("size small")) {
            initSize = "small";

        } else if (lineAfterThat.equals("size medium")) {
            initSize = "medium";

        } else if (lineAfterThat.equals("size large")) {
            initSize = "large";

        } else System.out.println("error");
        String nextFirstWord=inputStream.next(); //so that it reads only till the space
        if (nextFirstWord.equals("name")) {
            initName = inputStream.nextLine();

        } else {
            System.out.println("error");
        }
        String ageLineFirstWord = inputStream.next();
        if (ageLineFirstWord .equals("age")) {
            initAge =inputStream.nextInt();
        } else {
            System.out.println("error");
        }
        inputStream.nextLine(); //this is to move the scanner to the nextline
        String doctorLineFirstWord = inputStream.next();
        if (doctorLineFirstWord .equals("doctor")) {
            initDoctor = inputStream.nextLine();
        } else {
            System.out.println("error");
        }

Upvotes: 1

Related Questions