logo
down
shadow

How can I check for invalid input and loop until the input is valid?


How can I check for invalid input and loop until the input is valid?

By : nightcandle
Date : November 22 2020, 10:38 AM
seems to work fine Let's come up with an sample for you so you can follow as your blueprint
first, I chose do while loop because you need to ask this question at least once.
code :
do
{
   //Statements
}while(Boolean_expression);
    int i = 0;
    Scanner input = new Scanner(System.in);
    while (i == 0) {
        System.out.println("Enter number zero plz");
        int result = input.nextInt();
        if(result == 0 ){
            System.out.println("I entered right number");
            i = 1;
        } else
            System.out.println("you entered the wrong number \nplz try again");
    }


Share : facebook icon twitter icon
While loop to check for valid user input?

While loop to check for valid user input?


By : SEQUEL
Date : March 29 2020, 07:55 AM
help you fix your problem Python newbie here so sorry for what I'm sure is a stupid question, but I can't seem to solve the following challenge in a tutorial that is asking me to use a while loop to check for valid user input. , A shorter solution
code :
while raw_input("Enjoying the course? (y/n) ") not in ('y', 'n'):
    print("Sorry, I didn't catch that. Enter again:")
choice = raw_input("Enjoying the course? (y/n) ")
print("choice = " + choice)
student_surveyPromptOn = True
while student_surveyPromptOn:
    input = raw_input("Enjoying the course? (y/n) ")
    print("input = " + input)
    if choice != input:
        print("Sorry, I didn't catch that. Enter again:")
    else:
        student_surveyPromptOn = False
Enjoying the course? (y/n) y
choice = y
Enjoying the course? (y/n) n
choice = y
input = n
Sorry, I didn't catch that. Enter again:
Enjoying the course? (y/n) x
choice = y
input = x
Sorry, I didn't catch that. Enter again:
Enjoying the course? (y/n) 
student_surveyPromptOn = True
while student_surveyPromptOn:
    choice = raw_input("Enjoying the course? (y/n) ")
    if choice != 'y' and choice != 'n':
        print("Sorry, I didn't catch that. Enter again:")
    else:
        student_surveyPromptOn = False
get char input, check if it is valid or not, if it is invalid clear whole input

get char input, check if it is valid or not, if it is invalid clear whole input


By : Katerina
Date : March 29 2020, 07:55 AM
help you fix your problem Your program receives input line by line. You can just read the entire line with std::getline or std::istream::getline and inspect the first character.
Check if given input is a valid IP or Hostname or something invalid

Check if given input is a valid IP or Hostname or something invalid


By : Jacek
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I am trying to create a program to check whether the given input is a valid hostname, valid IP, or neither. The program is able to recognize a valid hostname or IP. However, If I try to put something random, like "xyz", it doesn't print the error message I want. , You can add a simple try block and catch the error(s) generated.
code :
import socket

def main():
    hostname = 'hyuiolpp'
    try:
        if socket.gethostbyname(hostname) == hostname:
            print('{} is a valid IP address'.format(hostname))
        elif socket.gethostbyname(hostname) != hostname:
            print('{} is a valid hostname'.format(hostname))
    except socket.gaierror:
        print("Something is wrong")

if __name__ == '__main__':
   main()
How can I check user input is valid and set an alert base on the input type?

How can I check user input is valid and set an alert base on the input type?


By : Pazzo Xx
Date : March 29 2020, 07:55 AM
should help you out I belive below code will help you to resolve your problem
app.component.html
code :
<input #inp type="text" (keyup.enter) ="keyup(inp)" />
import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  keyup(inp: HTMLInputElement) {
    let patt1 = /[0-9]/g;
    inp.value.match(patt1) !== null ? console.log(inp.value) : alert('this is NAN');
  }
}
Firefox error: Unable to check input because the pattern is not a valid regexp: invalid identity escape in regular expre

Firefox error: Unable to check input because the pattern is not a valid regexp: invalid identity escape in regular expre


By : jeevan
Date : March 29 2020, 07:55 AM
seems to work fine This is due to the following change: Bug 1227906 - HTML pattern attribute should set u flag for regular expressions
As someone has already said, you don't have to escape those characters. Just use:
Related Posts Related Posts :
  • How to write generic Java API without method Overloading
  • Java Serialization / Deserialization of an ArrayList works only on first program execution
  • Conway's Game of Life's rules dont apply properly
  • call a java method when Click on a html button without using javascript
  • Why does SSLSocketFactory.createSocket returns a Socket instead of SSLSocket?
  • How to schedule a task in Tomcat
  • compare two pdf files (approach) using java
  • What is the use of allowCoreThreadTimeout( ) in ThreadPoolExecutor?
  • Describe the Strategy Design Pattern in Java?
  • MongoDB too many open connections
  • Issue with generate JAXB classes with value constructors
  • Moving from one frame to another in java swing
  • Get a thread by Id
  • how to open front camera and back camera at same time?
  • Messed up with Java Declaration
  • Extracting chart in EXcel as image using java
  • How to get latitude and longitude in Android phone?
  • Do GC pauses and kill -STOP produce the same behavior?
  • Android HTML.ImageGetter Images resized
  • The way to instantiate map<String, List<String>> in Java
  • Java Making objects move while buttons held
  • Androidx Navigation View - `setNavigationItemSelectedListener` Doesn't Work
  • after compiling code JFrame does not appear
  • Netbeans project, JFrames ran from the main JFrame close all JFrames in project
  • Square Retrofit Client: How to enable/disable followRedirects? How to intercept redirect URL?
  • Java Circular Reference - Can’t Compile
  • Mouse click on specific time JAVA
  • android 4 onCreateDialog return type incompatible when overriding
  • Java Implementation of Oracle's DBMS_UTILITY.GET_HASH_VALUE Function
  • javac cannot be run, and furthermore does not seem installed
  • Function to obtain a thread given its ID
  • Java File path to Executable has spaces
  • Minimum Oracle Java VM heap memory setting?
  • Elegant way to create one of a large number of classes
  • Java fx binding styles
  • Summing weights based on string in large file
  • Android ListView setOnItemClickListener not working
  • How is the Java Bootstrap Classloader loaded?
  • Check object is blank
  • Add image between text in JEditorPane
  • Java POI Excel creating new column and new rows
  • NullPointerException when managing bean with Spring+JSF(Primefaces)
  • Content autocomplete in spring tool suite 4
  • Super Noob Help ~ MouseEvent/Frame
  • Making one project with dependencies stand-alone in Eclipse (Java)
  • Jsoup- getting certain attributes from website
  • MongoDB, how to make a link between collections with JAVA code
  • Overriding an abstract method means class abstract class or not in java?
  • Bukkit teleport - nullPointerException
  • How to check if a Float variable is negative in java
  • Servlet: Cannot forward after response has been committed
  • sql query to select a specific cell in excel
  • How to make new picture when mouse dragged in java
  • Use Constants interface in GWT Project
  • Setting size of a JPanel in GroupLayout
  • Construct a map from two equal size arrays
  • Implementing Read-Write Locks with Double-Checked Locking
  • Disable NO_BUTTON from JOptionPane Java if a condition is true
  • UDP multiple socket
  • Android Threading: This Handler class should be static or leaks might occur
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org