0
How do I compare a string?
I want to take input from the console in a string and compare each character https://code.sololearn.com/c4hUtQ9cRxPX/?ref=app
14 Answers
+ 2
If you want to equate
String s;
if(s.equal("Hello"))
Or to compare
Change equal to compare
+ 2
After the first test when you check the length, use "else if" in all other checks, instead of simple "if". That way the rest of the checks wont happen if the string is not the right size, you the program won't fail.
Alternatively you could also use try-catch clause to avoid the error.
+ 2
If this is a different issue then I suggest to put it in a new post. I certainly will try to help if I can, just give more details.
0
What do you mean by comparing a string? Checking how many times each letter occurs? Or compare the length of two strings? Or sort them alphabetically? Or check is one string includes another?
First step, define your problem more precisely please :)
0
May it work
https://code.sololearn.com/cHZrT0a7cUkr/?ref=app
0
Im trying to take in a string inputed in by the user.
the string consists of 6 characters.
the first character must be an I either uppercase or lower case.
the second character must always be a T upper or lower case
third, fourth, fifth and sixth characters must always be digits 0-9
How can i go about this using a string and character class?
0
Ok so you want to validate the string according to a certain pattern.
Think about what you want your program to do when the characters match or don't match. Print the result for each character? Or just check them and in the end show if the input was correct. What if the input is less or more than 6 characters.
0
i need it to only accept 6 characters
0
ort java.util.Scanner;
public class U4A1EnterCourseCode {
public static void main(String[] args)
{
System.out.println("Enter a course code");
Scanner input= new Scanner(System.in);
String s= input.nextLine();
if s.equals(
0
this is what i have so far
0
The requirements of this application are as follows: The application is to read a course code entered by the user from the keyboard. The course code is made of 5 characters and should follow these rules:
• First character is always an upper case I or a lower case i
• Second character is always an upper case T or a lower case t
• Third, fourth, fifth, and sixth characters are always digits (0-9)
The application then validates the course code against above the rules and prints a message if the course code is valid or not. If the course code is not valid, the application should print a message explaining why the course code is not valid.
Use these course codes to test your application:
IT4782 iT4782 OT4782 it&782
0
Ok so thats a good start, you could do something like this. It is not the complete solution but the idea is there.
https://code.sololearn.com/cUBF62X0MJYz/?ref=app
This type of validation can be done with regular expressions in more compact form, but that would be more advanced topic.
0
Please help. I’m getting a error message when the string is less than six characters:
import java.util.Scanner;
public class Program
{
public static void main(String[] args) {
//Takes users input
System.out.println("Enter a course code to validate");
Scanner input = new Scanner(System.in);
String text = input.nextLine();
//Boolean valid = true;
//Test the input is only 6 characters
if (text.length()<'5'){
System.out.println("Not Valid! Course code is too short. Course code must be at least six characters.");}
if(text.length() >5){
System.out.println("Not Valid! Course code is too long. Course code must be no more than six characters. ");}
//Test first character is I or i
if (text.toLowerCase().charAt(0) != 'i') {
System.out.print(text +" is Not Valid course code, first character must be l or i");
}
if (text.toLowerCase().charAt(1)!='t') {
System.out.println("Not valid course code.The second character must be T or t!");
}
else if (text.charAt(2)<'0' || text.charAt(2)>'9'){
System.out.println("Not Valid Course Code! third character must be a number!");
}
else if (text.charAt(3) < '0' || text.charAt(3)> '9') {
System.out.println("Invalid Course Code! The fourth character must be a number!");
}
else if (text.charAt(4) <'0' ||text.charAt(4) >'9') {
System.out.println("Invalid, fifth character must be a digit");}
else if (text.charAt(5) <'0' ||text.charAt(5) >'9') {
System.out.println("Not valid!, The sixth character must be a digit");}
else
System.out.println( text + " is a valid course code");
}
}
0
can you help me? im taking user input and echoing that input to the screen using at least three methods. can you assist me?