public static void main(String[] args) { try { BufferedReader br = new BufferedReader(new FileReader(args[0])); int tIndex = 0; int pIndex = 0; // This will probably change soon (name and implementation) NgramParser tnp = new NgramParser(args[1]); ArrayList<String> triplet = tnp.getTriplet(); ArrayList<String> polarity = tnp.getPolarity(); FileWriter sw = new FileWriter(args[2]); String line = null; while (((line = br.readLine()) != null) && (tIndex < triplet.size()) && (pIndex < polarity.size())) { if (line.matches("^[\\d]*:")) { // System.out.println(line); sw.write(line + "\n"); } else { Scanner sc = new Scanner(line); String trip = sc.findInLine(Pattern.compile("[a-zA-Z]+#[a-z]+[#]?[0-9]*")); // if (trip != null && trip.equals(triplet.get(tIndex))) { System.out.println(trip); if (trip != null && !trip.toLowerCase().contains("no#cl")) { // System.out.println(triplet.get(tIndex) + ":" +polarity.get(pIndex)); String pol = polarity.get(pIndex); sw.write(line + " " + pol + "\n"); sc.close(); tIndex++; pIndex++; } else { String pol = "neg"; sw.write("no#a#1" + " " + pol + "\n"); sc.close(); } } // sw.flush(); } sw.close(); } catch (IOException e) { e.printStackTrace(); } }
public void init() { Scanner scan = new Scanner(System.in); n = scan.nextInt(); m = scan.nextInt(); for (int i = 0; i < n; i++) { String input = scan.next(); record.add(input); } for (int i = 0; i < m; i++) { String input = scan.next(); char[] charArray = input.toCharArray(); boolean isValid = false; for (int j = 0; j < charArray.length; j++) { char c = charArray[j]; for (char d = 'a'; d <= 'c'; d = (char) (d + 1)) { if (d == c) continue; charArray[j] = d; if (record.contains(new String(charArray))) { isValid = true; break; } } charArray[j] = c; if (isValid) break; } if (isValid) System.out.println("YES"); else System.out.println("NO"); } scan.close(); }
public void init() { Scanner scan = new Scanner(System.in); count = scan.nextInt(); x0 = scan.nextLong(); y0 = scan.nextLong(); int result = 0; boolean special = false; for (int i = 0; i < count; i++) { long tempx = scan.nextLong(); long tempy = scan.nextLong(); if (tempx == x0 && tempy == y0) { special = true; continue; } boolean isDuplicate = false; for (int j = 0; j < result; j++) { long x1 = xList.get(j); long y1 = yList.get(j); if ((x1 - x0) * (tempy - y0) == (y1 - y0) * (tempx - x0)) { isDuplicate = true; break; } } if (!isDuplicate) { xList.add(tempx); yList.add(tempy); result++; } } if (special && result == 0) result = 1; System.out.println(result); scan.close(); }
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(); }
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(); }
public static void main(String[] args) { Scanner in = new Scanner(System.in); int T; T = in.nextInt(); while ((T--) > 0) { int n = in.nextInt(), k = in.nextInt(); int value[] = new int[n]; for (int i = 0; i < n; i++) { value[i] = in.nextInt(); } System.out.println(UKS(value, n, k)); } in.close(); }
public static void main(String[] args) throws FileNotFoundException, IOException { JOptionPane myWindow; myWindow = new JOptionPane(); String fileName; fileName = myWindow.showInputDialog("Enter External Text File to Open;"); Scanner scanner = new Scanner(new File(fileName)); JTextArea outputOriginalFile = new JTextArea(); outputOriginalFile.setText("The Original File was:\n"); JTextArea outputTextArea = new JTextArea(); while (scanner.hasNext()) { String fullName = scanner.nextLine(); outputOriginalFile.append(fullName + "\n"); } myWindow.showMessageDialog( null, outputOriginalFile, "Opened file....", myWindow.INFORMATION_MESSAGE); PrintWriter pw = new PrintWriter(new File("outputOfFile.txt")); String firstName, middleName, lastName; JTextArea outputTextArea1 = new JTextArea(); outputTextArea1.setText("The Modified Text File is:\n"); Scanner scanner2 = new Scanner(new File(fileName)); while (scanner2.hasNext()) { String fullName = scanner2.nextLine(); StringTokenizer st = new StringTokenizer(fullName, " "); firstName = st.nextToken(); middleName = st.nextToken(); lastName = st.nextToken(); pw.println(lastName + ", " + firstName + " " + middleName.substring(0, 1) + "."); outputTextArea1.append( lastName + ", " + firstName + " " + middleName.substring(0, 1) + "." + "\n"); } myWindow.showMessageDialog(null, outputTextArea1, "Results....", myWindow.INFORMATION_MESSAGE); scanner.close(); scanner2.close(); pw.close(); }
public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.close(); String s = ""; for (int i = 0; i < n; i++) { s = ""; for (int j = 0; j < n - i - 1; j++) { s += " "; } for (int k = 0; k < i + 1; k++) { s += "#"; } System.out.println(s); } }
private String getFileWords(String s) { String output = ""; try { // THIS CODE SEGMENT WILL NOT RUN IN AN APPLET THAT IS NOT "SIGNED" // By default applets cannot alter files on the hard drive of the user. // If the panel is opened in a regular application the save button should work. // This can be tested in HomeworkApplication.java through HomeworkRun.java only File inputFile = new File(s); Scanner inputScanner = new Scanner(inputFile); while (inputScanner.hasNext()) { output = output + inputScanner.nextLine() + "\n"; } inputScanner.close(); String numWords = getWords(output); return numWords; } catch (IOException e) { outputLabel.append(e + ""); } return output; }
public static void main(String[] args) { Scanner sc = null; Scanner stringReader = null; try { int ch = 0; sc = new Scanner(System.in); boolean validFile = false; stringReader = new Scanner(System.in); System.out.println("\n\t\t\tCONTACTS APP"); System.out.println( "--------------------------------------------------------------------------------"); while (ch != 5) { System.out.println("\nTO CREATE PHONEBOOK->PRESS 1"); System.out.println("TO LOAD A CREATED PHONEBOOK->PRESS 2"); System.out.println("TO DELETE CREATED PHONEBOOK->PRESS 3"); System.out.println("TO LIST ALL PHONEBOOKS->PRESS 4"); System.out.println("TO EXIT->PRESS 5"); while (!sc.hasNextInt()) { System.out.println("\nONLY NUMBERS RANGED FROM 1 - 5 SHOULD BE ENTERED..RETRY"); sc.nextLine(); } ch = sc.nextInt(); switch (ch) { case 1: System.out.println( "\nENTER NAME OF PHONEBOOK(Ex MyPhoneBook).DONT GIVE ANY EXTENSION."); validFile = false; String contactBookName = ""; while (!validFile) { contactBookName = stringReader.nextLine(); if (contactBookName.equals("")) { System.out.println("\nPHONEBOOK NAME MUST NOT BE EMPTY"); System.out.println( "\nENTER NAME OF PHONEBOOK(Ex MyPhoneBook).DONT GIVE ANY EXTENSION."); continue; } if (!(contactBookName.contains("/") || contactBookName.contains("\\") || contactBookName.contains(":") || contactBookName.contains("*") || contactBookName.contains("?") || contactBookName.contains("/") || contactBookName.contains("\"") || contactBookName.contains("<") || contactBookName.contains(">") || contactBookName.contains("|"))) { String extension[] = contactBookName.split("."); contactBookName = contactBookName + ".txt"; File contactBook = new File(contactBookName); if (!contactBook.exists()) { System.out.println("\nNEW PHONEBOOK CREATED::" + contactBookName); validFile = true; } else { System.out.println( "\nTHIS PHONEBOOK NAMED " + contactBookName + " ALREADY EXISTS\nPLEASE ENTER A DIFFERENT NAME"); } } else { System.out.println("\nFILE NAME CANNOT CONTAIN : /,\\,\",*,?,<,>,|"); System.out.println( "\nENTER NAME OF CONTACTS BOOK TO CREATE WITHOUT FILE EXTENSION "); } } contactBookOperations(contactBookName); break; case 2: contactBookName = ""; System.out.println( "\nENTER NAME OF PHONEBOOK(Ex MyPhoneBook) TO LOAD.DONT GIVE ANY EXTENSION."); contactBookName = stringReader.nextLine(); while (true) { if (contactBookName.equals("")) { System.out.println("\nPHONEBOOK NAME MUST NOT BE EMPTY "); } else { break; } } contactBookName += ".txt"; File contactBook = new File(contactBookName); if (!contactBook.exists()) { System.out.println("\nA PHONEBOOK WITH NAME " + contactBookName + " DOES NOT EXIST"); } else { contactBookOperations(contactBookName); } break; case 3: contactBookName = ""; System.out.println("\nENTER NAME OF PHONEBOOK WITHOUT EXTENSION TO DELETE"); contactBookName = stringReader.nextLine(); if (contactBookName.equals("")) { System.out.println("\nPHONEBOOK NAME CANNOT BE EMPTY "); } contactBookName += ".txt"; contactBook = new File(contactBookName); if (!contactBook.exists()) { System.out.println("\nA PHONEBOOK WITH NAME " + contactBookName + " DOES NOT EXIST"); } else { contactBook.delete(); System.out.println("\nA PHONEBOOK " + contactBookName + " DELETED"); } break; case 4: String curDirectory = System.getProperty("user.dir"); listPhoneBooks(curDirectory); break; case 5: System.out.println("\nSUCCESSFULLY EXITED"); break; default: System.out.println("\nENTER ONLY NUMBERS RANGED FROM 1 - 5"); break; } } } catch (Exception e) { System.out.println("AN ERROR OCCURED!!"); e.printStackTrace(); } finally { if (sc != null) sc.close(); if (stringReader != null) stringReader.close(); } }