TicTacToe Game - Adding Crosses And Naughts To Empty String

Jul 5, 2014

I have an empty string and I want to add to it crosses and naughts. I want to simulate a board of TicTacToe game. My goal is print out 5 strings like this : "xox xxx oxo". There are 3 groups of symbols separated with a space. The crosses and naughts are filled randomly.

Random rand = new Random();
char[] characters = new char[] { 'x', 'o' };
int numOfTimes = 0;
while (numOfTimes < 5)
{
String board = "";
String space = " ";
for (int group = 0; group < 3 ; group++)

[Code] .....

Why my code doesn't output the desired result ?

View Replies


ADVERTISEMENT

Java Tictactoe Game Images Won't Paint / Be Drawn

Jul 24, 2014

I'm following this tutorial![URL] I've copied over a lot of the code and also copied/pasted other parts! I understand how it works, but I can't figure out why the "X" or "O" won't paint onto the screen when the window is clicked on! Only a button is drawn when I click on the screen.

/*
package tictactoe;
import javax.swing.*;
import java.awt.Image;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.Toolkit;//look into this library

[code]....

View Replies View Related

Adding Empty Row To JTable After Pressing Enter

May 26, 2014

This is what i've done so far.

table.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_ENTER) {
model.addRow(new Object[]{"", "","",""}); } }
public void keyReleased(KeyEvent e) { }
public void keyTyped(KeyEvent e) { } } );

I've added JTable to JPanel and used DefaultTableModel. I have to add a new empty Row to table after i press enter key. But somehow I've not been successful so far.

View Replies View Related

String Testing For Null / Empty?

Oct 13, 2014

I've been beavering away with Java for a few months. But as with all languages the String implementation looks designed to trip up even experienced programmers.

My current development gets data from various sources outside my control. When I get a string I want to test if it is empty/null/or whatever. Simple enough one thinks.

But if you search the internet you see everone seems to have a slightly different approach. So what is the best way of determining that a string is not useful to you?

I've had success with this

if(string == null || string.length() == 0)

But I've seen people using methods - not necessarily of String (e.g equals, empty) and regular expressions.

What is the best approach to this considering coding efficiency and/or processing efficiency (accepting you'd have to be processing a lot of strings for the latter to be an issue).

View Replies View Related

Compile Error - Empty String?

Jul 1, 2014

I'm writing basically my first program for school. I've written small ones, following instructions, but this is the most vague. I'm having issues. I can't figure out what the error means. I'm not done with the code, but I think the ArrayList is throwing me off. I'm trying to gather user input and sum the total. Here's the code:

package graduationplanner;
import java.util.ArrayList;
import java.util.Scanner;
import java.lang.Double;
 public class GraduationPlanner {
public static void main(String[] args) {

[Code] ....

View Replies View Related

How To Compare Int To Empty String To Input Error

Apr 16, 2015

I am having issues with the program below everything works but I can't figure out a way to add code that if a user just hits enter without inputting anything it says "entering in nothing is not a valid choice" I am stuck on how to compare a int to a string ...

//import statements
import java.util.*; //for scanner class
// class beginning
public class Guess {
public static void main(String[] args ) {
//Declare variables area

[Code] ....

View Replies View Related

Swing/AWT/SWT :: Number Format Exception - Empty String

Mar 8, 2014

This is the exception message I am getting when I run the program and I select a job from the check box and enter an hour amount.

import javax.swing.*;
import java.awt.*;
public class HourPanel extends JPanel {
private double hours;
String textField;

[Code] .....

View Replies View Related

Continuously Get User Input Until Empty String Entered

Jul 26, 2014

I have the code below that just keeps getting the user's name and displaying it until the user enter's an empty string. Well, to simulate that, I just hit the keyboard instead of entering any name but for some reasons I am not seeing in my code, the programme just keeps looping.
 
System.out.println("Enter your name :
");
Scanner st = new Scanner(System.in);
while(st.hasNext()){
System.out.println("Enter your name :
");
String name = st.nextLine();
System.out.println(name);
if(name==" ") break;
}
System.out.println("you are out of the while loop now!!");

View Replies View Related

Servlets :: JSessionID Cookie Value - It Always Returns Empty String

Feb 6, 2014

I'm trying to get the jsessionid cookie value using the following line in my jsp. It always returns empty string. Is this correct behaviour?

response.encodeURL("")

View Replies View Related

How To Compare Int To A Empty String To Input Error Message To User

Apr 16, 2015

I made a guess a number program but I am having issue figuring out a way that when a user enter's in nothing for the program to spit out a message saying "hey entering nothing doesn't work try again" then ask for input. I have done some research and from what I have found is to read the input in as a String rather than int, and use something like Integer.valueOf() to get the integer value but I am completely lost on how to apply that to my program here is my code

//import statements
import java.util.*; //for scanner class
// class beginning
public class Guess {
public static void main(String[] args ) {
//Declare variables area
int guess, secretNumber = (int) (Math.random() * 10 + 1), lowGuess,highGuess;

[Code] ....

View Replies View Related

Reading Input With Scanner And Checking If User Entered Empty String?

Apr 7, 2015

The project is a program that allows the user to enter students and enter grades for each student. One of the requirements is that if there is already a grade stored for the student that it will display the previous grade. IF the user then enters a new grade the new grade will be stored. IF the user simply presses enter (enters an empty string) nothing is done. I have everything working except for the requirement of doing nothing if the user enters an empty string. If I just press enter at this point I get a NumberFormatException.

The below code is a method "setTestGrades" from the student class. This method iterates through each student object stored in an array list and then checks if there is a previous grade (Requirement# unset grades have to default to -1) before allowing the user to set a new grade.

public void setTestGrades(int testNumber) { //Sets the grade for the specified test number for each student in the arraylist.
testNumber -= 1;
Scanner input = new Scanner(System.in);
for (int i = 0; i < studentList.size(); i++) {
System.out.println("Please enter the grade for Test #" + (testNumber + 1) + " For Student " + studentList.get(i).getStudentName());

[code]....

View Replies View Related

Adding Items To The Game

Jan 4, 2015

I made my items class and I am storing my item as a String array to list them all and what I want to be able to do is type pickup, have the game read my location and display the items that are avaible to pickup at current location then type the item and store it in my playerInventory. Then, have it check the slot if it is doesn't equal null go to next.

Use a boolean value of 0 = false and 1 = true. then, have it check if the inv array = 1 or 0. if 1 go to next inv. If all are full then, ask player if they would like to replace an item. Use a 2d array for storage for slots and true or false value.

Here is my code

Player.java
package com.PenguinGaming;
import java.util.Random;
import java.util.Scanner;
public class Player {
public void User(){
Commands commands = new Commands();
Map map = new Map();

[Code] .....

View Replies View Related

Method Winner Is Undefined For Type TicTacToe?

Jan 16, 2014

Is there a reason why this error is occurring? I can't identify what's causing it to happen.

Java Code:

package tictac1;
import java.util.*;
public class TicTacToe{
//Instance variables
private char[][] board; //2D array of chars for the board
public TicTacToe(){ //creates a board where each space holds whitespace
board = new char[3][3];
for (int row = 0; row < 3; row ++){

[code]...

View Replies View Related

Tic Tac Toe - Adding Draw System / Game Over

Jul 31, 2014

I have been trying to add a draw system to my code so far once all the boxes have been taken it does not tell the player its a draw game over. I have tried and deleted some code but I have commented some stuff that did'nt work at the bottom and also the X's and O's can be replaced in the same spaces?

import java.util.*;
//import for scanner
public class NoughtsAndCrosses {
public static int row,col; //integer for rows and columns
public static Scanner scan = new Scanner(System.in); //Scans the program
public static char[][] board = new char [4][4]; //Characters for the board set out 4/4

[Code] .....

View Replies View Related

Adding A Tick Rate To Game To Unlock FPS?

Nov 25, 2014

I need to add a tick rate so i can unlock the FPS. Here's the main render class/update class/game loop class.

Added back the formatting

Java Code:

package com.educodes.Main;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;

[Code] ....

View Replies View Related

Text Based Game Adding Items

Apr 11, 2014

I am trying to make a text based game. the game has been working perfectly setting up the rooms, first couple of commands, and running it. I am now trying to add items to it but every time it try to run the game it returns :

java.lang.NullPointerException
at Room.addItem(Room.java:107)
at Game.createRooms(Game.java:133)
at Game.<init>(Game.java:28)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

[Code] .....

Here are the classes that matter for this particular situation

import java.util.HashMap;
public class Item
{
private HashMap<String, Item> itemList;
private String name;
private String itemDescription;

[Code] ....

I know that it is the line

itemList.put(item.getItemName(), new Item(item.getItemName(), item.getItemDescription()));

In the game class that is causing the nullpointer exception i just really cant figure out why that keeps happening and how to add the values correctly....

View Replies View Related

Adding Highscore To Txt File In Applet Game

May 13, 2014

I am making a breakout like game in an applet for fun. i have the game working but for the last part of the game i want to make it so each time the game is played it adds the score the player. however, the code that i wrote does not work at all. Here is the code for the game.

import java.applet.Applet;
import java.awt.Color;
import java.awt.Event;
import java.awt.Graphics;
import java.awt.Rectangle;

[Code] ......

View Replies View Related

Breakout Game - Adding Background Picture

Nov 16, 2014

I've been trying to post background picture for a long time in java. I've just finished writing breakout game and I want it to be more special and full of interesting things. I want to upload picture in java. I write

GImage image= new GImage("LEONARDO.JPG");
image.setSize(50,50)
add(image,200,400)

The size of the whole screen is 400,600.

View Replies View Related

Generic RPG Game - Java Applet When Adding Subroutine

Jul 11, 2014

I am trying to design a generic RPG game. The issue I am having right now is in my Class I have a subroutine (think that is the correct term) that is basically set up to hold a series of Print Statements. I am really just trying to get some Values that are stored within the Applet, have them assigned to their correct variables and then returned in the print statement. Yet, when I run the applet it just pops up the Applet blank and gives me a long list of errors and I really don't understand when they mean.

Here is the code:

The Class

public class CharacterSheet {
final String NL = System.getProperty("line.separator");
public String characterName;
int playerStr;
int playerCha;

[Code] ....

And the error messages:

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException: String is null
at sun.java2d.SunGraphics2D.drawString(Unknown Source)
at TFApplet.paint(TFApplet.java:15)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)

[Code] .....

View Replies View Related

Java Text Adventure Game - Adding Characters

Apr 14, 2014

I am making a java text adventure and i am trying to add characters to it. I wrote it exactly the same as i wrote the items class (which works fine) but for some reason the character class keeps getting a null pointer exception when it gets to line 140 in the room class

characters.add(c);
game class
import java.util.ArrayList;
import java.util.HashMap;
/**
* Game class for Free Willy Five: an exciting text based adventure game.
*

[Code] ....

View Replies View Related

Swing/AWT/SWT :: BorderLayout - Adding Multiple Video Game Oriented Objects Such As Sprites To JFrame?

May 24, 2014

I decided to write a small Java program to experiment around with BorderLayout, because I'm developing a Java game and I need to have 2 objects placed in a single JFrame at the same time, and everyone I asked said I need BorderLayout to do that.Before you answer: Also, is using BorderLayout the best option for adding multiple video game oriented objects such a sprites to a JFrame?

So the Java program I wrote is supposed to place a JButton on the JFrame and ALSO place a graphic component (a rectangle in this case). The problem is, only the button shows up, as can be seen in the image link below: URL....

Here is the code:

**main.java** --> The main method class + JFrame/JPanel/JButton constructor
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class main {

[code]....

View Replies View Related

Adding A Ratio Into A String

Sep 27, 2014

How do i add a ratio into this program. it needs to find the ratio from lowercase to uppercase.

import java.util.Scanner;
public class Strings
{
public static void main(String[] args)

[Code]....

View Replies View Related

Adding Array To A List - String?

Sep 8, 2014

Im making a simple code to add an array to a List (the code im referring to is <String> )

import java.util.*;
public class L5_ArrayListProgram {
public static void main(String[] args){
String[] things = {"lasers","ghouls", "food", "dark"};
List<String>list1 = new ArrayList<String>();
for(String x: things)
list1.add(x);

My simple question is - what are the <String> ...<String> for? I understand it makes the list1 variable a string, but why is it made like this? do we usualy use <String> when we need to make a variable a String?

View Replies View Related

Adding Integer Values Into String Arraylist

May 5, 2014

I'm having an issues with adding integer values to a string list. The question is asking me "the method should iterate over runners, and for each runner generate a random number between 90 and 180 (inclusive) which should be used to set the time (in minutes) for that runner."

I have been able to get the random number and iterating over the runner arraylist but I haven't been able to figure out how to add the values generated into the runners list. I am also using BlueJ.

Here's the whole code I have at the moment:

import java.util.*;
import java.io.*;
import ou.*;
import java.util.Random;
/**
* Write a description of class MarathonAdmin here.
*/
public class MarathonAdmin {
// instance variables - replace the example below with your own

[Code] .....

View Replies View Related

Adding String Line To Existent TXT File

Feb 7, 2015

i'm trying to add new value (string type) in an existent file.say that we have a .txt file which contain "mario"...i ask to the user a new name, and he write for expample "tony", now i want append the word "tony" in the existent file in this way: Iwrote this code:

Java Code:

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

[code]....

first is saved next to the value existing. why? maybe because the program does not check if there is a string in the file?but I do not think. because otherwise it would happen with any name that is not entered in an odd position.

View Replies View Related

Alert Returns Added String Instead Of Adding Var Together

Jan 28, 2014

The following alert returns an added string instead of adding the var together to give me a total?

var tvHours = prompt('How many hours of Tv do you watch last week?');
var webHours = prompt('How many hours of internet surfing did you do last week?');
var gameHours = prompt('How many hours of video games did you play last week?');
alert(tvHours + webHours + gameHours);

Also can you store a var in an alert? e.g ( var time = alert(tvHours + webHours + gameHours); ???

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved