Esempio n. 1
0
  public static void main(String[] args) throws NoSuchElementException {

    StackLL<Integer> stack = new StackLL<Integer>();
    Scanner sc = new Scanner(System.in);
    String op;

    while (sc.hasNext()) {
      op = sc.next();

      if (op.equals("Add")) {
        // Fill in the code
        while (sc.hasNextInt()) {
          stack.push(sc.nextInt());
        }
        System.out.println("Items added: " + stack);
      } else if (op.equals("Query")) {
        // Fill in the code

        while (sc.hasNextInt()) {
          int target = sc.nextInt();

          findAndPop(stack, target);
        }

        if (stack.isEmpty()) {
          System.out.println("Query not met: " + stack);
        } else {
          System.out.println("Query met: " + stack);
        }

        sc.nextLine();
      }
    }
  }
  // Start main()================================================================
  public static void main(String args[]) throws FileNotFoundException {

    Node[] firstTenThousand = new Node[10000]; // Stores the first ten thousand entries from input
    PrintWriter Out = new PrintWriter("TopTenThousand.txt"); // Ready for output to file
    int inputValue; // Stores each input value one at a time
    int count = 0; // Track line number of input values
    Scanner readFile = null;

    if (args.length > 0) {
      readFile = new Scanner(new File(args[0])); // Open Input File
    } else // No Input Given
    {
      System.out.println("Proper input file was not given as command line perameter");
      System.out.println("Example: java FindTopTenThousand data.txt");
      System.exit(-1);
    }

    // Read in first ten thousand values
    for (int i = 0; i < 10000 && readFile.hasNextInt(); i++) {
      inputValue = readFile.nextInt();
      count++;
      firstTenThousand[i] = new Node(inputValue, count);
    } // End for loop

    MinHeap heapArray =
        new MinHeap(firstTenThousand); // Turn first then thousand values into a min heap

    // Read all of the remaining entries in input file
    while (readFile.hasNextInt()) {
      inputValue = readFile.nextInt();
      count++;

      // If new value is larger than smallest of current values
      if (heapArray.heap[0].key < inputValue) {
        heapArray.exchangeSmallest(inputValue, count); // Exchange and heapify
      }
    } // End while loop

    readFile.close(); // Close input file

    Out.println("Ten thousand highest numbers from input file:"); // Formating for output file
    Out.println();

    // Print final results stored in heap
    for (int i = 0; i < 10000; i++) {
      Out.println(
          "Number From Input File: "
              + heapArray.heap[i].key
              + "      Input File Row Index: "
              + heapArray.heap[i].lineIndex);
    } // End for loop

    Out.close(); // Close output file
  } // End main()
 private static void displayPhoneBooks(ArrayList<String> phoneBooks)
     throws IOException, ParseException {
   if (phoneBooks.size() == 0) {
     System.out.println("\nNO PHONEBOOK AVAILABLE.CREATE A NEW PHONEBOOK.\n");
     return;
   } else {
     System.out.println("FOLLOWING ARE THE AVAILABLE PHONEBOOKS.");
     System.out.println("----------------------------------------\n");
     Scanner sc = new Scanner(System.in);
     for (int i = 0; i < phoneBooks.size(); i++) {
       String s = phoneBooks.get(i);
       System.out.println(s + "\t->PRESS " + (i + 1) + " LOAD THIS.");
     }
     System.out.println("TO GO BACK TO THE PREVIOUS MENU->PRESS " + (phoneBooks.size() + 1));
     int ch = 0;
     while (ch < 1 || ch > (phoneBooks.size() + 1)) {
       while (!sc.hasNextInt()) {
         System.out.println("\nONLY NUMBERS LISTED ABOVE SHOULD BE ENTERED..RETRY..");
         sc.nextLine();
       }
       ch = sc.nextInt();
       if (ch < 1 || ch > phoneBooks.size())
         System.out.println("\nONLY NUMBERS LISTED ABOVE SHOULD BE ENTERED..RETRY..");
     }
     if (ch == (phoneBooks.size() + 1)) return;
     contactBookOperations(phoneBooks.get(ch - 1) + ".txt");
   }
 }
  /**
   * Method reads file of name 'fileName' into Scanner object. Method reads each line into a String,
   * then tokenizes the String. Integer values from the String are output to the screen, while any
   * other tokens are skipped. Method assumes that integers are printed on same line as original
   * text.
   *
   * @throws FileNotFoundException
   */
  public static void printIntegersFromFile() throws FileNotFoundException {

    String fileName = "src/Question1.txt";
    Scanner input = new Scanner(new File(fileName));

    // process line by line
    while (input.hasNextLine()) {

      // read each line into String
      String line = input.nextLine();

      // tokenize String line by reading into new Scanner object
      Scanner tokens = new Scanner(line);

      // read until no tokens left on line
      while (tokens.hasNext()) {

        if (tokens.hasNextInt()) {

          // set nextInt and print to console
          int someNumber = tokens.nextInt();
          System.out.print(someNumber + " ");
        } else {
          // skip non integer tokens
          tokens.next();
        }
      }

      // insert blank line
      System.out.println();
    }
  }
  /**
   * a method that get the input from user and print out the magic square this is a recursive method
   */
  public void getInput() {
    System.out.print("please enter a positive odd integer __ or 0 to stop");
    Scanner scan = new Scanner(System.in);

    // check if the input is an intger
    if (!scan.hasNextInt()) {
      System.err.println("please enter a positive odd integer");
      getInput();

    } else {
      int input = Integer.parseInt(scan.next());
      if (input != 0) {
        if (input < 0 || input % 2 == 0) {
          System.err.println("please enter a positive odd integer");
          getInput();
        } else {
          OddMagicSquare s = new OddMagicSquare(input);
          String str = s.toString();
          System.out.println("a magic square with dimension of " + input + "X" + input);
          System.out.println(str);
          getInput();
        }
      }
    }
    return;
  }
  public void sol() {
    Scanner in = new Scanner(System.in);
    int maxOdd = Integer.MIN_VALUE;
    int minEven = Integer.MAX_VALUE;
    while (true) {
      if (!in.hasNextInt()) {
        System.out.println("please input numbers!");
        in.next();
        continue;
      }
      int curr = in.nextInt();
      if (curr == 0) break;
      if (curr % 2 == 0) {
        minEven = Math.min(curr, minEven);
      }
      if (curr % 2 == 1) {
        maxOdd = Math.max(maxOdd, curr);
      }
    }
    String maxOddString =
        (maxOdd == Integer.MIN_VALUE) ? "No Odd Number was inputted" : (maxOdd + "");
    String minEvenString =
        (minEven == Integer.MAX_VALUE) ? "No Even Number was inputted" : (minEven + "");

    System.out.println("The max odd number is:" + maxOddString);
    System.out.println("The min even number is:" + minEvenString);
  }
Esempio n. 7
0
  boolean solve(int caseNum) {
    if (!in.hasNextInt()) return false;
    int n = in.nextInt();
    if (n == 0) return false;

    UFNode[] graph = new UFNode[n + 1];
    int[] yes = new int[2];
    for (int i = 0; i < graph.length; i++) graph[i] = new UFNode();

    PARSE:
    while (true) {
      switch (in.next().charAt(0)) {
        case 'c':
          graph[in.nextInt()].merge(graph[in.nextInt()]);
          break;
        case 'd':
          graph[in.nextInt()].remove();
          break;
        case 'q':
          yes[graph[in.nextInt()].find() == graph[in.nextInt()].find() ? 0 : 1]++;
          break;
        case 'e':
          break PARSE;
      }
    }

    System.out.println(yes[0] + " , " + yes[1]);

    return true;
  }
  // @include
  public static void indirectSort(String fileName) throws Exception {
    // Stores file records into A.
    Scanner ifs = null;
    List<Integer> A = new ArrayList<>();
    try {
      ifs = new Scanner(new File(fileName));
      while (ifs.hasNextInt()) {
        A.add(ifs.nextInt());
      }
    } finally {
      ifs.close();
    }

    // Indirectly sorts file.
    Collections.sort(A);

    // Outputs file.
    PrintWriter ofs = null;
    try {
      ofs = new PrintWriter(new FileWriter(fileName));
      for (Integer a : A) {
        ofs.println(a);
      }
    } finally {
      ofs.close();
    }
  }
Esempio n. 9
0
  /** @param args */
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner keyboard = new Scanner(System.in);
    int[] myArray = new int[50];
    int num1 = 0;
    System.out.println("This program sorts and counts numbers in an array.");
    do {
      System.out.println("Please enter integer:");
      if (keyboard.hasNextInt()) myArray[num1++] = keyboard.nextInt();
      else break;
    } while (num1 < 50);
    System.out.println();

    int[] data = new int[num1];
    for (int i = 0; i < num1; i++) data[i] = myArray[i];

    Arrays.sort(data);
    System.out.println("N" + "\tCount");

    inner:
    for (int i = num1 - 1; i >= 0; i--) {
      for (int j = num1 - 1; j > i; j--) if (data[j] == data[i]) continue inner;

      int count = 0;
      for (int j = 0; j < num1; j++) if (data[j] == data[i]) count++;

      //		System.out.println("N"+"\tCount");
      System.out.println(data[i] + "\t" + count);
    }
  }
Esempio n. 10
0
  public void run() throws IOException {
    Scanner in = new Scanner(new File("partitions.in"));
    PrintWriter out = new PrintWriter(new File("partitions.out"));

    int sum = 0;
    while (true) {
      int n = in.nextInt();
      sum += n;
      int k = in.nextInt();
      if (n == 0 && k == 0) {
        break;
      }
      in.nextLine();
      ArrayList<Integer>[] set = new ArrayList[k];
      for (int i = 0; i < k; i++) {
        set[i] = new ArrayList<Integer>();
        String t = in.nextLine();
        Scanner sc = new Scanner(t);
        while (sc.hasNextInt()) {
          int q = 0;
          set[i].add(q = sc.nextInt() - 1);
        }
      }
      ArrayList<Integer> free = new ArrayList<Integer>();
      int keep = 0;
      for (int i = k - 1; i >= 0; i--) {
        ArrayList<Integer> nxt = next(set[i], free);
        if (nxt != null) {
          set[i] = nxt;
          keep = i + 1;
          break;
        } else {
          free.addAll(set[i]);
          Collections.sort(free);
        }
      }
      int newk = keep + free.size();
      out.println(n + " " + newk);
      for (int i = 0; i < keep; i++) {
        for (int j = 0; j < set[i].size(); j++) {
          if (j > 0) {
            out.print(" ");
          }
          out.print(set[i].get(j) + 1);
        }
        out.println();
      }
      Collections.sort(free);
      for (int i = 0; i < free.size(); i++) {
        out.println(free.get(i) + 1);
      }
      out.println();
    }

    assert sum <= 2000;

    in.close();
    out.close();
  }
Esempio n. 11
0
 /**
  * Utility method to read a set in from standard input via a Scanner. Each element requested is
  * checked to be between 0 and maxSize, and if it meets this requirement, it is included in the
  * current byteArray.
  *
  * @param in The Scanner to read from
  */
 public void readSet(Scanner in) {
   clear();
   while (in.hasNextInt()) {
     int n = in.nextInt();
     if (n >= 0 && n < maxSize) include(n);
   }
   in.next();
 }
Esempio n. 12
0
 public static void main(String[] args) {
   Scanner si = new Scanner(System.in);
   while (si.hasNextInt()) {
     int a = si.nextInt();
     int b = si.nextInt();
     System.out.println(a + b);
   }
 }
 private static Integer getCorrectUserInput(String message) {
   Scanner scanner = new Scanner(System.in);
   if (scanner.hasNextInt()) {
     return scanner.nextInt();
   } else {
     System.out.print("Please enter a valid positive number for " + message);
     return null;
   }
 }
Esempio n. 14
0
  public static void main(String[] args) throws IOException {
    Scanner scanner = new Scanner(new BufferedInputStream(System.in));

    int num[][] = {
      {0, 2, 3, 5, 6, 7, 8, 9},
      {2, 3, 4, 5, 6, 8, 9},
      {0, 2, 3, 5, 6, 8, 9},
      {0, 4, 5, 6, 8, 9},
      {0, 1, 2, 3, 4, 7, 8, 9},
      {0, 2, 6, 8},
      {0, 1, 3, 4, 5, 6, 7, 8, 9}
    };

    while (scanner.hasNextInt()) {
      int a = scanner.nextInt();
      String b = scanner.next();
      if (a == 0) break;
      String data[][] = new String[5 + (a - 1) * 2][(3 + (a - 1)) * b.length()];

      for (int i = 0; i < 5 + (a - 1) * 2; i++)
        for (int j = 0; j < (3 + (a - 1)) * b.length(); j++) data[i][j] = " ";

      for (int i = 0; i < b.length(); i++) {
        for (int j = 0; j < 3; j++) {
          for (int k = 0; k < num[j].length; k++) {
            if ((int) b.charAt(i) - 48 == num[j][k]) {
              for (int h = 0; h < a; h++) data[(a + 1) * j][1 + i * (a + 2) + h] = "-";
            }
          }
        }
        for (int j = 3; j < 5; j++) {
          for (int k = 0; k < num[j].length; k++) {
            if ((int) b.charAt(i) - 48 == num[j][k]) {
              for (int h = 0; h < a; h++) data[1 + h][(j - 3) * (a + 1) + (i * (a + 2))] = "|";
            }
          }
        }
        for (int j = 5; j < 7; j++) {
          for (int k = 0; k < num[j].length; k++) {
            if ((int) b.charAt(i) - 48 == num[j][k]) {
              for (int h = 0; h < a; h++) data[a + 2 + h][(j - 5) * (a + 1) + (i * (a + 2))] = "|";
            }
          }
        }
      }
      StringBuffer sb = new StringBuffer("");
      for (int i = 0; i < 5 + (a - 1) * 2; i++) {
        for (int j = 0; j < (3 + (a - 1)) * b.length(); j++) {
          if ((j - (a + 1)) % (a + 2) == 0 && (j + 1) != (3 + (a - 1)) * b.length())
            sb.append(data[i][j] + " ");
          else sb.append(data[i][j]);
        }
        sb.append("\n");
      }
      System.out.println(sb);
    }
  }
Esempio n. 15
0
 public static void main(String[] args) {
   Scanner sc = new Scanner(System.in);
   PrintWriter pw = new PrintWriter(System.out);
   while (sc.hasNextInt()) {
     solve(sc, pw);
   }
   sc.close();
   pw.flush();
   pw.close();
 }
Esempio n. 16
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 SUM = 0;
    // Skip first line.
    sc.nextInt();

    while (sc.hasNextInt()) {
      SUM += sc.nextInt();
    }
    System.out.println(SUM);
  }
  public static void main(String[] args) {
    Scanner kek = new Scanner(System.in);

    while (kek.hasNextInt()) {
      int n = in.nextInt();
      int[] lol = new int[n];
      for (int k : lol) {
        k = in.nextInt();
      }

      int countswaps = 0;
      for (int i = 0; i < lol.length - 1; i++) {}
    }
  }
Esempio n. 18
0
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    BigInteger[] fibs = new BigInteger[5001];
    fibs[0] = BigInteger.ZERO;
    fibs[1] = BigInteger.ONE;

    for (int i = 2; i < 5001; i++) {
      fibs[i] = fibs[i - 1].add(fibs[i - 2]);
    }

    while (in.hasNextInt()) {
      int num = in.nextInt();
      System.out.printf("The Fibonacci number for %d is %s\n", num, fibs[num]);
    }
  }
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    while (in.hasNextInt()) {
      int N = in.nextInt();
      if (N == 0) return;

      int[][] points = new int[N][2];
      for (int i = 0; i < N; i++) {
        points[i][0] = in.nextInt();
        points[i][1] = in.nextInt();
      }

      solve(points);
    }
  }
  /**
   * This function evaluates current Lisp expression in inputExpr It return result of the expression
   *
   * <p>The algorithm:
   *
   * <p>Step 1 Scan the tokens in the string. Step 2 If you see an operand, push operand object onto
   * the thisExprStack Step 3 If you see "(", next token should be an operator Step 4 If you see an
   * operator, push operator object onto the thisExprStack Step 5 If you see ")" // steps in
   * evaluateCurrentOperation() : Step 6 Pop operands and push them onto thisOpStack until you find
   * an operator Step 7 Apply the operator to the operands on thisOpStack Step 8 Push the result
   * into thisExprStack Step 9 If you run out of tokens, the value on the top of thisExprStack is is
   * the result of the expression.
   */
  public double evaluate() {
    Scanner inputExprScanner = new Scanner(inputExpr);

    // Use zero or more white space as delimiter,
    // which breaks the string into single character tokens
    inputExprScanner = inputExprScanner.useDelimiter("\\s*");

    while (inputExprScanner.hasNext()) {

      if (inputExprScanner.hasNextInt()) {
        String dataString = inputExprScanner.findInLine("\\d+");
        thisExprStack.push(new Double(dataString));
      } else {
        String aToken = inputExprScanner.next();
        char item = aToken.charAt(0);
        String nextToken;
        char nextItem;

        switch (item) {
          case '(':
            nextToken = inputExprScanner.next();
            nextItem = nextToken.charAt(0);
            if (nextItem == '+' || nextItem == '-' || nextItem == '*' || nextItem == '/') {
              thisExprStack.push(nextItem);
            }
            break;

          case ')':
            try {
              evaluateCurrentOperation();
            } catch (LispExprEvaluatorException e) {
              System.out.println("Error: " + e);
            } catch (ArithmeticException ae) {
              System.out.println("Error: " + ae);
            }
            break;

          default: // error
            throw new LispExprEvaluatorException(item + " is not a legal expression operator");
        } // end switch
      } // end else
    } // end while

    double result = (Double) thisExprStack.pop();

    return result;
  }
 public static void main(final String[] args) {
   final Scanner sis = new Scanner(System.in);
   output(HELP_MESSAGE);
   while (sis.hasNext()) {
     if (sis.hasNextInt()) {
       final int next = sis.nextInt();
       output("You entered an Integer = %d", next);
     } else if (sis.hasNextLong()) {
       final long next = sis.nextLong();
       output("You entered a Long = %d", next);
     } else if (sis.hasNextDouble()) {
       final double next = sis.nextDouble();
       output("You entered a Double = %f", next);
     } else if (sis.hasNext("\\d+")) {
       final BigInteger next = sis.nextBigInteger();
       output("You entered a BigInteger = %s", next);
     } else if (sis.hasNextBoolean()) {
       final boolean next = sis.nextBoolean();
       output("You entered a Boolean representation = %s", next);
     } else if (sis.hasNext(DATE_PATTERN)) {
       final String next = sis.next(DATE_PATTERN);
       output("You entered a Date representation = %s", next);
     } else // unclassified
     {
       final String next = sis.next();
       if (isValidURL(next)) {
         output("You entered a valid URL = %s", next);
       } else {
         if (EXIT_COMMANDS.contains(next)) {
           output("Exit command %s issued, exiting!", next);
           break;
         } else if (HELP_COMMANDS.contains(next)) {
           output(HELP_MESSAGE);
         } else {
           output("You entered an unclassified String = %s", next);
         }
       }
     }
   }
   /*
      This will close the underlying Readable, in this case System.in, and free those resources.
      You will not be to read from System.in anymore after this you call .close().
      If you wanted to use System.in for something else, then don't close the Scanner.
   */
   sis.close();
   System.exit(0);
 }
Esempio n. 22
0
  /*
   *	Retourne le nombre d'allumettes à prendre sur le plateau en accord avec la stratégie employée.
   *
   *	@param jeu Le jeu sur lequel appliquer la stratégie.
   */
  public int getPrises(Jeu jeu) {

    Scanner scanner = new Scanner(System.in);
    String input;
    int nombre = 0;

    System.out.print("Combien prenez-vous d'allumettes ? ");
    if (scanner.hasNextInt()) {
      nombre = scanner.nextInt();
    } else {
      System.out.println("Vous devez prendre un entier compris entre 1 et 3.");

      nombre = this.getPrises(jeu);
    }

    return nombre;
  }
Esempio n. 23
0
  public static void main(String[] args) throws IOException {
    Scanner scanner = new Scanner(new BufferedInputStream(System.in));

    while (scanner.hasNextInt()) {
      int a = scanner.nextInt();
      if (a == 0) break;
      while (a / 10 != 0) {
        int sum = 0;
        while (a / 10 != 0) {
          sum = sum + a % 10;
          a = a / 10;
        }
        a = a + sum;
      }
      System.out.println(a);
    }
  }
  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    TreeMap<String, Integer> keyMaterials =
        new TreeMap<String, Integer>() {
          {
            put("shards", 0);
            put("fragments", 0);
            put("motes", 0);
          }
        };

    TreeMap<String, Integer> junk = new TreeMap<>();

    while (scan.hasNextInt()) {
      int quantity = scan.nextInt();
      String material = scan.next().toLowerCase();

      // junk
      if (!(material.equals("shards")
          || material.equals("fragments")
          || material.equals("motes"))) {

        if (!(junk.containsKey(material))) {
          junk.put(material, quantity);
        } else {
          int previousQuantity = junk.get(material);
          junk.put(material, previousQuantity + quantity);
        }
      }

      // key materials
      else {
        if (!(keyMaterials.containsKey(material))) {
          keyMaterials.put(material, quantity);
        } else {
          int previousQuantity = keyMaterials.get(material);
          keyMaterials.put(material, previousQuantity + quantity);
        }
      }

      checkKeyMaterials(keyMaterials, junk);
      if (boughtItem) return;
    }
  }
Esempio n. 25
0
  public static void main(String[] args) throws IOException {
    Scanner scanner = new Scanner(new BufferedInputStream(System.in));
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    int n = 1;
    while (scanner.hasNextInt()) {
      int a = scanner.nextInt();
      int b = scanner.nextInt();
      if (a == 0) break;
      BigInteger sum = BigInteger.ZERO;
      BigInteger bb = BigInteger.valueOf(b);
      for (int i = 0; i < a; i++) sum = sum.add(scanner.nextBigInteger());

      System.out.println(
          "Bill #" + n + " costs " + sum + ": each friend should pay " + sum.divide(bb) + "\n");
      n++;
    }
  }
  public static void main(String[] args) throws Exception {
    Random rnd = new Random();
    for (int times = 0; times < 1000; ++times) {
      System.out.println("times = " + times);
      int n = 0;
      if (args.length == 1) {
        n = Integer.parseInt(args[0]);
      } else {
        n = rnd.nextInt(10000) + 1;
      }
      List<Integer> A = new ArrayList<>();
      for (int i = 0; i < n; i++) {
        A.add(rnd.nextInt(999999 + 1));
      }

      PrintWriter ofs = null;
      try {
        ofs = new PrintWriter(new FileWriter("input.txt"));
        for (Integer a : A) {
          ofs.println(a);
        }
      } finally {
        ofs.close();
      }
      indirectSort("input.txt");

      Scanner ifs = null;
      File input = new File("input.txt");
      try {
        ifs = new Scanner(input);
        A.clear();
        while (ifs.hasNextInt()) {
          A.add(ifs.nextInt());
        }
      } finally {
        ifs.close();
      }

      assert (isSorted(A));
      input.delete();
    }
  }
Esempio n. 27
0
  public static void main(String[] args) throws IOException {
    Scanner scanner = new Scanner(new BufferedInputStream(System.in));
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    while (scanner.hasNextInt()) {
      int n = scanner.nextInt();
      int k = scanner.nextInt();

      BigInteger sum = BigInteger.ONE;

      for (int i = n; i > n - k; i--) {
        sum = sum.multiply(BigInteger.valueOf(i));
      }
      for (int i = 2; i <= k; i++) {
        sum = sum.divide(BigInteger.valueOf(i));
      }

      System.out.println(sum.toString().length());
    }
  }
  public static void main(String[] args) {
    final int N = 5;
    int[] ints = new int[N];
    float[] floats = new float[N];

    Scanner input = new Scanner(System.in);
    int numInts = 0;
    int numFloats = 0;
    boolean cont = true;

    while (input.hasNext() && cont) {
      if (input.hasNextInt()) {
        if (numInts == N) {
          cont = false;
        } else {
          ints[numInts] = input.nextInt();
          numInts++;
        }
      } else if (input.hasNextFloat()) {
        if (numFloats == N) {
          cont = false;
        } else {
          floats[numFloats] = input.nextFloat();
          numFloats++;
        }
      } else {
        cont = false;
      }
    }
    System.out.println("");
    System.out.print("Integers: ");
    for (int i = 0; i < numInts; i++) {
      System.out.print(ints[i] + " ");
    }
    System.out.println("");
    System.out.print("Floats: ");
    for (int i = 0; i < numFloats; i++) {
      System.out.print(floats[i] + " ");
    }
    System.out.println("\n");
  }
Esempio n. 29
0
  /**
   * Method parse will parse the {@link #print()} String representation of a Tuple instance and
   * return a new Tuple instance.
   *
   * @param string of type String
   * @return Tuple
   */
  public static Tuple parse(String string) {
    if (string == null || string.length() == 0) return null;

    string = string.replaceAll("^ *\\[*", "");
    string = string.replaceAll("\\]* *$", "");

    Scanner scanner = new Scanner(new StringReader(string));
    scanner.useDelimiter("(' *, *')|(^ *')|(' *$)");

    Tuple result = new Tuple();

    while (scanner.hasNext()) {
      if (scanner.hasNextInt()) result.add(scanner.nextInt());
      else if (scanner.hasNextDouble()) result.add(scanner.nextDouble());
      else result.add(scanner.next());
    }

    scanner.close();

    return result;
  }
Esempio n. 30
0
  public void parseInput(String file, int threads, int limit)
      throws FileNotFoundException, InterruptedException {
    // long startParseTime = System.currentTimeMillis();
    m_jgAdapter = new JGraphModelAdapter<Position, DefaultEdge>(graph);
    jgraph = new JGraph(m_jgAdapter);
    this.threads = threads;

    Scanner input = new Scanner(new File(file));
    try {
      for (int r = 0; input.hasNextLine() && r < limit; r++) {
        Scanner line = new Scanner(input.nextLine());
        try {

          ArrayList<Position> row = new ArrayList<Position>();
          grid.add(row);

          System.out.println("Row " + r);

          for (int c = 0; line.hasNextInt() && c < limit; c++) {
            Position position = new Position(r, c, line.nextInt());
            row.add(position);
            graph.addVertex(position);
            positionVertexAt(position, position.column * 5, position.row * 5);
          }
        } finally {
          line.close();
        }
      }
    } finally {
      input.close();
    }

    graphGrid(grid);

    // ArrayList<ArrayList<Position>> grid2 = transpose(grid);
    // outputGrid(grid2);

  }