Question

This is another attempt to ask this question: I need a Java program that asks the...

This is another attempt to ask this question:

I need a Java program that asks the user for a String. the string must be atleast 7 character and only words and/or numbers, the program should read this input and output “Valid”. No special characters or whitespaces are allowed, if so output “Invalid”

*** Please note: The program must be able to know if the string is less than 7 characters and output INVALID. Also the program must be able to detect WHITESPACES and output INVALID. Please test beforehand.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class ValidString {

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner sc = new Scanner(System.in);

String str = sc.nextLine();

boolean invalid = false;

int count = 0;

for(int i=0 ; i<str.length() ; ++i) {

if(!Character.isDigit(str.charAt(i)) && !Character.isLetter(str.charAt(i))) {

invalid = true;

break;

}else {

++count;

}

}

if(!invalid) {

if(count >=7)

System.out.println("VALID");

else

System.out.println("INVALID");

}else {

System.out.println("INVALID");

}

}

}
=========================================================
See OUTPUT
3 import java.util.Scanner 4 5 public class ValidString f 7 public static void main(String[] args) I //TODO Auto-generated me

Thanks, PLEASE COMMENT if there is any concern.

Add a comment
Know the answer?
Add Answer to:
This is another attempt to ask this question: I need a Java program that asks the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Question, I need a program that asks a user to input a string && then...

    Java Question, I need a program that asks a user to input a string && then asks the user to type in an index value(integer). You will use the charAt( ) method from the string class to find and output the character referenced by that index. Allow the user to repeat these actions by placing this in a loop until the user gives you an empty string. Now realize that If we call the charAt method with a bad value...

  • Your assignment is to write an assembly language program which read a string and print it in uppercase. This program asks the user to enter a string which is subsequently displayed in uppercase. It is...

    Your assignment is to write an assembly language program which read a string and print it in uppercase. This program asks the user to enter a string which is subsequently displayed in uppercase. It is important to first ensure that string to be converted is in the a-z range. The program does not recognize any space, symbols or any kind of punctuation marks. Any time the user enters any of this character the program is going to repeatedly ask for...

  • Write Java program: • Is at least 8 characters long • Contains at least 1 lower letter charact...

    Write Java program: • Is at least 8 characters long • Contains at least 1 lower letter character • Contains at least 1 upper letter character • Contains at least 1 numeric digit • Contains at least 1 special character from the set: !@#$%^&* • Does not contain the word “and” or the word “the” Prompts the user for a password, including the requirements above in your output. Output a string that states valid or invalid. If invalid, state which...

  • Write a program in java to read a string object consisting 300 characters or more using...

    Write a program in java to read a string object consisting 300 characters or more using index input Stream reader. The program should perform following operations. The String must have proper words and all kind of characters.(Use String class methods). a, Determine the length of the string count the number of letters in the strings , count the number of numeric object d. Calculate the number of special character e. Compute the ratio of the numeric to the total f....

  • Your assignment is to write an assembly language program which read a string and print it in...

    Your assignment is to write an assembly language program which read a string and print it in uppercase. This program asks the user to enter a string which is subsequently displayed in uppercase. It is important to first ensure that string to be converted is in the a-z range. The program does not recognize any space, symbols or any kind of punctuation marks. Any time the user enters any of this character the program is going to repeatedly ask for the...

  • i need know how Write a program that tests whether a string is a valid password....

    i need know how Write a program that tests whether a string is a valid password. The password rules are: It must have at least eight characters. It must contain letters AND numbers It must contain at least two digits It must contain an underscore (_) Write a program that prompts the user to enter a password and displays the password entered and whether it is valid or invalid.

  • We are trying to make sure your passphrase is acceptable! Write a script that asks for...

    We are trying to make sure your passphrase is acceptable! Write a script that asks for an input string that will be used as your password. This input string will have three “words” separated by a space character. Each of these words has specific requirements, listed below. You must write a function for each requirement (three total functions). Make sure your functions follow the original template that provided. First Function: For the first word, you must check if it is...

  • Design and implement a Java program (name it PasswordTest) that accepts a string from the user...

    Design and implement a Java program (name it PasswordTest) that accepts a string from the user and evaluates it as a password, giving it a valid or invalid verdict A good password will be at least 8 characters and includes at least one lower-case letter, at least one upper-case letter, at least one digit, and at least one character that is neither a letter nor a digit. Your program will need to check each character in the string in order...

  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

  • Write a program in java that asks a user for a file name and prints the...

    Write a program in java that asks a user for a file name and prints the number of characters, words (separated by whitespace), and lines in that file. Then, it replaces each line of the file with its reverse. For example, if the file contains the following lines (Test1.txt): This is a test Hi there Output on the console: Number of characters: 22 Number of words: 6 Number of lines: 2 Data written to the file (Test1.txt): tset a si...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT