I am new to java world and and I've been trying to find answer to this question and couldn't. So can someone explain how can I use already initialized String from outside a thread. Here is the code the string I want to use is "name" but if I make "name" final I can't set value to it.
public class Users {
public static void GenerateNames() {
String name = "";
String str;
for (int i = 0; i <= 2; i++)
name = name + RandNames.GenerateRandomChar();
str = name;
Hashtable ht = new Hashtable();
if (ht.get(str) == null)
{
ht.put(str, name);
}
else {
}
Runnable r = new Runnable() {
public void run() {
int Anketa = (int) (1 + Math.random() * 6);
Hashtable voting = new Hashtable();
if (voting.get(name) == null)
{
}
}
};
new Thread(r).start();
}
}
Also is there a problem that I left "else" empty. I just need it to do nothing.
Just move the code that generates name into a separate method:
public static String GenerateRandomName() {
StringBuilder name = new StringBuilder();
for (int i = 0; i <= 2; i++) {
name.append(RandNames.GenerateRandomChar());
}
return name.toString();
}
The you'll be able to make name final:
public static void GenerateNames() {
final String name = GenerateRandomName();
...
}
Also is there a problem that I left "else" empty. I just need it to do nothing.
Just omit it altogether:
if (ht.get(str) == null)
{
ht.put(str, name);
}
One way to solve this type of problem is to use a second local variable:
public static void GenerateNames() {
String workName = ""; // you can probably think of a better variable name
for (int i = 0; i <= 2; i++)
workName = workName + RandNames.GenerateRandomChar();
final String name = workName;
and now you can use name in your anonymous inner class. (This is a pattern I use fairly often.)
In order to use name variable inside the run method you can declare this variable as a member of the Users class and mark it as static because it is used in static scope.
public class Users {
private static String name = "";
public static void GenerateNames() {
// your code
Runnable r = new Runnable() {
public void run() {
int Anketa = (int) (1 + Math.random() * 6);
Hashtable voting = new Hashtable();
if (voting.get(name) == null) {
}
}
};
new Thread(r).start();
}
}
Related
I am working to create a "basic" UI connect4 game. I am having trouble figuring out why when I call to print the "board", I am getting null, in return. Have I not initialize the array? If so, how do I do so? ~Thanks
My constructor...
public class Connect4{
private String game[][];
public Conncet4(String game[][]){
this.game = game;
}
with one of my methods...
public void dropChipX(int colm){
for(int i = 0; i<game.length;i++) {
for(int j = 0; j<game[0].length;j++) {
if( j%2 == 0 )
game[game.length-1][col] = "|";
else
game[i][j] = " ";
}
}
if(game[game.length-1][colm] == " ")
game[game.length-1][colm] = "X";
else
game[(game.length-1)-count][col] = "X";
count++;
}
I also have a toString to print out the array
public String toString() {
String result = "";
for(int i = 0; i<game.length;i++) {
for(int j = 0; j<game[0].length;j++)
result = (game[i][j]);
result += "\n";
}
return result;
}
The thing I am having trouble with is that when I do run my main, its returning null
public class Connect4TextConsole {
public static void main(String[] args) {
String fun[][] = new String[6][15];
Connect4 connect = new Connect4(fun);
connect.dropChipX(3);
System.out.print(connect);
connect.dropChipY(2);
System.out.print(connect);
}
}
I would advise you to reconsider this code:
public class Connect4{
private String game[][];
public Conncet4(String game[][]){
this.game = game;
}
}
You should make a defensive copy of that 2D array inside the constructor.
Any code that gave you the reference to the game 2D array that's passed to the constructor can modify that mutable reference. Your private designation means nothing.
Here is my program so far:
import java.util.Arrays;
public class HangmanWord {
private String[] possibleWords = {"laptop", "college", "programing"};
private String word;
private char[] progress;
private int wrongCount = 0;
public HangmanWord() {
int randomPossibleWord = (int) (Math.random() * possibleWords.length-1);
String word = possibleWords[randomPossibleWord];
char[] progress = new char[word.length()];
Arrays.fill(progress,'-');
}
public void display() {
System.out.print(progress);
System.out.println();
}
public boolean guess(char c) {
boolean matchFound = false;
for (int i = 0; i < word.length(); i++ ) {
if (word.charAt(i) == c ) {
progress[i] = c;
matchFound = true;
}
}
return false;
}
public boolean isSolved() {
for (int i = 0; i < progress.length; i++ ) {
if(progress[i] == '-'){
return false;
}
}
return true;
}
public int getWrongCount() {
return wrongCount;
}
public String getWord() {
return word;
}
}
public class Hangman {
public static void main(String[] args) {
int MAX_INCORRECT = 5;
System.out.println("Welcome to Hangman.");
HangmanWord wordObj = new HangmanWord();
System.out.print("Here is your word: ");
wordObj.display();
}
}
My output should look something like this:
Welcome to Hangman.
Here is your word: ------
However, I am getting the following errors:
Welcome to Hangman.
Here is your word: Exception in thread "main" java.lang.NullPointerException
at java.io.Writer.write(Writer.java:127)
at java.io.PrintStream.write(PrintStream.java:503)
at java.io.PrintStream.print(PrintStream.java:653)
at HangmanWord.display(HangmanWord.java:16)
at Hangman.main(Hangman.java:9)
You are redefining the variable word and progress in your constructor. You should simply use them without declaring them as a new variables because they're already defined. Currently you are defining them locally, the constructor works but uses those local defined variables and not your object's word and progress variables, therefore when you leave scope and call display() it will use your object's progress array which was never actually initialized.
Change it to the following so you aren't redefining the variables word and progress like so
public HangmanWord() {
int randomPossibleWord = (int) (Math.random() * possibleWords.length-1);
word = possibleWords[randomPossibleWord];
progress = new char[word.length()];
Arrays.fill(progress,'-');
}
According to an exercise for school I am supposed to println the int variable 'aantalWoorden' and the Arraylist 'woorden' after they are updated in the 'woordenNaarLijst' method. But whenever I do that, I get the values from the default constructor (Because they are updated inside a method). The method is supposed to be void, so I can't return the values. How do I get the values of the int and array as they are defined in the 'woordenNaarLijst' method without changing the method itself.
I am sorry if this is a easy question or if I am doing something wrong, but I am relatively new to programming.
public class AnalyseZin {
private String zin;
int aantalWoorden;
int i1 = 0, i2 = 0;
private ArrayList<String> woorden;
public AnalyseZin() {
woorden = new ArrayList<>();
aantalWoorden = 0;
}
public int getIndex(int i) {
char c;
for (; i < zin.length(); i++) {
c = zin.charAt(i);
if (c == '\n' || c == '.' || c == ',' || c == ' ')
return i;
}
return i;
}
public String getWoord() {
i2 = getIndex(i1);
StringBuilder sb = new StringBuilder();
for (; i1 < i2; i1++) {
sb.append(zin.charAt(i1));
}
return sb.toString();
}
public void woordenNaarLijst() {
String s;
while (true) {
s = getWoord();
if (s.length()==0) break;
woorden.add(s);
aantalWoorden++;
}
}
}
I left out irrelevant pieces of code, the code does work at my end.
Maybe if you just want to get the values of aantalWoorden and woorden regardless if it has been properly updated or not, you might want to create getters for those.
Example:
// Additional getter methods in AnalyseZin
public int getAantalWoorden(){
return aantalWoorden;
}
public ArrayList<String> getWoorden(){
return woorden;
}
So if you create the AnalyseZin object you can do this:
// create variables for the container of values from the AnalyseZin
int newIntVal = 0;
ArrayList<String> newListVal = new ArrayList<String>();
// create instance of the object
AnalyseZin test = new AnalyseZin();
// call woordenNaarLijst method to update the woorden and aantalWoorden
test.woordenNaarLijst();
// pass values from AnalyseZin
newIntVal = test.getAantalWoorden
newListVal = test.getWoorden();
// print out int values
System.out.println("new int value = " + Integer.toString(newIntVal));
// print out list contents
for (String item : newListVal){
System.out.println("item from list = " + item);
}
Hope this helps.
I am making a program for airplane seating arrangements for a class and i ended up making two toString methods but when I run the program the toString method in my airplane class is making something not work specifically:
str= str + seats[i][j].toString();
I believe that simply deleting the toString method in the seat class and somehow putting it back into the airplane class toString method would fix the problem or make it simpler. What's wrong?
Airplane class:
public class Airplane
{
private Seat [ ] [ ] seats;
public static final int FIRST_CLASS = 1;
public static final int ECONOMY = 2;
private static final int FC_ROWS = 5;
private static final int FC_COLS = 4;
private static final int ECONOMY_ROWS = 5;
private static final int ECONOMY_COLS = 6;
public Airplane()
{
seats = new Seat[FC_ROWS][ECONOMY_COLS];
}
public String toString()
{
String str = "";
for (int i=0; i<FC_ROWS; i++) {
for (int j=0; j<ECONOMY_COLS; j++)
{
str= str + seats[i][j].toString();
}
str = str + "\n";
}
return str;
}
}
Seat Class:
public class Seat
{
private int seatType;
private boolean isReserved;
public static final int WINDOW = 1;
public static final int AISLE = 2;
public static final int CENTER = 3;
public Seat(int inSeatType)
{
seatType = inSeatType;
isReserved = false;
}
public int getSeatType()
{
return seatType;
}
public void reserveSeat()
{
isReserved = true;
}
public boolean isAvailable()
{
if (!isReserved)
{
return true;
}
else return false;
}
public String toString()
{
if(isReserved == false)
{
return "*";
}
else return "";
}
}
In Seat.toString you should print a " " not "".
You're array is FC_ROWS by ECONOMY_COLS, so you're not creating all the seats. You should probably have two arrays (one for FC, one for Economy), since FC_ROWS != ECONOMY_ROWS.
You aren't actually creating Seats in your constructor. Use a nested loop to create them, otherwise you will get a NullPointerException. Creating an array doesn't create the objects contained in the array.
When you're creating the seats in the Airplane constructor, use if statements to figure out if the seat is supposed to be a Window, Aisle, etc.
seats seems to does not have Seat's instance.
Add this code :
for (int i=0; i<FC_ROWS; i++) {
for (int j=0; j<ECONOMY_COLS; j++)
{
seats[i][j] = new Seat();
}
}
below this :
seats = new Seat[FC_ROWS][ECONOMY_COLS];
I think that in Seat::toString, you mean to return " " (a space) if it isn't reserved.
I am new to using arrays of objects but can't figure out what I am doing wrong and why I keep getting a Null pointer exception. I am trying to create an Theatre class with an array of spotlight objects that are either set to on or off. But - whenever I call on this array I get a null pointer exception.
package theatreLights;
public class TheatreSpotlightApp {
public static void main(String[] args) {
Theatre theTheatre = new Theatre(8);
System.out.println("element 5 " + theTheatre.arrayOfSpotlights[5].toString());
}
}
package theatreLights;
public class Theatre {
spotlight[] arrayOfSpotlights;
public Theatre(int N){
arrayOfSpotlights = new spotlight[N];
for (int i = 0; i < arrayOfSpotlights.length; i++) {
arrayOfSpotlights[i].turnOn();
}
}
}
package theatreLights;
public class spotlight {
int state;
public spotlight(){
state = 0;
}
public void turnOn(){
state = 1;
}
void turnOff(){
state = 0;
}
public String toString(){
String stringState = "";
if(state == 0){
stringState = "is off";
}
else if(state==1){
stringState = "is on";
}
return stringState;
}
}
I must be doing something basic wrong in creating the array but can't figure it out.
replace
arrayOfSpotlights[i].turnOn();
with
arrayOfSpotLights[i] = new Spotlight();
arrayOfSpotlights[i].turnOn();
The line
arrayOfSpotlights = new spotlight[N];
will create an array of spotlights. It will however not populate this array with spotlights.
When you do "arrayOfSpotlights = new spotlight[N];" you init an array of length N, what you need to do is also init each object in it:
for i=0; i<N; i++
arrayOfSpotlights[i] = new spotlight();
arrayOfSpotlights[i].turnOn();
Hope I'm correct :)
You are not creating an spotlight objects.
arrayOfSpotlights = new spotlight[N];
This just creates an array of references to spotlights, not the objects which are referenced.
The simple solution is
for (int i = 0; i < arrayOfSpotlights.length; i++) {
arrayOfSpotlights[i] = new spotlight();
arrayOfSpotlights[i].turnOn();
}
BTW You should use TitleCase for class names.
You could write your class like this, without using cryptic code like 0 and 1
public class Spotlight {
private String state;
public Spotlight() {
turnOff();
}
public void turnOn() {
state = "on";
}
void turnOff() {
state = "off";
}
public String toString() {
return "is " + state;
}
}
You declared the array arrayOfSpotlights, but didn't initialize the members of the array (so they are null - and you get the exception).
Change it to:
public class Theatre {
spotlight[] arrayOfSpotlights;
public Theatre(int N){
arrayOfSpotlights = new spotlight[N];
for (int i = 0; i < arrayOfSpotlights.length; i++) {
arrayOfSpotlights[i]=new spotlight();
arrayOfSpotlights[i].turnOn();
}
}
}
and it should work.