How To Randomly Select A String In Array Based On Corresponding Number
Dec 21, 2014
I'm trying to generate a random word from the array that I have made including the words by making it corresponding with a randomly generated number. So for example, if the number generated is 0, then the word that the person has to guess would be "AUNT". How would I transfer the randomly generated number from one method into the array method to get the word the person has to guess?
Write a program called Word Guessing Game. Open the file FourLetterWords.txt and write the contents into an array of Strings (the file has 87 words in it). Then use a randomly generated number between 0 and 86 to select a word. The player will then try to guess the word selected by the game. The player is allowed 7 tries, if the player does not guess the word on the 7th try he/she losses. Display the letter of the word as they are guessed in the correct order, you will also display the incorrect letters. The game is over when:
- The player completes the word, or guesses the whole word correctly.
- The player does not guess the word in seven tries.
The player must also be allowed to terminate the game.
The game must have at least 5 classes:
- Main Class
- Class to return a random integer between 0 and 86.
- Class to return a populated array of 87, 4 letter words.
- Class to return a character that the player enters from the keyboard.
- Class to display both the correctly guessed letters and the incorrect letters.
My code (it is not complete, my attempt to do what I am trying to do is obviously not working.)
import java.util.Scanner;
public class WordGuessingGame {
public static void main(String[] args) {
final int NUMBER_OF_WORDS = 87;
RandomWordGenerator.random(NUMBER_OF_WORDS);
In my account driver I am trying to get the user inputted account number to get the account by account number. In my code
System.out.println("Which Account number: "); int account = scan.nextInt(); ac.get(account-1);
This works if my accounts are numbered incrementally starting with one, I want it to match the inputted account number
System.out.println("Account number: "); int num = scan.nextInt();
I am thinking a for loop is probably needed. Here is my code:
public class AccountDriver { public static void main(String[] args) { Scanner scan = new Scanner(System.in); ArrayList<Account> ac = new ArrayList<>(); boolean more=true; boolean again=false;
I have a school assignment that involves me sorting an array of objects based on one of the class String variables. I am using as the title says a simple selection sort method. The problem I'm having is that when I run the program in debug mode, it never seems to enter the if statement in the inner loop. I would like to say I've tried a number of things to figure it out, but honestly I'm just stumped as to why it's not working.
Here is the code:
public static void sortTransactions(Transaction[] oTransaction){// This is the sorting method, obviously it's not done so it currently just prints to screen. System.out.println("Successful call to sortTransaction()"); String min = ""; int curInd = 0; Transaction[] temp = new Transaction[1];
[Code] ....
The output when I check to see if the array is sorted verifies that the array never does get sorted.
1. Write a Java program that randomly generates a five-digit lottery number and prompts the user to enter a five-digit number. Each digit in the number is in the range between 0~9. The program should determine which prize the user wins according to the following rule:
- The user wins the first prize if the user input matches all five digits in the lottery number in exact order. -The user wins the second prize if the user input matches any four digits in the lottery number in exact positions. -The user wins the third prize if the user input matches any three digits in the lottery number in its exact position. -The user wins the fourth prize if the user input matches any two digits in the lottery number in its exact position.
- The user wins the fifth prize if the user input matches any one digit in the lottery number in its exact position.here is my code. I have tried replacing the && statements with || and it always returns either case 1 or case default.
import java.util.Scanner; import java.util.Random; class Hw5 { static int getPrize(int g1, int g2, int g3, int g4, int g5, int u1, int u2, int u3, int u4, int u5) {
I am getting a strange error and it almost seems like its not comparing it to the random generated number just the guess that i entered before. Here are my error messages.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at guess.the.numbers.GuessTheNumbers$ButtonHandler.actionPerformed(GuessTheNumbers.java:119) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
I need to make a string filled with naughts and crosses like this one : "xxx ooo xox". There are 3 groups separated with a space. how to fill the string randomly ?
I have this program where I'm supposed to fill an array with 1000 indices with 1000 randomly generated numbers between 1 and 1000. The program is supposed to check if any of the numbers match an index that is the same value (so for example, the number 4 is in index 4). How to check for that condition, especially using a binary search (I'm also told to use a binary search).
Right now the variable index isn't initialized because I don't know what to initialize it to exactly. How do I check to see if any numbers match the value of the same index?
import java.util.*; public class Identity { public static void main(String[] args) { int [] integers = new int [1000]; // Fill array with randomly generated numbers int [] display = GenerateRandom(integers);
What I need to do is ask the user to input some text and then turn it into pig latin. I am confused on how to select each word from the string to determine if it ends in a way or ay. I am to assume that the letters are all lowercase and the text ends with a period.
import java.util.Scanner; public class Trial { public static void main(String [ ] args) { Scanner input = new Scanner(System.in);
Write a Java program that randomly generates a five-digit lottery number and prompts the user to enter a five-digit number. Each digit in the number is in the range between 0~9. The program should determine which prize the user wins according to the following rule:
• The user wins the first prize if the user input matches all five digits in the lottery number in exact order. • The user wins the second prize if the user input matches any four digits in the lottery number in exact positions. • The user wins the third prize if the user input matches any three digits in the lottery number in its exact position. • The user wins the fourth prize if the user input matches any two digits in the lottery number in its exact position. • The user wins the fifth prize if the user input matches any one digit in the lottery number in its exact position.
and here is my code. I cant get it to print the right statements.
import java.util.Scanner; import java.util.Random; class Hw5 { static int getPrize(int g1, int g2, int g3, int g4, int g5, int u1, int u2, int u3, int u4, int u5) { //code for determining the prize comparing (g1, g2, g3, g4, g5) to (u1, u2, u3, u4, u5) if (u1 == g1 && u2 == g2 && u3 == g3 && u4 == g4 && u5 == g5)
I am trying to count the number of occurrences of a string in an array list. I used the following code:
int count = Collections.frequency(strings, search);
strings is the name of the array list and search is the string that I am trying to count the number of occurrences of. My program compiles correctly, but when I enter the string to search for, I get the following error: "Exception in thread "main" java.lang.NullPointerException at java.util.Collections.frquency(Collections.java:37 18)
Why am I getting this error message and how do I fix it?
I'm having a hard time with this problem, this is what I have, but I can not use two integers, I have to use one integer and a string...
This is the question:
Write a method called printStrings that accepts a String and a number of repetitions as parameters and prints that String the given number of times. For example, the call:
printStrings("abc", 5);
will print the following output: abcabcabcabcabc
This is what I attempted:
public class printStringsproject { public static void printStrings(int abc, int number) { for (int i = 1; i <= number; i++) { System.out.print("abc"); } } public static void main(String[] args) { printStrings(1, 5); } }
I must fill a 2d array randomly and then apply methods to the array. However i keep getting an out of bounds exception no matter what dimensions i use. I have a test and a class program.
import java.util.*; import java.lang.Math; import java.util.Arrays; import java.util.Random; class SummerStats { Random rand = new Random();
i am trying to print out a randomly generated array, but i only get
[I@7852e922
i did some research and the "[" is for arrays, "I" is for the int and "@" is the hash. and the rest is some sort of hex. I need to override this in a way, but i can't seem to find out how. this is my current code:
import java.util.Random;
public class Oppgave { public static void main(String[] args){ int myint[] = fyll(); System.out.println(myint); } public static int[] fyll() {
I'm trying to build a monopoly like game, and atm I'm trying to find way how to build the Community and Chance chest cards. so far, my logic is
1-create an ArrayList of cards with a given order
2-for a given number of times(for loop) generate 2 random numbers ,which will be the parameters for Collection.swap().
3-swap.
here's the code for the shuffler Button
shuffler.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { for(int i=0;i<shuffeledMessages.length;i++){ int randoma=(int)(Math.random()*4); int randomb=(int)(Math.random()*4); Collections.swap(myMessages,randoma,randomb); } } });
For now things seem to work pretty ok, but I'm wondering if this is a good and efficient way to shuffle a card chest especially in case of large number of cards. plus, I'm not sure what would be a good loop count for effective shuffling, in my case I used i<arraylist.size
I am trying to split a string based on length(example length 5) of the string. But I am having a issues with this substring(start, end) method. I get all substring which are of length 5. But if the last substring is less than 5 then I am not getting that last substring. But I need the last substring even if it is less than 5.
Started learning about Array's I'm doing an exercise where you create a for loop that randomly assigns values to each element within the array, but where is my code going wrong?
import java.util.Scanner; public class ArrayExamples{ public static void main(String[] args) { Scanner input = new Scanner(System.in); double exampleArray[] = new double[5]; System.out.print("Enter a Number: "); int num1 = input.nextInt();
I have an assignment for college that involves placing randomly chosen words from a text file into a 2-d array. I have nearly completed ithowever I am having difficulty with a list of string type.What I have done so far is below,
when I try to run an instance of the program(using BlueJ) I get an error saying there is a null pointer exception at line 35 which is wordsForGrid. add (puzzleWords.get(pos));
I am thinking that this is related to the fact that at this line in the loadWordsFromFile method List <String>words=new ArrayList<String>(); I could not create an empty list of string type, i.e List<String>words = new List<String>();
Is this correct? Or am I looking in the wrong place. The textfile I am using contains over 6000 words on a separate line for each if that makes any difference.
I am trying to have a user select from a printed out array list, instead of having the user type in the "bill type" each time there is a bill to avoid user error as much as possible. For example I would like to have it print out like this:
"Select bill type from list:
1. Rent 2. Car 3. etc..."
and I would like the user to choose a number and not type in the "bill type". I don't want to use "Switch case" because it would need to be an expanding and I don't think "switch case" can do that.
Here is the code:
package homebudget; class Spending { //Do i need a totalAmount variable? String type; double amount; int year, month, day; public Spending()
[Code] ....
case 2: //Give option to enter a new expense or pick from list. //How to do this? If Statement that doesn't list duplicates, or a while search?
resp = JOptionPane.showInputDialog("Enter the type of expense:"); type = resp; resp = JOptionPane.showInputDialog("Enter the amount of the expense:"); amount = Double.parseDouble(resp);
So I have this stack. I'm writing out all the operations and what not but I'm having trouble bypassing this "generic array creation" problem. I'm meant to be creating an array based implementation of a stack and from my research from google and my various attempts at things, I have not found a solution that works.
In addition; I have all the operations written that I need except for one final one. And that is clear(). clear() is meant to empty the array, essentially it is a popAll() method. Then all I need to do is set up so I can print out the arrays and I should be able to handle everything else.
StackInterface:
/** An interface for the ADT stack. */ public interface StackInterface<T> { /** Adds a new entry to the top of this stack. @param newEntry an object to be added to the stack */ public void push(T newEntry);
/** Removes and returns this stackÕs top entry. @return either the object at the top of the stack or, if the stack is empty before the operation, null */ public T pop();
I have to validate security number based on tHIS REQUEST
If the SECURITYNNUBER IS INVALID UNDER THE SECURITYNUMBER attribute,it must reject(Date & algorith). To override the normal validation,the subject must contain INVALIDSECURITYNUMBER. The incorrect ID number must still reflect in the SECURITYNUMBER attribute.
THE FIRST 6 DIGIT IS DATE
EXAMPLE 7701205334086
public boolean val_SECURITYNUMBER() { boolean flag = true; String u_Securtynr = transactionFile.getS_securtynumber(); // 1. Invalid securty number if completed if(!isBlank(u_Securtynr))