How To Access 4 Strings Randomly From String Array
Jun 13, 2014Now I am trying to print the 4 string randomly from string array..where string contains no of words which are splitted from the file....
View RepliesNow I am trying to print the 4 string randomly from string array..where string contains no of words which are splitted from the file....
View RepliesI'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);
[code]...
I am trying to update a Rubik's cubes side by adding each of the 9 squares of the Rubik's cube's side.
So for example:
cube.front.squares = cube.fronts.squares[1] + cube.front.square[2] + cube.left.sqaures[8] ... //until 9 sqaures are added, where squares is a String array of size 9. However, when I try doing this I get an error stating "change 'square' to 'String'". I know I can go in an individually change the values like:
cube.front.squares[0] = "whatever"
cube.front.squares[1] = "something else"
but I know there is a better way to do this...
I also just tried to use String[] concat= ArrayUtils.addAll(first, second) but that only takes in two arrays at most according to the API: [URL]...
String str = "#11* 1# 2*12# 3"
required o/p in array as
#11
* 1
# 2
*12
# 3
i.e splitting the strings for every 3rd string and storing it in an array of strings ...
how to create dynamic string array if we dont know number of strings in the beginning?
View Replies View RelatedI am having a problem with the following code. It compiles and runs fine however my output is wrong.
public class SplitString {
public static void main(String[] args) {
String[] string1 = split("ab#12#453", "#");
String[] string2 = split("a?b?gf#e", "[?#]");
for (int i = 0; i < string1.length; i++) {
System.out.print(string1[i] + ",");
[code]....
The split method in the String class returns an array of strings consisting of the substrings split by the delimiters. However, the delimiters are not returned. Implement the following new method that returns an array of strings consisting of the substrings split by the matching delimiters, including the matching delimiters.public static String[] split(String s, String regex)For example, split("ab#12#453", "#") returns ab, #, 12, #, 453 in an array of String, and split("a?b?gf#e", "[?#]") returns a, b, ?, b, gf, #, and e in an array of String.
I am having an array of strings and i want to find out whether these strings contained in the array contain a similar character or not.For example i am having following strings in the array of string:
aadafbd
dsfgdfbvc
sdfgyub
fhjgbjhjd
my program should provide following result: 3 because i have 3 characters which are similar in all the strings of the array(f,b,d).
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 ?
View Replies View RelatedI created and an Array of integers how can I get randomly get/pick the index of each array element.
View Replies View RelatedI 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();
[Code] .....
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() {
[Code]...
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
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();
[Code] .....
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,
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.io.*;
import java.lang.Math;
[code]....
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'm stuck with the following error message when i try to convert a String to XML and back to a String.
Exception in thread "main" org.w3c.dom.DOMException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it.
at com.sun.org.apache.xerces.internal.dom.ParentNode.internalInsertBefore(Unknown Source)
at com.sun.org.apache.xerces.internal.dom.ParentNode.insertBefore(Unknown Source)
at com.sun.org.apache.xerces.internal.dom.NodeImpl.appendChild(Unknown Source)
at com.innovatrail.consulting.xml.XMLTEST2.main(XMLTEST2.java:66)
Below is my Java code snippet: The code syntactically correct except line 66. If you comment out line 66 in the source code, everything works just fine. The XML representation of my input string is denoted by INPUT 1 and that of my output string is denoted by OUTPUT 2.
package com.Test.xml;
import java.io.ByteArrayInputStream;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
[Code] ....
INPUT 1:
<Collection>
<BookList>
<Book>
<Title>Wonder Man
</Title>
<Author>John Smith
</Author>
<Category>Fiction
[Code] .....
OUTPUT 2:
<Collection>
<Book>
<Title>Wonder Man</Title>
<Author>John Smith</Author>
<Category>Fiction</Category>
[Code] ....
We were suppose to make a program for an assignment and the prof provided some codes to start of. What does this basically mean? How do i access the string arrays from consolelist?
class ConsoleInfo {
private String conTitle;
private double conPrice;
private int conQty;
private String conPic;
private static String empPassword;
ConsoleInfo(String title, double price, int qty,String pic)
[Code]...
Why do we use public access modifiers with String toString() method in java while overriding???
View Replies View RelatedI want to clarify it whether this below code, when running this loop, will it create separate string objects as strings are immutable or else, will it keep the same reference(as each time inside loop, we use the same name 'rslt') and assign new string value for this?
while(inc < numberOfColumns){
String rslt=rs.getString(inc+1);
rowArr[inc] = rslt;
inc++;
}
I have this source code that is supposed to count the repeated occurrences of certain strings in the text file, "string.txt". The contents of the file are listed below:
AA-AB-AC-AG-AE-AL
AL-AQ-AE-AN-AO-AM
AM-AQ-AO-AA-AB-AC
AD-AJ-AK-AI-AM-AO
AN-AH-AC-AA-AP-AQ
AP-AP-AN-AN-AL-AP
AN-AD-AU-AE-AH-AQ
AK-AQ-AE-AL-AE-AA
AA-AJ-AB-AG-AE-AF
[code]....
Question: 1. Declare and implement a class named Substrings. The class will contain the following data members and methods:
Data members:
string S
Methods:
setS() take a string as a parameter and store it in S
printSub1(), printSub2(), printSub3(), printSub4() print all substrings of S. If S = "abcd",
printSub1() will print "a", "ab", "abc", "abcd", "b", "bc", "bcd", "c", "cd", "d",
printSub2() will print "abcd", "bcd", "cd", "d", "abc", "bc", "c", "ab", "b", "a",
printSub3() will print "a", "b", "c", "d", "ab", "bc", "cd", "abc", "bcd", "abcd", and
printSub4() will print "abcd", "abc", "bcd", "ab", "bc", "cd", "a", "b", "c", "d".
Alright so after doing some googling and watching tutorials I managed to put together the first two but I still don't exactly understand how nested for loops work, however just a single loop I do understand.
Secondly, the 3rd and 4th prints are completely destroying my brain using for loops, which we are supposed to use. Check out my code below, also keep in mind the 3rd is giving errors and the 4th i had to revert back to the same as the first print temporarily because my code because so messed up. How to understand nested for loops better.
[code=Java]
import java.util.Scanner;
class subString {
String S;
void setSubstring(String SS) {
S = SS;
[Code] .....
Compiling: error for myS.printSub3 and myS.printSub4 is the same as 1 because i had to revert it after ruining the code.
I'm attempting a small program as I'm teaching myself the ropes. In it, I need to compare one string (the base) to another which is just the base string that's had it's characters shuffled.
Java Code:
String base = "ABCDEFG"
String shuffled = "CDAFBEG" mh_sh_highlight_all('java');
What I need to do is run a loop that shuffles the base string each time, but compares and saves any characters that match in the correct location. For example, if shuffled = "CDAFBEG", then the G would be "locked" in place and the rest of the characters shuffled and looped back to the comparison.
I have all the code I need for shuffling the string, but I'm not sure how I would go about comparing each character and then also locking it in place. I get the basics, I think, of needed to use several variables.
I need my code to print out the top three most common IP Addresses in the ArrayList. Right now I have it printing out the top IP Address. I am a little confused as to how to get it to print out three. Every time I change something, It doesn't give me the correct results
My Code:
public class Log_File_Analysis {
private static ArrayList<String> ipAddress = new ArrayList<>();
private static String temp , mostCommon;
static int max = 0;
static int num = 0;
[Code] .....
I'm trying to calculate the average of grades that a user puts in. Here is my code so far:
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.println("Please enter an array of grades seperated bt a comma.");
input.nextLine();
String arrayOfGrades = "100,50,100";
String[] grades = arrayOfGrades.split(",");
[Code] .....
I think I'm on the right track, the only big error I'm really getting is the line: sum += grades[i]. It's saying string can not be converted into a double.
An array has objects with String firstName, String lastName, int ID, char examType('M' or 'F'), and int score. Every object has both Midterm('M') and Final('F'). The array has to be sorted by the first Midterm- 'M' and immediately followed by the Final ('F') of the same person (object). Im having troubles with coming up with a proper algorithm with gettin the Final- 'F' after Midterm for the same person.
Java Code: public static Exam[] collateExams(Exam[] exams) {
Exam [] r = new Exam[10];
r = exams;
int[] position = new int[10];
int index = 0;
for(int i = 0; i < exams.length; i++)
[Code]...
I want to keep count of how many students are in my array. the array i made up of objects from other classes. like the class Student how do i do this. i have tried contains but better way it to to a loop to go through the array and determine if each object is a particular type but i don't know how to do this. here is the code
import java.util.*;
import java.util.ArrayList;
public class Driver {
public static void main(String[] args){
/* Comments required
PersonFileReader pfr = new PersonFileReader("person.dat");
ArrayList<Person> testData = pfr.readData();
Database db = new Database(testData);
[Code] .....
I am working on an assignment where I initially have to take as input a number. That number will be the number of strings the user is going to type. As they are typed, they are to be sorted alphabetically, then printed. It took me a while to get this far, and I am stuck on how to properly invoke the other methods in main. There has to be at least three methods: main, one to do the sorting, and one to print the new array. I'm quite sure there are mistakes in my code, but this is definitely a challenge for me. Here is the code. Keep in mind that the methods being invoked are blank on purpose.
I am not allowed to use the Arrays, Arraylist or Collection class. This is basically to test my ingenuity and to see how arrays actually work.
import java.util.*;
public class Sort_as_Inserted {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter number of elements to be sorted: ");
String element_number = input.nextLine();
[Code] ....