Java: Accepts a string from the user and check whether the string is correct or not
Validate String for XYZ Rules
Write a Java program that accepts a string from the user and checks whether it is correct or not.
The conditions for getting the "correct answer" are:
a) There must be only three characters X, Y, and Z in the string, and no other characters.
b) Any string of any form such as aXYZa can get the "correct answer", where a is either an empty string or a string consisting only of the letter X;
c) If aXbZc is true, aXbYZca is also valid, where a, b, c are either empty strings or a string consisting only of the letter X.
Visual Presentation:
Sample Solution:
Java Code:
// Importing necessary classes
import java.util.PriorityQueue;
import java.util.Scanner;
// Defining a class named "Main"
public class Main {
// Static nested class "Dic" representing a pair of word and page number
static class Dic implements Comparable{
// Instance variables to store word and page number
String moji;
int page;
// Parameterized constructor to initialize word and page number
Dic(String moji, int page){
this.moji=moji;
this.page=page;
}
// Implementation of the compareTo method to define the natural order of Dic objects
public int compareTo(Dic d) {
if(this.moji.equals(d.moji)) {
return this.page-d.page;
}
else {
return this.moji.compareTo(d.moji);
}
}
}
// Main method, the entry point of the program
public static void main(String[] args) {
// Using the try-with-resources statement to automatically close the Scanner
try(Scanner sc = new Scanner(System.in)){
// Creating a PriorityQueue to store Dic objects
PriorityQueue pq=new PriorityQueue<>();
// Prompting the user to input pairs of a word and a page number
System.out.println("Input pairs of a word and a page number:");
// Reading input until there is no more input
while(sc.hasNextLine()) {
// Reading a line and splitting it into word and page number
String str=sc.nextLine();
String[] token=str.split(" ");
String s=token[0];
int n=Integer.parseInt(token[1]);
// Creating a new Dic object and adding it to the PriorityQueue
pq.add(new Dic(s, n));
}
// Initializing a variable to store the previous word
String pre="";
// Printing the word and page number in alphabetical order
System.out.println("\nWord and page number in alphabetical order:");
while(!pq.isEmpty()) {
// Polling the smallest Dic object from the PriorityQueue
Dic dic=pq.poll();
// Checking if the current word is the same as the previous one
if(dic.moji.equals(pre)) {
System.out.print(" "+dic.page);
}
else if(pre.equals("")) {
System.out.println(dic.moji);
System.out.print(dic.page);
}
else {
System.out.println();
System.out.println(dic.moji);
System.out.print(dic.page);
}
// Updating the previous word
pre=dic.moji;
}
System.out.println();
}
}
}
Sample Output:
Input a string: XYZ Correct format..
Flowchart:
For more Practice: Solve these Related Problems:
- Write a Java program to validate a string that contains only the characters X, Y, and Z following a strict pattern.
- Write a Java program to check if a string follows the pattern where any number of X's may be followed by Y's and ending with Z's.
- Write a Java program to validate strings composed solely of X, Y, and Z, ensuring at least one occurrence of each letter.
- Write a Java program to determine if a string with only X, Y, and Z is valid when its order is reversed.
Go to:
PREV : Pair Words with Page Numbers.
NEXT : Highest and Lowest Scores from Student Data.
Java Code Editor:
Contribute your code and comments through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.