Ok I have a program that accepts input from a file. it reads in inputs and is supposed to close automagically when it sees the number '42'. Only problem, I can read the input just dont know how to look for 42.

Here's the code? HELP plz.

import java.io.* ;
 public class life42 {
 public static void main (String args[]) {
 if (args.length == 1 )
 {
  try 
  {
  FileInputStream fstream = new
   FileInputStream(args[0]) ;
 DataInputStream in =
 new DataInputStream (fstream);
 
 while (in.available() !=0)
  System.out.println(in.readLine()) ;
 
}
in.close();
}
catch (Exception e)
{
 System.err.println("There was an error reading your input");
}
System.out.println("Input read");
}}}

Can you also give us a sample of the file?

I would use a Scanner object to read each token in the file. Then if that token matches 42, exit.

Something says try that Scanner and see what happens. Looking into it right now, looks like exactly what I needed

cheak this

import java.io.* ;
import java.util.*;
 public class life42 {
 public static void main (String args[]) {
 try 
  {
  FileInputStream fstream = new FileInputStream("ex.txt") ;
 DataInputStream in =new DataInputStream (fstream);
 
 while (in.available() !=0)
      {
     
     String str=new String(in.readLine());
     char[] c=str.toCharArray();
     
    
    for ( int i = 0; i  < c.length; i++ ) 
          {
        if(c[i]=='4')
              {
            if(c[i+1]=='2')
            break;
            }

       System.out.print ( c [ i ]  ) ; 
          }       
           }
 in.close();
}
catch (Exception e)
{
 System.err.println("There was an error reading your input");
}
System.out.println("Input read");
}}
This article has been dead for over six months. Start a new discussion instead.