1 / 12
Mar 2011

How do I read to EOF using Java?

I have been stuck on numerous problems because I can't figure out the proper code for it.
I have looked at MANY sites trying to understand how to do it, but I'm having no luck.

I have tried things like the following...

try{
readLine()
\ rest of code
}
catch{}

while(r.read(cbuf) != -1){}
while(sc.hasNext())

I have had ZERO LUCK

Could someone please post how to do it. (Please don't post 'Just google it bro'... I HAVE TRIED)

  • created

    Mar '11
  • last reply

    Mar '11
  • 11

    replies

  • 1.1k

    views

  • 2

    users

  • 4

    links

Also, why wont this work either? (It continues to want input)

import java.util.Scanner;
public class Main {
    public static void main(String[] args) throws java.io.IOException{
        
    Scanner sc = new Scanner(System.in);
            
    while(sc.hasNextLine()){
        System.out.println(sc.nextLine());
        
    }

}
}

For example, if I run the above code on this problem, it times out... I don't know why. (I know if wont give a correct answer anyways, but it doesnt even finish the while loop)

spoj.pl/problems/NHAY/7

That one is a really bad example. You'll likely need a heavily optimized input method to get a java solution to pass. (not a standard method)

I doubt the 42 people who have done it in Java have a heavily optimzed input method.

Using the information I posted in my first post in this topic I got wrong answer on my first submission. I don't use java at all.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Main{
    public static void main(String[] args) throws IOException{
        InputStreamReader a = new InputStreamReader(System.in);
        BufferedReader b = new BufferedReader(a);
        while(b.readLine() != null){
            System.out.println("hi");
        }
     }
    }

Got it thanks

What's wrong with how I am reading to EOF? spoj.pl/problems/CPRMT/1

This is so annoying...

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main
{
    public static void main(String[] args) throws java.io.IOException{
        Scanner sc = new Scanner(System.in);
    String line = null;
    
    while((line=sc.nextLine())!=null){

            String b = sc.nextLine();
            ArrayList<String> letters = new ArrayList<String>();
            
            if(line.length() > b.length()){
                int temp = b.length();
                for(int i = 0; i < temp; i++){
                    if(line.contains(Character.toString(b.charAt(i)))){
                        if(letters.contains(Character.toString(b.charAt(i)))){
                            
                        }
                        else{
                            letters.add(Character.toString(b.charAt(i)));
                        }
                    }
                }
            }
            
            else{
                int temp = line.length();
                for(int i = 0; i < temp; i++){
                    if(b.contains(Character.toString(line.charAt(i)))){
                        if(letters.contains(Character.toString(line.charAt(i)))){
                            
                        }
                        else{
                            letters.add(Character.toString(line.charAt(i)));
                        }
                    }
                }
            }
            
            Collections.sort(letters);
            for(String letter : letters){
                System.out.print(letter);
            }
            System.out.println();
    }
}
}