Rish
Rish

Reputation: 73

How to read from txt file line and print arrays of strings and integers

I am reading from a text file a specific line defined by a user input with if(nameLine.contains(inputFileName)){ I am trying to print an array of names and array of numbers from that line. Could anyone help me out with how to do so? Do I have to create 2 different methods or can I do it all in main? I only know how to work with one Array in one code with while loop, but I am not sure how to have it all combined. Thank you for your help!

My code is not finished, but this is how it looks so far

public static void main(String[] args) throws IOException {

    Scanner in = new Scanner(System.in);
    System.out.print("Enter the State name: ");
    String inputFileName = in.next();
    String names = "pplnames17.txt";

    List<String> namearr = new ArrayList<String>();

    File namesFile = new File(names);
    Scanner stateSearch = new Scanner (namesFile);

    //print title
    String line0 = Files.readAllLines(Paths.get("pplnames17.txt")).get(0);
    System.out.printf("%s in %s", line0, inputFileName);

    while(stateSearch.hasNextLine()){
        String nameLine = stateSearch.nextLine();
        if(nameLine.contains(inputFileName)){
            System.out.printf("\n%s",nameLine);
        }
    }

This is a partial of my Array code for String, which I had it after a while loop

    while(stateSearch.hasNextLine()){
    namearr.add(stateSearch.nextLine());
}
String[] arr = namearr.toArray(new String [0]);
System.out.println(arr);

Upvotes: 0

Views: 1081

Answers (1)

achAmh&#225;in
achAmh&#225;in

Reputation: 4266

Whilst this isn't the greatest solution, here is a potential solution that you could build upon/tidy up.

Basically, read in your file, loop through each line, if the line contains the user input, split each string on that line, check if text or numeric, and add to the relevant array.

public static void main(String[] args) throws FileNotFoundException {
        List<String> nameArr = new ArrayList<>();
        List<Double> intArr = new ArrayList<>();
        System.out.print("Enter the State name: ");
        Scanner in = new Scanner(System.in);
        String inputFileName = in.next();

        Scanner sc = new Scanner(new File("pplnames17.txt"));

        while (sc.hasNextLine()) {
            String nameLine = sc.nextLine();
            if (nameLine.contains(inputFileName)) {
                String[] inputs = nameLine.split(" ");
                for (String input : inputs) {
                    if (isNumeric(input)) {
                        intArr.add(Double.parseDouble(input));
                    } else {
                        nameArr.add(input);
                    }
                }
            }
        }
        if (!nameArr.isEmpty()) {
            System.out.println(nameArr);
        } else {
            System.out.println("No names found");
        }
        if (!intArr.isEmpty()) {
            System.out.println(intArr);
        } else {
            System.out.println("No numbers found");
        }
    }

    public static boolean isNumeric(String str) {
        try {
            double d = Double.parseDouble(str);
        } catch (NumberFormatException e) {
            return false;
        }
        return true;
    }

I tested it and my text file was like this:

abc def ghi 12 55 jkl mno pqr

joe jack mary 1 2 james 4

paul jim kim bob 45 othername

bill yu will gill 455 Paulo

User input of mary gave an output of:

[joe, jack, mary, james]

[1.0, 2.0, 4.0]

Upvotes: 1

Related Questions