char question

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 1888 Views - Last Post: 28 April 2009 - 04:17 AM Rate Topic: -----

#16 333OnlyHalfEvil   User is offline

  • D.I.C Addict

Reputation: 24
  • View blog
  • Posts: 674
  • Joined: 20-March 09

Re: char question

Posted 25 April 2009 - 08:22 PM

View PostPeanutButter, on 24 Apr, 2009 - 05:23 PM, said:

Is this game played in the console or does it have a swing GUI?


It has a GUI.

View PostFuzzyness, on 24 Apr, 2009 - 08:41 PM, said:

View Post333OnlyHalfEvil, on 24 Apr, 2009 - 12:31 AM, said:

I made a hangman style game. The reason I want a truly empty char is because to input a letter guess you have to erase the empty char and then type your letter for the program to determine if the letter exists in the unknown word. I know that there is ways around this like using String or making the program delete the empty char before it starts processing your letter guess. It would just be so much easier to have a truly empty char.

Hmm.. why not something like this?
Scanner in = new Scanner(System.in);
boolean play = false;
System.out.print("Do you wish to play?");
if(in.nextLine.charAt(0) == ('y' || 'Y'))
 play = true;
String guess;
char charGuess;
while(play)
{
guess = null;// Resets guess to null incase carry over from prev loop round
charGuess = null; // resets the charGuess to null to avoid carry over after loop finishes once
System.out.print("Please enter a letter to guess: ");
guess = input.next();// Scans the letter/word they enter
charGuess = guess.charAt(0); // gets the first char of the enterance just because we know people dont follow directions
for(int i = 0; i<word.length;i++) // assuming you made the word to be guess an array to be checked
	if(word[i] == charGuess) // checks the array at i to see if the charGuess is the same as it
		dashes[i] = charGuess; //Assuming you made an array of dashes to be guessed, will switch the correct index to the guess letter
}


There is a little example of a piece you may think about. Its not nearly the full game or the code, just a method of switch the chars and showing the word.


The game uses a GUI.

View Postpbl, on 24 Apr, 2009 - 09:10 PM, said:

View PostLocke, on 24 Apr, 2009 - 02:59 PM, said:

I'm not entirely sure if there is a truly empty char.

It is not ! Is 0 an empty integer ?

You just need 2 arrays of char
one to hold the real values.... and one to hold the values to display into which cahr = ' '


I'll give the char array a try.

View PostLocke, on 25 Apr, 2009 - 06:13 PM, said:

View Postpbl, on 24 Apr, 2009 - 11:10 PM, said:

View PostLocke, on 24 Apr, 2009 - 02:59 PM, said:

I'm not entirely sure if there is a truly empty char.

It is not ! Is 0 an empty integer ?


I'd bet 0 isn't an empty char, since it is still an ASCII value that Java can infer from...


When you use the ASCII value 0 it gives a little square instead of an empty char.
Was This Post Helpful? 0
  • +
  • -

#17 SpeedisaVirus   User is offline

  • Baller

Reputation: 115
  • View blog
  • Posts: 855
  • Joined: 06-October 08

Re: char question

Posted 26 April 2009 - 11:51 AM

Pretty sure java doesn't have a true empty char. Chars are primatives and need a value. I am not going to do it but perhaps this will give some light...do this and see what happens.
String s = "";
char [] arr = s.toCharArray()
system.out.println(arr[0]);


Or something like this. You would want to find out what is stored in the 0th index of this array. If there is an empty char that would be it.

This post has been edited by SpeedisaVirus: 26 April 2009 - 11:52 AM

Was This Post Helpful? 0
  • +
  • -

#18 Locke   User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 552
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: char question

Posted 26 April 2009 - 11:53 AM

View PostSpeedisaVirus, on 26 Apr, 2009 - 12:51 PM, said:

Pretty sure java doesn't have a true empty char. Chars are primatives and need a value. I am not going to do it but perhaps this will give some light...do this and see what happens.
String s = "";
char [] arr = s.toCharArray()
system.out.println(arr[0]);


Or something like this. You would want to find out what is stored in the 0th index of this array. If there is an empty char that would be it.


Watch the typoes here...:rolleyes:

This post has been edited by Locke: 26 April 2009 - 11:54 AM

Was This Post Helpful? 0
  • +
  • -

#19 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: char question

Posted 26 April 2009 - 04:32 PM

View PostLocke, on 25 Apr, 2009 - 06:13 PM, said:

View Postpbl, on 24 Apr, 2009 - 11:10 PM, said:

View PostLocke, on 24 Apr, 2009 - 02:59 PM, said:

I'm not entirely sure if there is a truly empty char.

It is not ! Is 0 an empty integer ?


I'd bet 0 isn't an empty char, since it is still an ASCII value that Java can infer from...

Empty INTEGER I said not empty CHAR
Was This Post Helpful? 0
  • +
  • -

#20 computerfox   User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: char question

Posted 26 April 2009 - 05:35 PM

just make a space. there is no "empty" char in JAVA. it's called not making that char :wink:
Was This Post Helpful? 0
  • +
  • -

#21 Locke   User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 552
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: char question

Posted 26 April 2009 - 06:32 PM

View Postpbl, on 26 Apr, 2009 - 05:32 PM, said:

View PostLocke, on 25 Apr, 2009 - 06:13 PM, said:

View Postpbl, on 24 Apr, 2009 - 11:10 PM, said:

View PostLocke, on 24 Apr, 2009 - 02:59 PM, said:

I'm not entirely sure if there is a truly empty char.

It is not ! Is 0 an empty integer ?


I'd bet 0 isn't an empty char, since it is still an ASCII value that Java can infer from...

Empty INTEGER I said not empty CHAR


Well, no, I know it's not an empty integer...but since it's not an empty integer, it wouldn't be the ASCII value of an empty char, since it's still an ASCII value to be inferred from.

This post has been edited by Locke: 26 April 2009 - 06:34 PM

Was This Post Helpful? 0
  • +
  • -

#22 333OnlyHalfEvil   User is offline

  • D.I.C Addict

Reputation: 24
  • View blog
  • Posts: 674
  • Joined: 20-March 09

Re: char question

Posted 26 April 2009 - 08:50 PM

Setting the value equal to null doesn't work either.
Was This Post Helpful? 0
  • +
  • -

#23 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: char question

Posted 27 April 2009 - 03:43 PM

View Post333OnlyHalfEvil, on 26 Apr, 2009 - 07:50 PM, said:

Setting the value equal to null doesn't work either.

null is only for Object
you can actually use Character instead of char and set it to null
Was This Post Helpful? 0
  • +
  • -

#24 Locke   User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 552
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: char question

Posted 27 April 2009 - 10:56 PM

But then when you try to compare it, won't you get a NullPointerException?
Was This Post Helpful? 0
  • +
  • -

#25 The Fry-O-Lator   User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 27
  • Joined: 25-April 09

Re: char question

Posted 27 April 2009 - 11:41 PM

Try '\0'
Was This Post Helpful? 0
  • +
  • -

#26 333OnlyHalfEvil   User is offline

  • D.I.C Addict

Reputation: 24
  • View blog
  • Posts: 674
  • Joined: 20-March 09

Re: char question

Posted 28 April 2009 - 02:02 AM

View PostThe Fry-O-Lator, on 27 Apr, 2009 - 10:41 PM, said:

Try '\0'


I get the same little square as I do when I try
char ch = 0;
char ch = ' ' - ' ';


This post has been edited by 333OnlyHalfEvil: 28 April 2009 - 02:02 AM

Was This Post Helpful? 0
  • +
  • -

#27 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: char question

Posted 28 April 2009 - 04:17 AM

Yea....
the write your own class

[code]
class MyChar implement Comparable<MyChar>{
char digit;
boolean isNull;

MyChar(char x) {
digit = x;
isNull = x == ' ';
}

Strint toString() {
String str = "";
if(isNull)
return str;
return str + digit;
}

bollean equals(MyChar other) {
if(isNull && other.isNull)
return true;
if(isNull || othe.isNull)
return true;
return digit == other.digit;
}
}
[code]
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2