Advertisement
isefire

Data Recovery CodeEval

Aug 20th, 2014
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.93 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class DataRecov
  5. {
  6.     public static String[][] sort(String[][] bothnext)
  7.     {
  8.         String[][] bothfinal = bothnext;
  9.         int currentnum = 1;
  10.         for (int i = 0; i < bothfinal.length; i++)
  11.         {
  12.             if (currentnum == bothfinal.length) currentnum = 1;
  13.             for (int j = i; j < bothfinal.length; j++)
  14.             {
  15.                 int a = Integer.parseInt(bothfinal[i][0]);
  16.                 int b = Integer.parseInt(bothfinal[j][0]);
  17.                 if (b == currentnum || b == bothfinal.length+20)
  18.                 {
  19.                     //placeholder
  20.                     String placenum = bothfinal[i][0];
  21.                     String placeword = bothfinal[i][1];
  22.  
  23.                    
  24.                     bothfinal[i][0] = bothfinal[j][0];
  25.                     bothfinal[i][1] = bothfinal[j][1];
  26.                    
  27.                     bothfinal[j][0] = placenum;
  28.                     bothfinal[j][1] = placeword;
  29.                     break;
  30.                 }
  31.                
  32.             }
  33.             currentnum += 1;
  34.            
  35.         }
  36.         return bothfinal;
  37.     }
  38.     public static void bin(BufferedReader in)
  39.     {
  40.         String line;
  41.         try
  42.         {
  43.             while ((line = in.readLine()) != null)
  44.             {
  45.                
  46.                 String[] bothlst = line.split(";");
  47.                 String[] sentence = bothlst[0].split(" ");
  48.                 String[] nums = bothlst[1].split(" ");
  49.                 String[][] bothnext = new String[sentence.length][2];
  50.                 for (int i = 0; i < bothnext.length; i++)
  51.                 {
  52.                     try
  53.                     {
  54.                         bothnext[i][0] = nums[i];
  55.                     }
  56.                     catch (Exception e)
  57.                     {
  58.                         //Note need to handle how to declare missing numbers in hint
  59.                         bothnext[i][0] = Integer.toString(sentence.length+20);
  60.                     }
  61.                     bothnext[i][1] = sentence[i];
  62.                 }
  63.                 String[][] bothfinal = sort(bothnext);
  64.                 for (int i = 0; i < bothfinal.length; i++)
  65.                 {
  66.                     if (i == bothfinal.length-1) System.out.println(bothfinal[i][1]);
  67.                     else System.out.print(bothfinal[i][1]+ " ");
  68.                 }
  69.             }
  70.         }
  71.         catch (Exception e)
  72.         {
  73.             System.out.println(e);
  74.         }
  75.     }
  76.     public static void main(String[] args)
  77.     {
  78.         try
  79.         {
  80.             FileReader inc = new FileReader(args[0]);
  81.             BufferedReader in = new BufferedReader(inc);
  82.             bin(in);
  83.         }
  84.         catch (Exception e)
  85.         {
  86.             System.out.println("File not found");
  87.         }
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement