This site requires JavaScript, please enable it in your browser!
Greenfoot back
SilentPenguin
SilentPenguin wrote ...

2014/11/23

Help with assign a specific name

SilentPenguin SilentPenguin

2014/11/23

#
I am relatively new to Greenfoot and I was wondering how I could assign a specific name to an actor if they have not enter any name into a sting. For example, a player has not enter any name into and i wanted to set the name as a Player 1 etc. Many thanks.
danpost danpost

2014/11/23

#
Just add a line that basically says "if the name variable equals the value it would be if no name was entered, then set the name variable to "Player1". Use the 'equals' method for comparing String objects.
SilentPenguin SilentPenguin

2014/11/23

#
How would i do this? The code below shows what I have done. It saves the players names and returns them to a new world. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; import javax.swing.*;// (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.io.*; /** * Write a description of class Enter_Player_Names here. * * @author (your name) * @version (a version number or a date) */ public class Enter_Player_Names extends Actor { String Player1 = "Player1"; String Player2 = "Player2"; /** * Act - do whatever the Enter_Player_Names wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if(Greenfoot.mouseClicked(this)) { String Player1 = JOptionPane.showInputDialog("Player 1, please enter your name"); if String Player1.isalnum()://If the input playerOneName equals alphnumeric then playerOne is set to playerOneName { Player1 = Player1Name else: Player1 = "Player 1" } String Player2 = JOptionPane.showInputDialog("PLayer 2, please enter your name"); writefile(Player1); writefile2(Player2); returnHome(); } } public void writefile(String Player1) { FileWriter fw = null; try { fw = new FileWriter("data.txt"); //false would rewrite file fw.write(""+Player1+"\r\n"); } catch (Exception e){ e.printStackTrace(); } finally { if(fw != null) { try { fw.close(); } catch (Exception e) { e.printStackTrace(); } } } } public void writefile2(String Player2) { FileWriter fw = null; try { fw = new FileWriter("data.txt"); //false would rewrite file fw.write(""+Player2+"\r\n"); } catch (Exception e){ e.printStackTrace(); } finally { if(fw != null) { try { fw.close(); } catch (Exception e) { e.printStackTrace(); } } } } public void returnHome() { Greenfoot.setWorld(new MainMenu()); }
danpost danpost

2014/11/23

#
First off, you would not use the type when referring to a variable that is already declared. So this:
if String Player1.isalnum():
would not have 'String' in it and you would need round brackets around the condition. So it would be this:
if (Player1.isalnum()) {
    // what to do
}
However, there is no 'isalnum' method in the String class (or any method to check the general type of data stored within the string). All strings could be considered as being alpha-numeric strings; so the question itself is almost not worth asking. What might be important is whether the String object returned was empty or not. Maybe you should be using this:
if ("".equals(Player1)) Player1 = "Player1";
BTW, there is a 'code' link below the reply box for entering code into your posts. Please use it when posting codes.
You need to login to post a reply.