简体   繁体   中英

Trying to find the nth element to the last from a list (with a file input)

Input would look like

a b c d 4
e f g h 2

where each line would be read like a list and integer representing as an index in the list

I first try to read the file line be line and store it in the list. Heres what i have

public class FileReader {

    public static void main(String[] args) {
        String line = null;
        List<String> list = new ArrayList<String>();

        try {
            FileInputStream fstream = new FileInputStream("test.txt");
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            // File file = new File("test.txt");
            // Scanner scanner = new Scanner(file);
            while ((line = br.readLine()) != null) {
                list.add(line);
            }

            System.out.println(list);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

Now i want to remove the white spaces from the list and store the values in char array and then i was planning on traversing that array backwards till the nth element, depending on the input for n.

String[] elements = line.trim().split("\\s");
char[] chars = new char[elements.length - 1];
int i= Integer.parseInt(elements[elements.length - 1]);
for (i = 0; i < elements.length - 1; i++)
    char[i] = elements[i].charAt(i);

Someone provided me this piece of code earlier and i tried it and it throws a nullpointerexception at String[] elements.

It's because you are running until line is null here

    while((line = br.readLine()) != null)
    {   
            list.add(line);

    }

And then you are trying to call .trim() on it.

Do you mean to be processing the strings in list instead?

If so try looping over you list, you are already splitting it correctly and getting the last element. All you need to do is caluclate the offset, in this case it will be the length - 1 - the last element, in you String[] elements and you can print that out.

    for (int i = 0; i < list.size(); i++)
    {
        String currentLine = list.get(i);
        String[] elements = currentLine.trim().split("\\s");
        int lastElement = Integer.parseInt(elements[elements.length - 1]);

        String desiredValue = elements[elements.length - 1 - lastElement];
        System.out.println("desiredValue = " + desiredValue);
    }

You can avoid most of the work you're doing. I don't know if your input will require much flexibility (code to that if necessary) but in your example you only have 1 digit for the index.

Just avoid all the traversing and looping entirely:

        String currentLine = file.nextLine();
        //Find value from last space in the string, until the end of the string (will be the number)
        int index = Integer.parseInt(currentLine.substring(
                currentLine.lastIndexOf(' ') + 1, currentLine.length()));
        //Remove all spaces from the current line
        currentLine = currentLine.replaceAll("\\s+","");
        //Remove the index at the end from the string, leaving only the characters
        currentLine = currentLine.substring(0, currentLine.indexOf(index + ""));

        char desiredValue = currentLine.charAt(currentLine.length() - index);
        System.out.println("desiredValue = " + desiredValue);

This saves a lot of adding stuff to arrays if none of that is needed later, just do it all the first time through.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM