![]() |
|
|||||||
| JOIN NOW! | Mark Forums Read |
| Today we are discussing Official Java Thread in the University Forum. Join in the discussion and express your views on Official Java Thread "Any value greater than 255? Well, I tried 266 and the program gives me the value 100001010 when I run it (as expected)...? Hmm...I see. So that would probably used the String.charAt() method? Then I ..." |
![]() |
|
|
LinkBack | Thread Tools |
|
|
#16 (permalink) |
|
Respected Member
![]() Location: .
Gender: Male Posts: 14,536
Rep Power:
703
Rep Level:
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Any value greater than 255? Well, I tried 266 and the program gives me the value 100001010 when I run it (as expected)...?
Hmm...I see. So that would probably used the String.charAt() method? Then I might need a loop...something like... i = 0; While(i<=args.length) i++; Thats to "walk along" the length of string I suppose...I'll see what I can do.
Last edited by Zero; 13-11-2008 at 21:57. |
|
|
|
|
|
#17 (permalink) |
|
Forum Addict! Location: in space
Age: 24
Gender: Male Country:
![]() Posts: 1,681
Rep Power:
115
Rep Level:
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
i need help with my program if any1s got a chance
ima upload the java file and c if u can get it to work prop im doin a noughts and crosses game, and am tryin to play against the computer im having a problem in making the computer make its move after ive made mine [ATTACHMENT REMOVED UPON REQUEST]
![]() ![]() Last edited by DJ B4VVY; 29-07-2009 at 18:48. |
|
|
|
|
|
#18 (permalink) |
|
Loyal Member Location: The Future
Gender: Male Country:
![]() Posts: 3,305
My Mood:
Rep Power:
504
Rep Level:
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
I know this is very ugly dirty code... But please bear in mind, I'm new to Java myself and I've been up since 5am LOL.. Anyway... Here's what I came up with, it works... There's no error checking so if you don't send in valid binary and not letters, it doesn't check... Have a look at it, see what you think, maybe it'll jog your train of thought....
Code:
public static int binToDec (String b)
{
// Declarations
int string_length = 0;
int loop_control = 0;
int inc_b = 1;
char toconvert = 'a';
int converted = 0;
int return_value = 0;
// Initialise
string_length = b.length();
loop_control = string_length;
// Process binary data
while (loop_control > 0)
{
toconvert = b.charAt((loop_control - 1));
converted = Integer.parseInt(Character.toString(toconvert));
return_value = return_value + (converted * inc_b);
inc_b = inc_b * 2;
loop_control = loop_control - 1;
}
// Return value
return return_value;
}
Call it with that main thing you had...
![]() "You ain't have to do much to tempt me titts and over what the age of consent be that said you don't have to be that far over sometimes jailbait is worth goin jail over" |
|
|
|
| Sponsored Links | |
|
|
#20 (permalink) |
|
Loyal Member Location: The Future
Gender: Male Country:
![]() Posts: 3,305
My Mood:
Rep Power:
504
Rep Level:
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
LOL I know better than open a .zip file with code in it.......
![]() "You ain't have to do much to tempt me titts and over what the age of consent be that said you don't have to be that far over sometimes jailbait is worth goin jail over" |
|
|
|
|
|
#21 (permalink) |
|
Loyal Member Location: The Future
Gender: Male Country:
![]() Posts: 3,305
My Mood:
Rep Power:
504
Rep Level:
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
I opened it anyway, I haven't done Swing. So can't tell you.... Soz.... And would need to run it to see what it does... LOL and I don't even know how to do that...
![]() "You ain't have to do much to tempt me titts and over what the age of consent be that said you don't have to be that far over sometimes jailbait is worth goin jail over" |
|
|
|
| Sponsored Links | |
|
|
#22 (permalink) |
|
Forum Addict! Location: in space
Age: 24
Gender: Male Country:
![]() Posts: 1,681
Rep Power:
115
Rep Level:
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
open it up with any editor program
and run it off from dos using the javac - compile and java to run if u guys dnt no how to do it nvm then, i jus thought id give it a shot
![]() ![]() |
|
|
|
|
|
#23 (permalink) | |
|
Respected Member
![]() Location: .
Gender: Male Posts: 14,536
Rep Power:
703
Rep Level:
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
I've desk-checked the loop and I understand it...its given me an idea...I think I'd try to implement it as a for loop and I've got a vague idea how to do that... I'm a bit confused why you assigned "a" to the variable toConvert of type char though...at the declarations section?
Last edited by Zero; 14-11-2008 at 00:04. |
|
|
|
|
|
|
#24 (permalink) |
|
Loyal Member Location: The Future
Gender: Male Country:
![]() Posts: 3,305
My Mood:
Rep Power:
504
Rep Level:
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Code:
C:\>cd C:\oxo2
C:\>javac oxo2.java
Note: oxo2.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
C:\>javac -Xlint oxo2.java
oxo2.java:112: warning: [unchecked] unchecked call to add(E) as a member of the
raw type java.util.ArrayList
al.add(box); // add
s the box to the arraylist
^
1 warning
![]() "You ain't have to do much to tempt me titts and over what the age of consent be that said you don't have to be that far over sometimes jailbait is worth goin jail over" |
|
|
|
| Sponsored Links | |
|
|
#25 (permalink) | |
|
Loyal Member Location: The Future
Gender: Male Country:
![]() Posts: 3,305
My Mood:
Rep Power:
504
Rep Level:
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
No reason, just for the sake of initialising it to a known value. Until I set it to something I don't know whats in there, could be garbage... So set it to something when you declare it, just to ensure that something that was in memory at the time isn't in there. Which could be anything...
![]() "You ain't have to do much to tempt me titts and over what the age of consent be that said you don't have to be that far over sometimes jailbait is worth goin jail over" |
|
|
|
|
|
|
#26 (permalink) |
|
Respected Member
![]() Location: .
Gender: Male Posts: 14,536
Rep Power:
703
Rep Level:
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Oh ok...Hmm...I was reading that when you initialise the variable...without the value...then java uses the default value?
Thats only in Java I think... The default for boolean is False, default for int is 0, default for string is null and default for float is 0.0F etc... Well, my brains not functioning anymore now..so I think I'll give it another shot 2mo and then show what I've got in here..
|
|
|
|
|
|
#28 (permalink) |
|
Respected Member
![]() Location: .
Gender: Male Posts: 14,536
Rep Power:
703
Rep Level:
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Thanks for the sticky mr/mrs mod!
Right, take a look at this; Code:
public static int binToDec (String b)
{
//this method uses a "for" loop
int result = 0;
for (int i = 0; i < b.length(); i++)
{
result = (result * 2) + (b.charAt(i) - '0');
}
return result;
}
//end of method
I haven't really tested it fully and there isn't anything for error/exception handling, so its fairly basic. It still gets the job done though!
Last edited by Zero; 14-11-2008 at 18:09. |
|
|
|
|
|
#29 (permalink) |
|
Respected Member
![]() Location: .
Gender: Male Posts: 14,536
Rep Power:
703
Rep Level:
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Note: I didn't write that code.
I can't seem to follow the logic however... the length of the string determines how many time the method loops, and the "b.charAt" extracts each character from the string... and then minuses zero from it? How does that work? And surely in binary each digit has a certain weight (1,2,4,8,16,32,64,128, etc.)...? Is that sorted out using the expression "result = result * 2" ?
|
|
|
|
|
|
#30 (permalink) |
|
Loyal Member Location: The Future
Gender: Male Country:
![]() Posts: 3,305
My Mood:
Rep Power:
504
Rep Level:
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Looks as if someone is offsetting the ASCII decimal of 0 from whatever number it is to calculate the number that is in the string.
Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion So if you have 3 the ASCII decimal of 3 is -> 51. And 0 in ASCII decimal is -> 48 so: the calculation: '3' - '0' is really doing: 51 - 48 = 3 Yeah it make sense LOLL.... Far simpler than the tripe I wrote....
![]() "You ain't have to do much to tempt me titts and over what the age of consent be that said you don't have to be that far over sometimes jailbait is worth goin jail over" |
|
|
|
| Sponsored Links | |
![]() |
| Bookmarks |
| Tags |
| java, programming |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| The Official GTA: IV Thread | DJ B4VVY | Technology, Hardware, Software and Games | 94 | 07-11-2008 17:34 |
| Euro 08 - Official Thread | Cheeky_Princess | Local, National and International Sports | 84 | 28-06-2008 11:07 |
| Official Music Thread | mindstate | Music and Radio | 26 | 16-05-2008 09:17 |
| official joke on the member above u thread | mindstate | Arcade Chat + Fun & Games Talk | 23 | 10-11-2007 22:45 |