Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 4th, 2005, 6:16 PM   #11
Silvanus
Hobbyist Programmer
 
Silvanus's Avatar
 
Join Date: Aug 2005
Location: Hiding from... them...
Posts: 110
Rep Power: 3 Silvanus is on a distinguished road
You probably want to put it in the if statement that checks if again == y.
Silvanus is offline   Reply With Quote
Old Oct 5th, 2005, 6:19 PM   #12
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 572
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
This is the code I used

import cs1.Keyboard;
import java.util.*;

public class HiLo
{
    public static void main (String[] args)
    {
        int play = 1;
        while(play > 0)
        {
        Random generator = new Random();
        int answer = generator.nextInt(100)+1;
        
        System.out.println("The computer is thinking of a number \nbetween 1 and 100. What is it?");
        int escape = 0;
        while(escape < 1)
        {
        int guess = Keyboard.readInt();
        
        if(guess == answer)
        {
            System.out.println("That's it");
            escape = 1;
        }
        else if(guess > answer)
        {
            System.out.println("Too High");
        }
        else if(guess < answer)
        {
            System.out.println("Too Low");
        }
        }
        
        System.out.println("Would you like to play again (y/n)");
        char again = Keyboard.readChar();
        if(again == 'y')
        {
            System.out.print ("\033c");
            play = 1;
        }
        else if(again == 'n')
        {
            play--;
        }
    }
       
    }
}

all it did was print a wierd symbol back where it says "The computer is thinking of a number between 1 and 100."
crawforddavid2006 is offline   Reply With Quote
Old Oct 6th, 2005, 2:03 AM   #13
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
Quote:
The code I gave you is an standard terminal escape code. It should work beautifully on everything *but* Windows.
that's like saying "this should work on every car, except those with gasoline engines".
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Oct 6th, 2005, 6:34 PM   #14
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 572
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
what will work on windows because that is what i'm using
crawforddavid2006 is offline   Reply With Quote
Old Oct 7th, 2005, 2:37 AM   #15
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
we can't compile anything that includes weird-ass files like cs1.keyboard unless we have that file. that's why all of the posts suck. nobody can run this thing. we're all just left here blowing in the wind. since it's an include, how about do a copy-and-paste and show the full source.
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Oct 7th, 2005, 7:41 PM   #16
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 572
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
Try this
crawforddavid2006 is offline   Reply With Quote
Old Oct 9th, 2005, 10:15 AM   #17
Swimmfree200
Newbie
 
Join Date: Oct 2005
Posts: 2
Rep Power: 0 Swimmfree200 is on a distinguished road
I didnt get too look at what crawford posted but i know that with the JVM you cannot make direct calls to the operating system. This means that the terminal clear code will not work. Also with the skipping lines, this leaves you at the bottom of the screen and most people do not want this. This leaves you really with only one way of doing it. Java has a class which allows you to call "Native classes" meaning classes written in different languages. This means you can write a class in C++ or C that clears the screen and make a call to it in java. Im working on this problem myself right now and am not exactly sure the way to go about it. Here is a link to a class that was already written by someone that will do it for u. http://mimosa.snu.ac.kr/~danke/etc/ClearScreen.zip

I also found another way of doing it using ANSI code but it requires you to alter your config.sys file. This would then make the code machine specific. If you want this code too just let me know.
Swimmfree200 is offline   Reply With Quote
Old Oct 9th, 2005, 10:36 AM   #18
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
Swimmfree200 , the JVM can write to the screen can't it? Good becase that's all the terminal clear screen code needs.

Thanks for the insight.

[php]
import java.io.*;

public class RunCommand {

public static void main(String args[]) {

String s = null;

Process clear;

try {
clear = Runtime.getRuntime().exec("cls");

BufferedReader stdOut = new BufferedReader(new InputStreamReader(clear.getInputStream()));

while((s = stdOut.readLine()) != null)
System.out.print(s);

System.exit(0);
}
catch (IOException e) {
try {
clear = Runtime.getRuntime().exec("clear");

BufferedReader stdOut = new BufferedReader(new InputStreamReader(clear.getInputStream()));

while((s = stdOut.readLine()) != null)
System.out.print(s);

System.exit(0);
} catch(IOException d) {
System.out.println("Get a real machine:");
d.printStackTrace();
System.exit(-1);
}
}
}
}
[/php]

That program clears the screen or throws an IOException on any windows or unix machine with the commands "cls" or "clear" respectively.
__________________


Last edited by tempest; Oct 9th, 2005 at 10:48 AM.
tempest is offline   Reply With Quote
Old Oct 9th, 2005, 11:55 PM   #19
2roll4life7
Programmer
 
2roll4life7's Avatar
 
Join Date: Aug 2005
Location: 0x0010 * 0x0091 + 0x0004
Posts: 65
Rep Power: 3 2roll4life7 is on a distinguished road
Quote:
Originally Posted by tempest
Swimmfree200 , the JVM can write to the screen can't it? Good becase that's all the terminal clear screen code needs.

Thanks for the insight.

[php]
import java.io.*;

public class RunCommand {

public static void main(String args[]) {

String s = null;

Process clear;

try {
clear = Runtime.getRuntime().exec("cls");

BufferedReader stdOut = new BufferedReader(new InputStreamReader(clear.getInputStream()));

while((s = stdOut.readLine()) != null)
System.out.print(s);

System.exit(0);
}
catch (IOException e) {
try {
clear = Runtime.getRuntime().exec("clear");

BufferedReader stdOut = new BufferedReader(new InputStreamReader(clear.getInputStream()));

while((s = stdOut.readLine()) != null)
System.out.print(s);

System.exit(0);
} catch(IOException d) {
System.out.println("Get a real machine:");
d.printStackTrace();
System.exit(-1);
}
}
}
}
[/php]

That program clears the screen or throws an IOException on any windows or unix machine with the commands "cls" or "clear" respectively.
Not that this has anything to do with the topic but I love it when fellow Java programmers terminate the VM before a useful debugging message can be read. Only exception being if System.setOut(...) or setErr(...) was resigned to something like a text file (which could still be a problem, because the file may not have closed properly).
__________________
#if 0 /* in case someone actually tries to compile this */
- libpng version 1.2.8 (example.c)

<Jim_McNeat> Is there like a way to put a compiler in "Just trust me on that one" mode?
2roll4life7 is offline   Reply With Quote
Old Oct 10th, 2005, 9:53 AM   #20
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
There are two things that can go wrong, you can not have "clear" or not have "cls"... if you can't debug that without debugging information you've got issues as a programmer. Not to mention that this is supposed to be wrong in a terminal, so this code would be useless anywhere else...
__________________

tempest is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:47 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC