java.util.scanner

Using Scanner.next() to get text input

心已入冬 提交于 2019-12-23 12:46:25
问题 I am trying to get text input from the keyboard in Java 6. I am new to the language and whenever i run the following code, I get this error: package test1; import java.util.Scanner; public class Test { public static void main(String[] args) { boolean quit = false; while (!quit){ Scanner keyIn; String c = "x"; while (c != "y" && c != "n") { keyIn = new Scanner(System.in); c = keyIn.next(); keyIn.close(); } if (c == "n") quit = true; } } } Exception in thread "main" java.util

Does closing Scanner affect performance

人走茶凉 提交于 2019-12-23 12:42:48
问题 I was solving a competitive problem and in problem, I was taking user input using scanner. These are 2 code segments, one closing scanner and one without closing scanner. Closing scanner import java.util.Scanner; public class JImSelection { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = Integer.valueOf(scanner.nextLine()); while (n-- > 0) { double number = (Math.log(Long.valueOf(scanner.nextLine())) / Math.log(2)); System.out.println((int) number -

Data Validation and Scanners in Java

只谈情不闲聊 提交于 2019-12-23 12:34:54
问题 I have a question regarding data validation and scanners.The following piece of code checks userinput.Anything other than an integer is not allowed and the user is asked to re-enter a value.My question is that the code works only if the scanner is declared within the while loop.The program executes infinitely if the scanner is declared outside.Why is that?Thanks. int UserInp; boolean dataType=false; while(dataType==false) { Scanner sc=new Scanner(System.in); try { System.out.print("\nEnter a

Error while redirecting the input from file

自古美人都是妖i 提交于 2019-12-22 11:28:35
问题 Here's my code: import java.util.Scanner; class Graph{ boolean [][]array; int N; Graph (){ array = new boolean [1001][1001]; N=0; } void read_graph() { Scanner sc = new Scanner(System.in); N = sc.nextInt(); sc.nextLine(); String str; String []substr; for (int K=1; K<=N; K++){ str = sc.nextLine(); substr = str.split(" "); for (int I=0; I<substr.length; I++) System.out.println(substr[0]+" "+substr[I]); } } void query(){ Scanner sc = new Scanner(System.in); int P, Q; int counter = 0; boolean

Getting input from user in Console without using Scanner

非 Y 不嫁゛ 提交于 2019-12-22 09:39:26
问题 I would like to know about other ways of getting input from users using other classes like BufferedReader ,etc rather than using Scanner class. So, was there any other way of getting input from the user? If so, was it efficient than Scanner class? 回答1: if you are using the Java SE6 or higher then you can make use of Console clas Console console = System.console(); if (console==null){ System.out.print("console not available "); }else { String line = console.readLine("Enter name :"); System.out

Java Scanner does not wait for input

Deadly 提交于 2019-12-22 08:54:10
问题 I have two blocks of code here. One scanner properly waits user input, and the other just blows right through it and calls nextInt() which returns a NoSuchElementException . Here is the block that works: public void startGame() { out.println("Player1: 1 for dumb player, 2 for smart player, 3 for human player."); Scanner scan = new Scanner(System.in); p = scan.nextInt(); if (p == 1) p1 = new DumbPlayer("ONE"); if (p == 2) p1 = new SmartPlayer("ONE"); else p1 = new HumanPlayer("ONE"); out

Find the number of unordered anagramic pairs of substrings

情到浓时终转凉″ 提交于 2019-12-21 21:02:10
问题 I am trying to solve the following question: https://www.hackerrank.com/challenges/sherlock-and-anagrams This is my code import java.util.*; public class Solution { public static boolean check(String s1,String s2) { int [] count1 = new int[26]; for( int i = 0; i < s1.length(); i++ ) { char ch1 = s1.charAt(i); count1[ch1-'a']++; } int [] count2 = new int[26]; for( int i = 0; i < s2.length(); i++ ) { char ch2 = s2.charAt(i); count2[ch2-'a']++; } int count =0; for(int j=0;j<26;j++) { count =

Read input line by line

不羁岁月 提交于 2019-12-21 07:06:10
问题 How do I read input line by line in Java? I searched and so far I have this: import java.util.Scanner; public class MatrixReader { public static void main(String[] args) { Scanner input = new Scanner(System.in); while (input.hasNext()) { System.out.print(input.nextLine()); } } The problem with this is that it doesn't read the last line. So if I input 10 5 4 20 11 6 55 3 9 33 27 16 its output will only be 10 5 4 20 11 6 55 3 回答1: Ideally you should add a final println() because by default

Adding an object from file into program

一个人想着一个人 提交于 2019-12-20 07:26:15
问题 I'm programming a game and in the program I need to add new enemies based off of a file. Right now my problem is that I've run into an infinite while loop when trying to read this file. I'm relatively new to programming so I'm not exactly sure how to fix this. Here is the problem code. An example of how the entry in the file looks is: "Troll,6,4,1". Thank you for your help. try { Scanner input = new Scanner(new File(filename)); while(input.hasNext()); { input.useDelimiter(",|\n"); String name

Printing a replaced line in a new text file

只愿长相守 提交于 2019-12-20 07:21:19
问题 I am trying to edit a matlabfile and replace some of the coding parts init in some specific lines. However, using the format below to make the changes it wont change the line context at all. (it will print the same old line). Any idea what am I doing wrong? 'replaceAll' is not suitable to replace some words with some others in the lines? Thanks in advance. try { PrintWriter out = new PrintWriter(new FileWriter(filenew, true)); Scanner scanner = new Scanner(file); while (scanner.hasNextLine())