Southbay Robotic and Coding Center
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Challenge 3

Go down

Challenge 3 Empty Challenge 3

Post by Admin Mon Aug 28, 2017 2:47 pm

[b]Challenge Question: [/b]
Read a pair of small integers from a single input line in the file 'test.in' and print their sum to the file 'test.out'.

Admin
Admin
Admin

Posts : 25
Join date : 2017-08-17
Location : Torrance CA

http://southbayrobot.com

Back to top Go down

Challenge 3 Empty Re: Challenge 3

Post by Admin Mon Aug 28, 2017 2:50 pm

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

class test {
 public static void main (String [] args) throws IOException {
   // Use BufferedReader rather than RandomAccessFile; it's much faster
   BufferedReader f = new BufferedReader(new FileReader("test.in")); // input file name goes above
   PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("test.out")));
   // Use StringTokenizer vs. readLine/split -- lots faster
   StringTokenizer st = new StringTokenizer(f.readLine()); // Get line, break into tokens
   int i1 = Integer.parseInt(st.nextToken());    // first integer
   int i2 = Integer.parseInt(st.nextToken());    // second integer
   out.println(i1+i2);                           // output result
   out.close();                                  // close the output file
 }
}
Admin
Admin
Admin

Posts : 25
Join date : 2017-08-17
Location : Torrance CA

http://southbayrobot.com

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum