1

I'm attempting to take an input from the user, then if it matches one of the existing files, read the file and put the words, letters, or number into an array. The "words", and "alphabet" files seem to work fine, but the "numbers" is giving me an issue. It finds the file, reads it, puts it into an array, and gives the summation of the numbers; however, the output of the array is every other number, as opposed to all numbers, like it should be doing.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class fileIO
{
    public static void main (String[] args) throws FileNotFoundException
    {
        String filename;
        System.out.println("Please enter name of file (alphabet,words,numbers): ");
        Scanner input = new Scanner(System.in);
        filename = input.nextLine();


        if(filename.equals("numbers"))
        {
            int sum = 0;
            Scanner reader = new Scanner(new File("/home/ninjew/workspace/FileIO/src/" + filename + ".txt"));
            ArrayList<Integer> arr = new ArrayList<Integer>();
            while(reader.hasNext())
            {
                arr.add(reader.nextInt());
                sum = sum + reader.nextInt();
            }
            System.out.println(arr);
            System.out.println("The summation is: " + sum);
            reader.close();
        }

        else if(filename.equals("words") || filename.equals("alphabet"))
        {
            Scanner reader = new Scanner(new File("/home/ninjew/workspace/FileIO/src/" + filename + ".txt"));
            // This is for words and letters within the file. Print words and letters.
            ArrayList<String> arr = new ArrayList<String>();

            while(reader.hasNext())
            {
                String line = reader.next();

                Scanner scanner = new Scanner(line);
                scanner.useDelimiter(",");
                while(scanner.hasNext()){
                    arr.add(scanner.next());
                }
                scanner.close();
            }
            System.out.println(arr);
            reader.close();

        }
    }
}

3 Answers 3

4

In if(filename.equals("numbers")) you are doing two reads from your file in this block, which is why it is skipping numbers

        while(reader.hasNext())
        {
            arr.add(reader.nextInt());
            sum = sum + reader.nextInt();
        }

Should be

        while(reader.hasNext())
        {
            int val = reader.nextInt();
            arr.add(val);
            sum = sum + val;
        }
Sign up to request clarification or add additional context in comments.

1 Comment

That worked perfect. Thanks for clearing that up for me!
1

Your numbers reader loop is adding first number to the array and the next number to the sum.

If input is 1 2 3 4 5 6, your array is [1, 3, 5] and your sum is 2 + 4 + 6 = 12.

Comments

1

You are reading next two integers in one go. Every time you call scanner.nextInt() it will read a next int. You need to store nextInt in a temp variable and use it in both the places.

while(reader.hasNext())
            {
                int next = reader.nextInt();
                arr.add(next);
                sum = sum + next;
            }

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.