user-input

JUnit testing with simulated user input

廉价感情. 提交于 2019-11-26 01:49:20
问题 I am trying to create some JUnit tests for a method that requires user input. The method under test looks somewhat like the following method: public static int testUserInput() { Scanner keyboard = new Scanner(System.in); System.out.println(\"Give a number between 1 and 10\"); int input = keyboard.nextInt(); while (input < 1 || input > 10) { System.out.println(\"Wrong number, try again.\"); input = keyboard.nextInt(); } return input; } Is there a possible way to automatically pass the program

JUnit testing with simulated user input

我们两清 提交于 2019-11-26 01:08:38
I am trying to create some JUnit tests for a method that requires user input. The method under test looks somewhat like the following method: public static int testUserInput() { Scanner keyboard = new Scanner(System.in); System.out.println("Give a number between 1 and 10"); int input = keyboard.nextInt(); while (input < 1 || input > 10) { System.out.println("Wrong number, try again."); input = keyboard.nextInt(); } return input; } Is there a possible way to automatically pass the program an int instead of me or someone else doing this manually in the JUnit test method? Like simulating the user

Spring MVC: How to perform validation?

二次信任 提交于 2019-11-26 00:56:54
问题 I would like to know what is the cleanest and best way to perform form validation of user inputs. I have seen some developers implement org.springframework.validation.Validator. A question about that: I saw it validates a class. Does the class have to be filled manually with the values from the user input, and then passed to the validator? I am confused about the cleanest and best way to validate the user input. I know about the traditional method of using request.getParameter() and then

raw_input in python without pressing enter

荒凉一梦 提交于 2019-11-26 00:29:19
问题 I\'m using raw_input in Python to interact with user in shell. c = raw_input(\'Press s or n to continue:\') if c.upper() == \'S\': print \'YES\' It works as intended, but the user has to press enter in the shell after pressing \'s\'. Is there a way to accomplish what I need from an user input without needing to press enter in the shell? I\'m using *nixes machines. 回答1: Under Windows, you need the msvcrt module, specifically, it seems from the way you describe your problem, the function msvcrt

How do I read multiple lines of raw input in Python?

醉酒当歌 提交于 2019-11-26 00:09:53
问题 I want to create a Python program which takes in multiple lines of user input. For example: This is a multilined input. It has multiple sentences. Each sentence is on a newline. How can I take in multiple lines of raw input? 回答1: sentinel = '' # ends when this string is seen for line in iter(raw_input, sentinel): pass # do things here To get every line as a string you can do: '\n'.join(iter(raw_input, sentinel)) Python 3: '\n'.join(iter(input, sentinel)) 回答2: Keep reading lines until the user

How can I sanitize user input with PHP?

纵然是瞬间 提交于 2019-11-25 23:55:31
问题 Is there a catchall function somewhere that works well for sanitizing user input for SQL injection and XSS attacks, while still allowing certain types of HTML tags? 回答1: It's a common misconception that user input can be filtered. PHP even has a (now deprecated) "feature", called magic-quotes, that builds on this idea. It's nonsense. Forget about filtering (or cleaning, or whatever people call it). What you should do, to avoid problems, is quite simple: whenever you embed a string within

How to show “Done” button on iPhone number pad

给你一囗甜甜゛ 提交于 2019-11-25 23:28:21
There is no "Done" button on the number pad. When a user finishes entering numeric information in a text field, how can I make the number pad disappear? I could get a "Done" button by using the default keyboard, but then users would have to switch to the numeric keys in order to input numbers. Is there a way to show a "Done" button on the number pad? Luda Another solution. Perfect if there are other non-number pad text fields on the screen. - (void)viewDidLoad { [super viewDidLoad]; UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)]; numberToolbar.barStyle =

Disadvantages of scanf

纵饮孤独 提交于 2019-11-25 23:02:25
问题 I want to know the disadvantages of scanf() . In many sites, I have read that using scanf might cause buffer overflows. What is the reason for this? Are there any other drawbacks with scanf ? 回答1: The problems with scanf are (at a minimum): using %s to get a string from the user, which leads to the possibility that the string may be longer than your buffer, causing overflow. the possibility of a failed scan leaving your file pointer in an indeterminate location. I very much prefer using fgets

How do you implement a good profanity filter?

不打扰是莪最后的温柔 提交于 2019-11-25 22:38:01
问题 Many of us need to deal with user input, search queries, and situations where the input text can potentially contain profanity or undesirable language. Oftentimes this needs to be filtered out. Where can one find a good list of swear words in various languages and dialects? Are there APIs available to sources that contain good lists? Or maybe an API that simply says \"yes this is clean\" or \"no this is dirty\" with some parameters? What are some good methods for catching folks trying to

Asking the user for input until they give a valid response

北城以北 提交于 2019-11-25 21:29:55
问题 I am writing a program that accepts an input from the user. #note: Python 2.7 users should use `raw_input`, the equivalent of 3.X\'s `input` age = int(input(\"Please enter your age: \")) if age >= 18: print(\"You are able to vote in the United States!\") else: print(\"You are not able to vote in the United States.\") The program works as expected as long as the the user enters meaningful data. C:\\Python\\Projects> canyouvote.py Please enter your age: 23 You are able to vote in the United