0

I have created a program that is supposed to read a text file for Integers and put them into an Arraylist, and then there are a bunch of methods to act on it. but, after some trouble shooting I am noticing that my program won't pull the integers from the text file in. Could anyone point me in the right direction?

    package project1;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
import java.lang.System;

    public class Main {
        public static void main(String[] Args) {
            Main mainObject = new Main();
            mainObject.run();
        }
        public void run() {
            **ArrayList<Integer> list = new ArrayList<>();
            String fileName = "p01-in.txt";
            Scanner in = new Scanner(fileName);
            while (in.hasNextInt()) {
                list.add(in.nextInt());
                int line = in.nextInt();
                System.out.println("%s  %n" + line);
            }
            in.close();**
            ArrayList<Integer> listRunsUpCount = new ArrayList<>();
            ArrayList<Integer> listRunsDnCount = new ArrayList<>();
            Main findRuns = new Main();
            listRunsUpCount = findRuns.FindRuns(list, 0);
            listRunsDnCount = findRuns.FindRuns(list, 1);
            ArrayList<Integer> listRunsCount = new ArrayList<>();
            Main mergeRuns = new Main();
            listRunsCount = mergeRuns.MergeRuns(listRunsUpCount, 
            listRunsDnCount);
            Main Output = new Main();
            Output.Output("p01-runs.txt", listRunsCount);
        }
4
  • What's the problem you are facing? Commented Jul 8, 2018 at 16:30
  • 2
    new Scanner(String) constructs a Scanner that reads from that String (not a File). You probably meant to pass a file there. Commented Jul 8, 2018 at 16:30
  • 1
    Every time you call in.nextInt() in your while loop you are pulling in a integer value. You make the call twice, is this intentional. Perhaps what you should be doing is placing what is read into a variable then utilizing that twice instead: int val = in.nextInt(); list.add(val); System.out.println("%s %n" + String.valueOf(val));. OR put int line = in.nextInt(); above list.add(...) and then change list.add(in.nextInt()); to list.add(line);. Commented Jul 8, 2018 at 16:42
  • By the way, it is good practice to follow the Java Naming Conventions. Variables and method names should start with lowercase. So you should refactor the variable names Args and Output and the method names FindRuns, MergeRuns and Output. Commented Jul 26, 2018 at 14:55

1 Answer 1

1

You can use BufferedReader to read file content line by line.

try {
        BufferedReader reader = new BufferedReader(new FileReader(fileName));
        String line=reader.readLine();
        while (line != null) {
            line = reader.readLine();
            list.add(Integer.parseInt(line.trim()));
        }
        reader.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.