| | |
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 |
android api applet application array arrays automation awt bidirectional binary birt bluetooth busy_handler(null) calculator chat class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia link linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing threads time tree unlimited webservices windows





