can someone help me understand what i'm doing wrong here. attempted to run, program builds but no output is generated and the compiler does'nt generate an error message.

thank you all.

import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;


public class arrayManipulator 
{

                 public static void main(String[] args) throws IOException
	{
		ArrayList<Integer> myArray = new ArrayList<Integer>();
		
		Scanner s = null;
		try 
		{
			s = new Scanner(new BufferedReader (new FileReader("usrnames.txt")));
				while(s.hasNext())
				{
					if(s.hasNextInt())
					{
						for(int i=0; i<myArray.size(); i++)
						{
						myArray.set(i, s.nextInt());
						System.out.print(" "+myArray.get(i));
						}
					}
				}
		}
		finally
		{ s.close();
		}

	}

Dani AI

Generated

The observed silence comes from the loop logic: the code reads tokens with the Scanner but never adds any integers to the list before iterating. The inner loop uses i < myArray.size() while myArray is empty, so its body never runs; using myArray.set(...) would also be wrong unless the index already exists. That is why the program builds and runs but prints nothing.

A simple, correct pattern is to consume integers as they are found and add them to the list, then print afterwards. For modern Java, use try-with-resources to ensure the Scanner is closed safely:

ArrayList<Integer> myArray = new ArrayList<Integer>();
try (Scanner s = new Scanner(new File("usrnames.txt"))) {
    while (s.hasNextInt()) {
        myArray.add(s.nextInt());
    }
}
System.out.println(myArray);

Additional notes tied to the thread: 's code closed s in a finally even when s might remain null if the file failed to open; that can cause a NullPointerException. Using try-with-resources (shown above) or checking if (s != null) before close() avoids that. 's character-by-character approach is fragile: reading single chars and parsing them will break on multi-digit integers, negatives, or formatted separators. Prefer token-based parsing (Scanner, String.split, or StringTokenizer) or read lines and parse tokens per line.

If the input file includes non-integer tokens that should be ignored, use hasNextInt() to guard calls to nextInt(), or read tokens and attempt Integer.parseInt(...) inside a try/catch to skip bad tokens.

Recommended Answers

All 2 Replies

Here's a way to filter out what you need without worrying about hideous "blocks" from the Scanner class--

import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;

public class arrayManipulator{
        public static void main(String... args) throws IOException{
		ArrayList<Integer> myArray = new ArrayList<Integer>(0);
		BufferedReader s = null;
		try{
			s = new BufferedReader(new FileReader("C:/Documents and Settings/Mark/My Documents/usrnames.txt"));
				while(s.ready()){
					String value = "" + (char)s.read();
					try{
						Integer x = Integer.parseInt(value);
						myArray.add(x);
					}catch(Exception e){
						continue;
					}
				}
				System.out.println(myArray);
		}finally{ 
			s.close();
		}
	}
}

Thanks Alex

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.