Question

In: Computer Science

Write a Java application with a JavaFXGUI that takes a String input by the user and...

Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once.

The GUI needs, at minimum, a label to say "Enter String: ", a TextBox to take input for the String, a label to show the answer ("String contains all 26 letters" or "String does not contain all 26 letters", and a button to trigger the method to determine the outcome.

Hints:

  • Use toLowerCase() or toUpperCase() on the characters from the String so that you do not have to worry about case.
  • *Absolutely don't* write 26 different statements to test all 26 letters. You can use a loop and cast (int) the loop counter to a char; upper case A is Unicode character 65.
  • String has a contains() method much like the one in List.

Solutions

Expert Solution

/************************

GuiApp.java

********************/

package gui.fx;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class GuiApp extends Application {
  
public static void main(String[] args) {
launch(args);
}
  
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("String Check");
  
Label lbl1 = new Label("Enter String:"); //label
  
TextField txt = new TextField(); //Text field where string will be input
txt.setPrefSize(400, 40);
txt.setMaxWidth(400); //max width is set to 400
  
Label lbl2 = new Label(""); //label for showing final outcome
  
Button btn = new Button(); //button : on click of this final outcome will be shown
btn.setText("Get Result");
btn.setOnAction(new EventHandler<ActionEvent>() { //this will be triggered when button is clicked
@Override
public void handle(ActionEvent event) {
String textToCheck = txt.getText(); //get the text from textfield
String result = checkResult(textToCheck); //get the result by calling checkResult() method
lbl2.setText(result);//set the outcome in lbl2
}
});
  
  
//add all the above in a VBox
VBox root = new VBox(20); //VBox will put all the children with a vertical gap of 20
root.getChildren().add(lbl1);//add lbl1
root.getChildren().add(txt);//add text field
root.getChildren().add(btn);//add the button
root.getChildren().add(lbl2);//add lbl2 to show the outcome
  
primaryStage.setScene(new Scene(root, 500, 300));
primaryStage.show();
}
  
/**
* The method checks whether the given text contains all the 26 letters or not
* @param text
* @return
*/
private String checkResult(String text){
   String result = null;
   String tempText = text.toLowerCase(); //converts text into all lower case
   boolean isLetterPresent = true;
   for(char currentLetter = 'a' ; currentLetter <='z'; currentLetter++){ //loop through each letter from a to z
       //System.out.println(String.valueOf(currentLetter));
       if(!tempText.contains(String.valueOf(currentLetter))){ //if tempText doesnot contain the current character
           isLetterPresent = false;//set the letter is not present
           break; //it will brak out of the loop
       }
   }
  
   //set result based on whether ll letter is found or not
   if(isLetterPresent){
       result = "String contains all 26 letters";
   }else{
       result = "String does not contain all 26 letters";
   }
  
   return result;
}
}

======================================

OUTPUT

======================================


Related Solutions

Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
Java Code: Write an application that takes in user input. (Name, Age, and Salary) ------ Write...
Java Code: Write an application that takes in user input. (Name, Age, and Salary) ------ Write an application that includes a constructor, user input, and operators.
Write a program that takes a string input from the user and then outputs the first...
Write a program that takes a string input from the user and then outputs the first character, then the first two, then the first three, etc until it prints the entire word. After going up to the full word, go back down to a single letter. LastNameUpDown. Input: Kean Output: K Ke Kea Kean Kea Ke K
Write a Java program that prompts the user to input a string and prints whether it...
Write a Java program that prompts the user to input a string and prints whether it is a palindrome. A palindrome is a string which reads the same backward as forward, such as Madam (disregarding punctuation and the distinction between uppercase and lowercase letters). The program must use the stack data structure. The program must include the following classes: The StackX class (or you can use the Java Stack class). The Palindrome class which must contain a method named palindrome()...
Write a Java method that takes an array of char and a String as input parameters...
Write a Java method that takes an array of char and a String as input parameters and and returns an boolean. The method returns true if we can find the input string inside the array by starting at any position of the array and reading either forwards or backwards.
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
Write a recursive method using Java that takes a string s as input and returns a...
Write a recursive method using Java that takes a string s as input and returns a list that contains all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For instance, the word ‘cat’ is an anagram of ‘act’. Notice that the output list cannot contain duplicates.
Write a Java method that takes an input string and computes the income minus the expenses....
Write a Java method that takes an input string and computes the income minus the expenses. The income components are indicated by numbers; while the expenses from your spending are numbers starting with a minus sign '-'. The input string may contain lowercase and uppercase letters, as well as other characters. Note that Character.isDigit(char) tests if a char is one of the chars '0', '1', ..., '9'. Also recall that Integer.parseInt(string) converts a string to an int. Test cases :...
Using Python, write a simple application that takes user input of plaintext and key, and encrypt...
Using Python, write a simple application that takes user input of plaintext and key, and encrypt the plaintext with Vigenere Cipher. The application should then print out the plaintext and the ciphertext.
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns true if the String is a palindrome.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT