public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Please enter your name :"); String userName = sc.nextLine(); System.out.print("Wow, " + userName); System.out.print(", your name is " + userName.length()); System.out.println(" characters long (if I count the spaces)."); // now count how many alphabetic characters there are in the name String lowerCaseName = userName.toLowerCase(); // counter for the number of letters int count = 0; for (int i = 0; i < lowerCaseName.length(); i++) { // if the character is alphabetic, then increment count char letter = lowerCaseName.charAt(i); if ((letter >= 'a' && letter <= 'z')) count++; } System.out.println("There are " + count + " letters in your name"); for (int i = lowerCaseName.length() - 1; i >= 0; i--) { System.out.print(lowerCaseName.charAt(i)); } System.out.println(""); // find the position of the space String firstName; int spacePos = userName.indexOf(' '); if (spacePos == -1) firstName = userName; else firstName = userName.substring(0, spacePos); System.out.println("First name :" + firstName); }
/** * Moves and archives old, non-executed transactions into an archive file. * * <p>Makes sure the file to archive to doesn't exist, and shows amount of archived transactions */ private static void arkiveraGamlaTransaktioner() { File archive; System.out.print("Mata in ett nytt filnamn att arkivera till: "); archive = new File(tbScanner.nextLine()); while (archive.exists()) { System.out.print("Filen existerar redan; var god mata in ett nytt filnamn: "); archive = new File(tbScanner.nextLine()); } tbScanner.reset(); System.out.println("Sparar till: " + archive); // Hämtar datum en vecka tillbaka Calendar c = Calendar.getInstance(); c.add(Calendar.WEEK_OF_YEAR, -1); // Försöker arkivera till den nya filen try { int antalArkiveradeTransaktioner; antalArkiveradeTransaktioner = m.archiveTransactions(c.getTime(), archive); System.out.println("Arkiverade " + antalArkiveradeTransaktioner + " transaktioner"); } catch (IOException e) { System.out.println("Kunde inte skriva till arkivfilen!"); } } // s**t på arkiveraGamlaTransaktioner
public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); AdvancedArithmetic myCalculator = new Calculator(); int sum = myCalculator.divisorSum(n); System.out.println("I implemented: AdvancedArithmetic\n" + sum); }
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(); }
private void updateWifiState(int state) { Activity activity = getActivity(); if (activity != null) { activity.invalidateOptionsMenu(); } switch (state) { case WifiManager.WIFI_STATE_ENABLED: // this function only returns valid results in enabled state mIbssSupported = mWifiManager.isIbssSupported(); mSupportedChannels = mWifiManager.getSupportedChannels(); mScanner.resume(); return; // not break, to avoid the call to pause() below case WifiManager.WIFI_STATE_ENABLING: addMessagePreference(R.string.wifi_starting); break; case WifiManager.WIFI_STATE_DISABLED: setOffMessage(); break; } mLastInfo = null; mLastState = null; mScanner.pause(); }
public static String requestName() { Scanner keyboard = new Scanner(System.in); System.out.print("Name: "); String name = keyboard.nextLine(); keyboard.close(); return name; }
public static void main(String[] args) throws ParseException { Scanner scan = new Scanner(System.in); String s1 = scan.nextLine(); String s2 = scan.nextLine(); Dm dm = new Dm(s1, s2); System.out.println(dm.getDifference()); }
private int generateIndex(String content, int docid) { Scanner s = new Scanner(content); // Uses white space by default. int totalcount = 0; while (s.hasNext()) { ++_totalTermFrequency; ++totalcount; String token = s.next(); // decrement the size() by 1 as the real doc id // int did=_documents.size()-1; Vector<Integer> plist = _index.get(token); if (plist != null) { if (plist.lastElement() != docid) { plist.add(docid); _index.put(token, plist); } } else { // _terms.add(token); Vector<Integer> p = new Vector<Integer>(); p.add(docid); _index.put(token, p); } } return totalcount; }
public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Introduce el número de empleados: "); int empleados = scan.nextInt(); int num = 1; int salarioAlto = 0, salarioBajo = 0; double importe = 0; while (num <= empleados) { System.out.print("Introduzca el salario del empleado " + num + ": "); double salario = scan.nextDouble(); importe += salario; if (salario >= 100 && salario < 300) { salarioBajo++; } else { salarioAlto++; } num++; } System.out.println("Hay " + salarioBajo + " personas que cobran entre $100 y $300."); System.out.println("Hay " + salarioAlto + " personas que cobran más de $300."); System.out.println("El importe de los sueldos al personal es de: $" + importe); }
/** * @param args * @throws FileNotFoundException */ public static void main(String[] args) throws FileNotFoundException { // TODO Auto-generated method stub System.out.println("Swim Meet Planner"); // scanner to read file Scanner fileReader = new Scanner(new File("src/Finalprojectinput")); // create results Map HashMap<String, ArrayList<Integer>> results = new HashMap<String, ArrayList<Integer>>(); // adding names and times to results Map while (fileReader.hasNext()) { String name = fileReader.next(); int time = fileReader.nextInt(); // does swimmer exist in map? if (results.containsKey(name)) { results.get(name).add(time); } else { ArrayList<Integer> times = new ArrayList<Integer>(); times.add(time); results.put(name, times); } } fileReader.close(); Average a = new Average(); a.average(results); }
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(); }
void read() { maze = new char[MAX][MAX]; height = 0; Scanner scanner = new Scanner(System.in); while (scanner.hasNextLine()) { String s = scanner.nextLine(); width = s.length(); for (int j = 0; j < width; j++) { maze[height][j] = s.charAt(j); } height++; } for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (maze[i][j] == 'R') { startRow = i; startCol = j; maze[i][j] = ' '; } if (maze[i][j] == 'X') { goalRow = i; goalCol = j; maze[i][j] = ' '; } } } }
/** Reads in the letter location data for the word hunt puzzle */ public static void readPuzzle() { letters = new String[3][3][2]; try { Scanner reader = new Scanner(new File("graph.txt")); while (reader.hasNext()) { String temp = reader.next(); // if the String is not a number it is a character if (!NUMBERS.contains(temp)) { // find the coordinates of the letter/String int x = reader.nextInt(); int y = reader.nextInt(); int z = reader.nextInt(); letters[x][y][z] = temp; } } // prints out the word hunt set up, in separate squares that represent the 2D arrays at each // depth for (int k = 0; k < 2; k++) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { System.out.print(letters[i][j][k] + " "); } System.out.print("\n"); } System.out.print("\n"); } } catch (FileNotFoundException e) { System.out.println("No such file."); } }
/** Creates hashtable to store all the words in the dictionary */ public static void createHashTable() { try { Scanner fileReader = new Scanner(new File("largedictionary.txt")); // read in every word from the dictionary file while (fileReader.hasNext()) { String theWord = fileReader.next(); // create hash index for the word int index = toHash(theWord); // linear probing if the hashtable cell is occupied while (hashtable[index] != null) { index += 1; // if we somehow hit the end of the hashtable, go back to the beginning if (index == TABLE_SIZE) { index = 0; } } hashtable[index] = theWord; } } catch (FileNotFoundException e) { System.out.println("File not found."); } }
public static void main(String args[]) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; ++i) arr[i] = in.nextInt(); int count = 0; for (int j = 1; j < n; ++j) { int key = arr[j]; int i = j - 1; while (i >= 0 && arr[i] > key) { arr[i + 1] = arr[i]; --i; ++count; } arr[i + 1] = key; } System.out.println(count); }
// Ask for user input public char userInput() { Scanner input = new Scanner(System.in); char letter = ' '; System.out.println("Guess a letter: "); letter = input.nextLine().charAt(0); // store only first letter from the user input return letter; }
public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int v = input.nextInt(); boolean[] canBuy = new boolean[n]; int canBuyCounter = 0; for (int i = 0; i < n; i++) { int n1 = input.nextInt(); int[] prices = new int[n1]; for (int j = 0; j < n1; j++) { prices[j] = input.nextInt(); } for (int j : prices) { if (v > j) { canBuyCounter++; canBuy[i] = true; break; } } } System.out.println(canBuyCounter); boolean firstPrint = true; for (int i = 0; i < n; i++) { if (canBuy[i]) { if (firstPrint) { firstPrint = false; } else { System.out.print(" "); } System.out.print((i + 1)); } } System.out.println(); }
public static void main(String[] args) { // TODO Auto-generated method stub Scanner ler = new Scanner(System.in); System.out.printf("informe o nome do arquivo de texto: \n"); String nome = ler.nextLine(); System.out.printf("\nconteudo do arquivo texto"); try { FileReader arq = new FileReader(nome); BufferedReader lerArq = new BufferedReader(arq); String linha = lerArq.readLine(); while (linha != null) { System.out.printf("%s\n", linha); linha = lerArq.readLine(); String val1 = linha.substring(0, linha.indexOf(',')); int a = Integer.parseInt(val1); String val2 = linha.substring(linha.indexOf(',') + 1, linha.lastIndexOf(',')); int b = Integer.parseInt(val2); String val3 = linha.substring(linha.lastIndexOf(',') + 1, linha.length()); int c = Integer.parseInt(val3); setValida(new ValidaTriangulo(a, b, c)); } arq.close(); } catch (IOException e) { System.out.printf("erra na abertura do arquivo: %s.n", e.getMessage()); } System.out.println(); }
@Override public int corpusDocFrequencyByTerm(String term) { String indexFile = _options._indexPrefix + "/merge.txt"; try { BufferedReader reader = new BufferedReader(new FileReader(indexFile)); String line; while ((line = reader.readLine()) != null) { int termDocFren = 0; String title = ""; String data = ""; Scanner s = new Scanner(line).useDelimiter("\t"); while (s.hasNext()) { title = s.next(); data = s.next(); } if (title.equals(term)) { String[] docs = data.split("\\|"); termDocFren = docs.length; } reader.close(); return termDocFren; } reader.close(); } catch (Exception e) { e.printStackTrace(); } return 0; }
public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); long d3 = 3, d5 = 5, d15 = 15; long n3 = 0, n5 = 0, n15 = 0; if ((1 <= T) && (T <= 100000)) { for (int j = 0; j < T; j++) { long sum3 = 0, sum5 = 0, sum15 = 0, sum = 0; long l3, l5, l15 = 0; int N = sc.nextInt(); if ((1 <= N) && (N <= 1000000000)) { N = N - 1; l3 = N % 3; l3 = N - l3; n3 = (l3 - 3) / 3 + 1; sum3 = (3 * ((n3) + 1) * (n3)) / 2; l5 = N % 5; l5 = N - l5; n5 = (l5 - 5) / 5 + 1; sum5 = (5 * ((n5) + 1) * (n5)) / 2; l15 = N % 15; l15 = N - l15; n15 = (l15 - 15) / 15 + 1; sum15 = (15 * ((n15) + 1) * (n15)) / 2; sum = sum3 + sum5 - sum15; System.out.println(sum); } } } }
public static void main(String[] args) { Scanner sc = new Scanner(System.in); String title = sc.nextLine(); MyBook new_novel = new MyBook(); new_novel.setTitle(title); System.out.println("The title is: " + new_novel.getTitle()); }
public static void main(String args[]) throws Exception { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ Scanner in = new Scanner(System.in); int n = in.nextInt(); SuperStack stack = new SuperStack(); PrintWriter out = new PrintWriter(System.out, true); for (int i = 0; i < n; i++) { String s = in.next(); if (s.equals("push")) { int value = in.nextInt(); out.println(stack.push(value)); } else if (s.equals("pop")) { int res = stack.pop(); if (stack.size == 0) { out.println("EMPTY"); } else { out.println(res); } } else if (s.equals("inc")) { int x = in.nextInt(); int d = in.nextInt(); out.println(stack.inc(x, d)); } } out.close(); }
public static void main(String args[]) { Scanner input = new Scanner(System.in); int T = input.nextInt(); CPoint[] array = new CPoint[SIZE]; for (int t = 0; t < T; t++) { int n = input.nextInt(); for (int i = 0; i < n; i++) { int a = input.nextInt(); int b = input.nextInt(); array[i] = new CPoint(); array[i].setPoint(a, b); } int ans = 0; for (int i = 0; i < n; i++) { int sum = 0; for (int j = 0; j < n; j++) if (i != j) { if (array[j].x > array[i].x && array[j].y > array[i].y) sum++; if (array[j].x < array[i].x && array[j].y < array[i].y) sum++; } if (sum > ans) ans = sum; } System.out.println(ans); } }
public static void main(String[] args) { try { flraf = new FLRAF(28); sc = new Scanner(new File("btree/words.txt")); } catch (FileNotFoundException f) { System.out.println(f); } while (sc.hasNext()) flraf.write(sc.next()); System.out.println("Block 0: " + flraf.read(0)); System.out.println("Block 5643: " + flraf.read(5643)); System.out.println("Block 45406: " + flraf.read(45406)); sc = new Scanner(System.in); System.out.print("Another? > "); while (sc.nextLine().equalsIgnoreCase("y")) { System.out.print("Enter index to read > "); String s = sc.nextLine(); if (s.contains(",")) { Integer j = Integer.parseInt(s.substring(0, s.lastIndexOf(","))); Integer k = Integer.parseInt(s.substring(s.lastIndexOf(",") + 1)); String[] st = flraf.read(j, k); System.out.println("Blocks : " + j + " - " + k + " : "); for (int i = 0; i < st.length; i++) if (st[i] != null) System.out.println(st[i]); else System.out.println("Index out of range"); } else { Integer i = Integer.parseInt(s); s = flraf.read(i); if (s != null) System.out.println("Block " + i + ": " + s); else System.out.println("Index out of range"); } System.out.print("Another?"); } }
private void updateConnectionState(DetailedState state) { /* sticky broadcasts can call this when wifi is disabled */ if (!mWifiManager.isWifiEnabled()) { mScanner.pause(); return; } if (state == DetailedState.OBTAINING_IPADDR) { mScanner.pause(); } else { mScanner.resume(); } mLastInfo = mWifiManager.getConnectionInfo(); if (state != null) { mLastState = state; } for (int i = getPreferenceScreen().getPreferenceCount() - 1; i >= 0; --i) { // Maybe there's a WifiConfigPreference Preference preference = getPreferenceScreen().getPreference(i); if (preference instanceof AccessPoint) { final AccessPoint accessPoint = (AccessPoint) preference; accessPoint.update(mLastInfo, mLastState); } } }
public static void main(String args[]) { Scanner s1 = new Scanner(System.in); int len = s1.nextInt(); int freq[] = new int[100001]; int starts[] = new int[100001]; int big = 0; int small = len + 1; int end = 0, size = 0, l = 0, r = 0; int curr; for (int i = 0; i < len; i++) { curr = s1.nextInt(); if (freq[curr] == 0) { starts[curr] = i; freq[curr] = 1; } else { (freq[curr])++; end = i; } if (freq[curr] >= big) { big = freq[curr]; size = (end) - starts[curr] + 1; if (size < small) { small = size; l = starts[curr] + 1; r = end + 1; } } } s1.close(); System.out.print(l + " " + r); }
// Start listener thread @Override public void run() { Scanner sc = new Scanner(System.in); String command; String[] tokens; // Listen to stdin commands while (true) { command = sc.nextLine(); System.out.println("[Shell Debug]: command: \"" + command + "\""); tokens = command.split(StreamingClient.DELIM); switch (tokens[0]) { case "help": System.out.println("Commands: "); System.out.println("\thelp - display this message"); System.out.println( "\trequest <arg1> <arg2> ... (whitespace separated) - send a request to the server"); System.out.println("\tclose - send a signal to close connection"); System.out.println("\tclear - clear console"); break; case "request": // Sends a request message break; case "close": StreamingClient.sendMessage("Close"); StreamingClient.setConnected(false); return; case "clear": clear(); break; } } }
public static void main(String[] args) { Scanner in = new Scanner(System.in); // Read in the number of elements int n = in.nextInt(); // Store the list of cities in an array int[] cities = new int[n]; for (int i = 0; i < n; i++) { cities[i] = in.nextInt(); } // Since the list is in ascending order (as specified in the problem // statement), the minimum difference will be to either the left or right // of the element, and the maximum will either be to the first or last // element for (int i = 0; i < n; i++) { int left = Integer.MAX_VALUE; int right = Integer.MAX_VALUE; if (i > 0) { left = Math.abs(cities[i] - cities[i - 1]); } if (i < n - 1) { right = Math.abs(cities[i] - cities[i + 1]); } int min = Math.min(left, right); left = Math.abs(cities[i] - cities[0]); right = Math.abs(cities[i] - cities[n - 1]); int max = Math.max(left, right); System.out.println(min + " " + max); } }
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); }
public static String openFile(String nameProgram) throws FileNotFoundException { Scanner scanner = new Scanner(new File(nameProgram)); String programString = ""; while (scanner.hasNext()) programString += scanner.next(); scanner.close(); return programString; }