コード例 #1
0
ファイル: RegexTest.java プロジェクト: KwokShing/Java
  public static void main(String[] args) throws PatternSyntaxException {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter pattern: ");
    String patternString = in.nextLine();

    Pattern pattern = Pattern.compile(patternString);

    while (true) {
      System.out.println("Enter string to match: ");
      String input = in.nextLine();
      if (input == null || input.equals("")) return;
      Matcher matcher = pattern.matcher(input);
      if (matcher.matches()) {
        System.out.println("Match");
        int g = matcher.groupCount();
        if (g > 0) {
          for (int i = 0; i < input.length(); i++) {
            // Print any empty groups
            for (int j = 1; j <= g; j++)
              if (i == matcher.start(j) && i == matcher.end(j)) System.out.print("()");
            // Print ( for non-empty groups starting here
            for (int j = 1; j <= g; j++)
              if (i == matcher.start(j) && i != matcher.end(j)) System.out.print('(');
            System.out.print(input.charAt(i));
            // Print ) for non-empty groups ending here
            for (int j = 1; j <= g; j++)
              if (i + 1 != matcher.start(j) && i + 1 == matcher.end(j)) System.out.print(')');
          }
          System.out.println();
        }
      } else System.out.println("No match");
    }
  }
コード例 #2
0
  public static void main(String[] args) {
    int i = 4;
    double d = 4.0;
    String s = "HackerRank ";

    Scanner scan = new Scanner(System.in);

    /* Declare second integer, double, and String variables. */
    int j;
    double e;
    String t;
    /* Read and save an integer, double, and String to your variables.*/
    j = scan.nextInt();
    e = scan.nextDouble();
    scan.nextLine();
    t = scan.nextLine();
    /* Print the sum of both integer variables on a new line. */
    System.out.println(i + j);
    /* Print the sum of the double variables on a new line. */
    System.out.println(d + e);
    /* Concatenate and print the String variables on a new line;
    the 's' variable above should be printed first. */
    System.out.println(s + t);

    scan.close();
  }
コード例 #3
0
 public static void main(String[] args) {
   /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
   Scanner sc = new Scanner(System.in);
   int mapSize = Integer.parseInt(sc.nextLine());
   char[][] map = new char[mapSize][];
   for (int i = 0; i < mapSize; i++) {
     String numbersString = sc.nextLine();
     map[i] = numbersString.toCharArray();
   }
   printMapWithCavities(map, mapSize);
 }
コード例 #4
0
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int T = Integer.parseInt(in.nextLine());

    for (int a = 0; a < T; a++) {

      String PAN = in.nextLine();
      System.out.println(isValidPAN(PAN));
    }

    in.close();
  }
コード例 #5
0
  public static void main(String[] args) {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    Scanner sc = new Scanner(System.in);
    String[][] numArray = new String[6][];
    int largestSum = 0;
    for (int i = 0; i < 6; i++) {
      numArray[i] = sc.nextLine().split(" ");
    }

    for (int i = 0; i <= 3; i++) {
      for (int j = 0; j <= 3; j++) {
        int sum =
            Integer.parseInt(numArray[i][j])
                + Integer.parseInt(numArray[i][j + 1])
                + Integer.parseInt(numArray[i][j + 2])
                + Integer.parseInt(numArray[i + 1][j + 1])
                + Integer.parseInt(numArray[i + 2][j])
                + Integer.parseInt(numArray[i + 2][j + 1])
                + Integer.parseInt(numArray[i + 2][j + 2]);
        // System.out.println(sum);
        if (i == 0 && j == 0) {
          largestSum = sum;
        } else {
          if (sum > largestSum) {
            largestSum = sum;
          }
        }
      }
    }

    System.out.println(largestSum);
  }
コード例 #6
0
ファイル: InferModel.java プロジェクト: viirya/sina_topic
  private InstanceList readFile() throws IOException {

    String NL = System.getProperty("line.separator");
    Scanner scanner = new Scanner(new FileInputStream(fileName), encoding);

    ArrayList<Pipe> pipeList = new ArrayList<Pipe>();
    pipeList.add(new CharSequence2TokenSequence(Pattern.compile("\\p{L}\\p{L}+")));
    pipeList.add(new TokenSequence2FeatureSequence());

    InstanceList testing = new InstanceList(new SerialPipes(pipeList));

    try {
      while (scanner.hasNextLine()) {

        String text = scanner.nextLine();
        text = text.replaceAll("\\x0d", "");

        Pattern patten = Pattern.compile("^(.*?),(.*?),(.*)$");
        Matcher matcher = patten.matcher(text);

        if (matcher.find()) {
          docIds.add(matcher.group(1));
          testing.addThruPipe(new Instance(matcher.group(3), null, "test instance", null));
        }
      }
    } finally {
      scanner.close();
    }

    return testing;
  }
コード例 #7
0
  /** @param args the command line arguments */
  public static void main(String[] args) {

    // Takes user input and converts into a character array
    Scanner sc = new Scanner(System.in);
    int n = Integer.parseInt(sc.nextLine());
    String input = sc.nextLine();
    String[] a = input.split(" ");

    BigInteger[] storage = new BigInteger[n];
    BigInteger sum = new BigInteger("0");

    // Converts and Stores Big Integers
    for (int i = 0; i < n; i++) storage[i] = new BigInteger(a[i]);

    // Sums the BigIntegers in storage
    for (int j = 0; j < n; j++) sum = sum.add(storage[j]);

    // Outputs the sum of values in storage as a string
    System.out.println(sum);
  }
コード例 #8
0
  public static void main(String[] args) throws IOException {
    Scanner in = new Scanner(System.in);
    // final String fileName = System.getenv("OUTPUT_PATH");
    // BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
    int res;

    int _arr_size = Integer.parseInt(in.nextLine());
    int[] _arr = new int[_arr_size];
    int _arr_item;
    for (int _arr_i = 0; _arr_i < _arr_size; _arr_i++) {
      _arr_item = Integer.parseInt(in.nextLine());
      _arr[_arr_i] = _arr_item;
    }

    res = lonelyInteger(_arr);
    System.out.println(res);
    // bw.write(String.valueOf(res));
    // bw.newLine();

    // bw.close();
  }
コード例 #9
0
ファイル: EndOfFile.java プロジェクト: jsferrer1/hackerrank
 public static void main(String[] args) {
   /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
   Scanner in = new Scanner(System.in);
   String input = "";
   int n = 1;
   String output = "";
   while (in.hasNextLine()) {
     if (in.hasNext()) {
       output += n + " " + in.nextLine() + "\n";
       n++;
     }
   }
   System.out.println(output);
 }
コード例 #10
0
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    Matcher m =
        Pattern.compile(
                "(?<countryCode>\\d{1,3})"
                    + "([ -])"
                    + "(?<localAreaCode>\\d{1,3})"
                    + "\\2"
                    + "(?<number>\\d{4,10})")
            .matcher("");

    int N = Integer.parseInt(in.nextLine());
    while (N-- > 0) {
      String line = in.nextLine();
      if (m.reset(line).matches()) {
        String countryCode = m.group("countryCode");
        String localAreaCode = m.group("localAreaCode");
        String number = m.group("number");
        String output =
            "CountryCode=" + countryCode + ",LocalAreaCode=" + localAreaCode + ",Number=" + number;
        System.out.println(output);
      }
    }
  }