| | |
no luck with reading integers from file in arraylist
![]() |
•
•
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 |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor crashcourse database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle physics plazmic print problem program programming project recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree trolltech unlimited utility webservices windows





