Problems input

Write here if you have problems with your Java source code

Moderator: Board moderators

Post Reply
DiegoTrivino
New poster
Posts: 4
Joined: Wed Nov 14, 2012 7:15 pm

Problems input

Post by DiegoTrivino »

If I have the following entry:
2

1
-1 0
-5 -3
2 5
0 0

1
-1 0
0 1
0 0

should read as java?? with a variable BufferedReader and readLine () method and read line by line? or just read a single line in the following format:

2 \ n \ n1 \ n-1 0 \ n-5 -3 \ n2 5 \ n0 0 \ n \ n1 \ n-1 0 \ n0 1 \ n0 0
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: Problems input

Post by brianfry713 »

Which problem number?
Check input and AC output for thousands of problems on uDebug!
DiegoTrivino
New poster
Posts: 4
Joined: Wed Nov 14, 2012 7:15 pm

Re: Problems input

Post by DiegoTrivino »

I read only one line?
2 \ n \ n1 \ n-1 0 \ n-5 -3 \ n2 5 \ n0 0 \ n \ n1 \ n-1 0 \ n0 1 \ n0 0

or should I read several lines?

2

1
-1 0
-5 -3
2 5
0 0

1
-1 0
0 1
0 0
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: Problems input

Post by brianfry713 »

I'm not sure I understand what you're asking. On some problems it is best to parse the input line by line, on others you should read an int at a time.
Check input and AC output for thousands of problems on uDebug!
DiegoTrivino
New poster
Posts: 4
Joined: Wed Nov 14, 2012 7:15 pm

Re: Problems input

Post by DiegoTrivino »

Thanks, I was able to fix the problem, but now I have a new problem with the entry:
if not tell me which is the termination condition, as I know I'll finish the entry?
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: Problems input

Post by brianfry713 »

I can't tell you how to parse the input without a problem description.
Check input and AC output for thousands of problems on uDebug!
Hikari9
New poster
Posts: 20
Joined: Tue Jan 22, 2013 4:39 pm

Re: Problems input

Post by Hikari9 »

if you're asking about reading such input terminated by blank lines, that will work even if the last line has NO blank line, just check if the line read is a null string.

sample code:

Code: Select all

import java.util.*;
import java.io.*;

public class Main{
	public static void main( String args[] ) throws IOException{
	
		BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
		
		String in;
		int n = Integer.parseInt( br.readLine() );
		
		br.readLine(); // ignore first blank line
		
		while( n-- != 0 ){
			while( ( in = br.readLine() ) != null ){ // will always work even if EOF
				// do stuff here
			}
		}
		
	}
}
Post Reply

Return to “Java”