Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Some Help Please :)
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
AshIsMyNam3




PostPosted: Sat Sep 29, 2012 9:21 pm   Post subject: Some Help Please :)

Hey guys!
I need some help with my java program.
Question:
I have to make a program which will classify the character a user enters as a digit, letter (and if it a letter-->lowercase/uppercase, consonant).
When the respective character is entered, the i/o window should look EXACTLY as what's shown:
--Enter f--OUTPUT:
Enter a character: f
It is a letter, lowercase, consonant.
--Enter A--OUTPUT:
Enter a character: A
It is a letter, uppercase.
--Enter 9--OUTPUT:
Enter a character: 9
It is a digit.

I was wondering if there's a way to clear screen in java (similar to "cls" in Turing). If not, is there some way to make it such that the input of the user is not shown in the I/O window?
Currently my program just prints "println" 10 times to hide the users input, but if you scroll up, it's still there.
This is my program (Yes I know it isn't brilliantly structured, but I've just started learning Java, so give me some time Very Happy).
PROGRAM:
//Importing the scanner so I can use it later in the program
import java.util.Scanner;
class LetterDigitDecisionAssignment
{
public static void main (String[] args)
{
//Declaring scanner variable, asking the user to input a character and assigning a variable for the inputted character
Scanner s1 = new Scanner(System.in);
System.out.println ("Enter a character:");
char ch = s1.nextLine().charAt(0);
//Printing 10 blank lines so that I/O box looks like that on the assignment sheet
for (int i=0; i<10; i++){
System.out.println();
}
//Printing the inputted variable
System.out.println ("Enter a character: "+ch);
//If the character is a digit, the program will print "It is a digit"
if (Character.isDigit(ch))
{
System.out.print ("It is a digit.");
}
//If the character is a letter, the program will (eventually) print "It is a letter"
else if (Character.isLetter(ch))
{
//If the character is lowercase, the program will (eventually) print "lowercase"
if (Character.isLowerCase(ch))
{
//If this character is a consonant, the program will print consonant
if (ch != 'a' && ch!= 'A' && ch != 'e' && ch!= 'E' && ch != 'i' && ch!= 'I' && ch != 'o' && ch!= 'O' && ch != 'u' && ch!= 'U')
{
System.out.print ("It is a letter, lowercase, consonant.");
}
//If this character is a vowel, the program will not print anything (else)
else
{
System.out.print ("It is a letter, lowercase.");
}
}
//If the character is uppercase, the program will (eventually) print "uppercase"
if (Character.isUpperCase(ch))
{
if (ch != 'a' && ch!= 'A' && ch != 'e' && ch!= 'E' && ch != 'i' && ch!= 'I' && ch != 'o' && ch!= 'O' && ch != 'u' && ch!= 'U')
{
//If this character is a consonant, the program will print consonant
System.out.print ("It is a letter, uppercase, consonant.");
}
//If this character is a vowel, the program will not print anything (else)
else
{
System.out.print ("It is a letter, uppercase.");
}
}
}
}


Thanks to everyone who views/replies to this. Your help is greatly appreciated!
Sponsor
Sponsor
Sponsor
sponsor
evildaddy911




PostPosted: Sun Sep 30, 2012 9:23 am   Post subject: RE:Some Help Please :)

just draw a white box over the text and reset the text drawing location. there's also a "draw text" method (i don't remember what it's called) that you could use if you can't find a way to reset the location

EDIT: in the graphics class: void drawString(String str, int x, int y)
AshIsMyNam3




PostPosted: Sun Sep 30, 2012 10:42 am   Post subject: Re: RE:Some Help Please :)

evildaddy911 @ Sun Sep 30, 2012 9:23 am wrote:
just draw a white box over the text and reset the text drawing location. there's also a "draw text" method (i don't remember what it's called) that you could use if you can't find a way to reset the location

EDIT: in the graphics class: void drawString(String str, int x, int y)

Thanks for the help. Donated some bits Smile
Zren




PostPosted: Sun Sep 30, 2012 10:43 am   Post subject: RE:Some Help Please :)

@EvilDaddy: He's printing to the console, not a graphics canvas.

@Ash:
System.out is an output stream. The Console/Terminal is the application that reads that stream (and also handles the InputStream). As such, the console itself is separate from the the stream it's reading.

Depending on the console application itself, it may have a command to clear it. Windows console has the "cls" command. However this is not as simple as doing System.out.println("cls"); Nor should you do this method as it wouldn't make your code cross-platform as it's specific to only that console.

There are a couple of ways to "clear the screen", but neither of them are good practice.

The first is to scroll the screen with empty output to make it appear like the console is empty. However, not all console's are the same height in lines, and you should not assume there is a maximum height either.

The second is to get the current Runtime and execute the console specific command. As I've said above, this is a bad idea. There may or may not be a 3rd party library that does this in an abstract way, but you should focus on learning the capabilities of the core language first. To tell the truth, I'm not sure how this would works. My theory is System.out is wrapped in code that always prints the text to the console, and getting the runtime itself will not wrap that in the 'print' command and instead treat it as it's own command. Finding out the truth of the matter involves more delving than I'd wish atm.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: