Cod sursa(job #1431248)

Utilizator Deliu_Cosmin_Dragos_321CADeliu Cosmin Dragos Deliu_Cosmin_Dragos_321CA Data 9 mai 2015 05:58:56
Problema A+B Scor 100
Compilator java Status done
Runda Arhiva de probleme Marime 2.02 kb
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;


public class Main {
	
	String filePath = "adunare.in";
	BufferedReader reader = null;
	Queue<String> wordBuffer = new LinkedList<String>();;
	
	
	public void open() {
		if (reader != null) {
			throw new IllegalStateException("File already opened for reading");
		}
		try {
			reader = new BufferedReader(new FileReader(filePath));
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * Closes the file opened for reading.
	 * <p>
	 * Should be called after finishing reading.
	 */
	public void close() {
		wordBuffer.clear();
		if (reader != null) {
			try {
				reader.close();
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
	}

	/**
	 * Parses the underlying file and returns the next word.
	 *
	 * @return a word as a <code>String<code>, or <code>null<code> if the end of the file has been
	 * reached.
	 */
	public String getNextWord() {

		if (!wordBuffer.isEmpty()) {
			return wordBuffer.poll();
		}

		// refill word buffer
		String[] nextWords = new String[0];
		while (nextWords.length == 0) {
			nextWords = parseNextLine();
			if (nextWords == null) {
				return null;
			}
		}
		wordBuffer.addAll(Arrays.asList(nextWords));
		return wordBuffer.poll();
	}

	private String[] parseNextLine() {
		String line;
		try {
			line = reader.readLine();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		if (line == null) {
			return null;
		}
		return line.toLowerCase().split("[^a-z0-9']+");
	}


	public static void main(String[] args) throws FileNotFoundException {
		
		int a,b;
		Main l = new Main();
		
		l.open();
		a = Integer.parseInt(l.getNextWord());
		b = Integer.parseInt(l.getNextWord());
		
	    PrintWriter writer = new PrintWriter("adunare.out");
	    writer.write(String.valueOf(a + b) + "\n");
	    writer.close();
	    l.close();
		

	}

}