| | |
no luck with reading integers from file in arraylist
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2004
Posts: 26
Reputation:
Solved Threads: 0
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.
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();
}
} Here's a way to filter out what you need without worrying about hideous "blocks" from the Scanner class--
java Syntax (Toggle Plain Text)
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(); } } }
![]() |
Other Threads in the Java Forum
- Previous Thread: Inserting event sounds in java application
- Next Thread: Database for midlet (EAV model)
| Thread Tools | Search this Thread |
account android api applet application array arrays automation bidirectional binary birt bluetooth chat class classes client code columns component data database designadrawingapplicationusingjavajslider draw eclipse editor error errors event exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance input integer intellij j2me java javaprojects jlabel jme jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source plazmic print problem program programming project property recursion ria scanner screen search server set size sms smsspam sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads tree windows





