Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Program to Check if a Given Word Contains All Unique Characters, Exercises of Java Programming

This java program generates five random numbers and removes the corresponding letters from an arraylist. The user is then asked to input a word in caps, and the program checks if all the characters in the word are present in the arraylist. If so, it displays a message confirming that all characters are present. Otherwise, it displays a message indicating that some characters are missing.

Typology: Exercises

2016/2017

Uploaded on 05/13/2017

i3booze
i3booze 🇺🇸

1 document

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
package alphabet;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JOptionPane;
public class Main {
public static void main(String[] args)
{
Random generator = new Random();
ArrayList letters = new ArrayList();
for (char c='A'; c<'Z'; c++)
letters.add(c);
for(int i = 0; i < 5; i++)
{
int x = 25;
int r = generator.nextInt(x);
System.out.println("The random numbers are: "+ r);
x--;
letters.remove(r);
}
String word = JOptionPane.showInputDialog("Please Enter Word in CAPS: ");
String message = "i have all characters ";
System.out.println("The word is: "+ word);
for(int i = 0; i < word.length(); i++)
{
if (letters.contains(word.substring(i)))
{
JOptionPane.showMessageDialog(null, message);
break;
}
else
JOptionPane.showMessageDialog(null, message =
"i dont have all characters ");
break;
}
}
}

Partial preview of the text

Download Java Program to Check if a Given Word Contains All Unique Characters and more Exercises Java Programming in PDF only on Docsity!

package alphabet;

import java.util.ArrayList; import java.util.Random; import javax.swing.JOptionPane;

public class Main {

public static void main(String[] args) { Random generator = new Random();

ArrayList letters = new ArrayList(); for (char c='A'; c<'Z'; c++) letters.add(c);

for(int i = 0; i < 5; i++) { int x = 25; int r = generator.nextInt(x); System.out.println("The random numbers are: "+ r); x--; letters.remove(r); }

String word = JOptionPane.showInputDialog("Please Enter Word in CAPS: ");

String message = "i have all characters ";

System.out.println("The word is: "+ word);

for(int i = 0; i < word.length(); i++) { if (letters.contains(word.substring(i))) { JOptionPane.showMessageDialog(null, message); break; } else JOptionPane.showMessageDialog(null, message = "i dont have all characters "); break; } }