civicy2k
asked on
Capture the Char value and display it
Hi. I have a task that need the user to put in a number (for example: 3), then put in a character (for example: #). And display the following result:
###
###
###
the number (3) equals to the rows and columns.
Here is my code, my problem is I can't make the "#" display. I dunno how to capture the "#" when user input it and show it. I know I should use char instead of short, but dunno how to use char to capture it.
import java.util.Scanner;
public class SquareModify
{
public void squareOfCharacter()
{
Scanner input = new Scanner( System.in );
System.out.print( "Enter a number to determine the size of the square: " );
int side = input.nextInt();
System.out.println();
System.out.print( "Enter a character to be the pattern: " );
short fillCharacter = input.nextShort();
System.out.println();
for ( int rows = 1; rows <= side; rows++ )
{
for ( int cols = 1; cols <= side; cols++ )
System.out.printf( "%s", fillCharacter );
System.out.println();
}
System.out.println();
}
}
Anyone can helps please? Thanks!
###
###
###
the number (3) equals to the rows and columns.
Here is my code, my problem is I can't make the "#" display. I dunno how to capture the "#" when user input it and show it. I know I should use char instead of short, but dunno how to use char to capture it.
import java.util.Scanner;
public class SquareModify
{
public void squareOfCharacter()
{
Scanner input = new Scanner( System.in );
System.out.print( "Enter a number to determine the size of the square: " );
int side = input.nextInt();
System.out.println();
System.out.print( "Enter a character to be the pattern: " );
short fillCharacter = input.nextShort();
System.out.println();
for ( int rows = 1; rows <= side; rows++ )
{
for ( int cols = 1; cols <= side; cols++ )
System.out.printf( "%s", fillCharacter );
System.out.println();
}
System.out.println();
}
}
Anyone can helps please? Thanks!
The decimal value of `#`character is = 35.
Hexa Decimal value for the character '#' is 23
Octal value for the character '#' is 43
Octal value for the character '#' is 43
ASKER
ashok3sep,
Even I made the changes, the type of fillCharacter is still short, and I don't think short will accept the character #, @, $, etc......
Please advise,
Thanks.
Even I made the changes, the type of fillCharacter is still short, and I don't think short will accept the character #, @, $, etc......
Please advise,
Thanks.
ASKER
let me make it clear, if user enter 4 and @, it will display:
@@@@
@@@@
@@@@
@@@@
if 5 and $,
$$$$$
$$$$$
$$$$$
$$$$$
@@@@
@@@@
@@@@
@@@@
if 5 and $,
$$$$$
$$$$$
$$$$$
$$$$$
ASKER
sorry, 5 and $ will be
$$$$$
$$$$$
$$$$$
$$$$$
$$$$$
$$$$$
$$$$$
$$$$$
$$$$$
$$$$$
System.out.print((char)fil lCharacter );
import java.util.*;
public class SquareModify
{
public void squareOfCharacter()
{
System.out.println( "Enter a number to determine the size of the square: " );
int side = 3;
System.out.println();
System.out.println( "Enter a character to be the pattern: " );
short fillCharacter = 35;
for ( int rows = 0; rows < side; rows++ )
{
for ( int cols = 0; cols < side; cols++ )
{
System.out.print((char)fil lCharacter );
//System.out.print();
}
System.out.println();
}
}
public static void main(String[] args)
{
SquareModify cc = new SquareModify();
cc.squareOfCharacter();
}
}
public class SquareModify
{
public void squareOfCharacter()
{
System.out.println( "Enter a number to determine the size of the square: " );
int side = 3;
System.out.println();
System.out.println( "Enter a character to be the pattern: " );
short fillCharacter = 35;
for ( int rows = 0; rows < side; rows++ )
{
for ( int cols = 0; cols < side; cols++ )
{
System.out.print((char)fil
//System.out.print();
}
System.out.println();
}
}
public static void main(String[] args)
{
SquareModify cc = new SquareModify();
cc.squareOfCharacter();
}
}
you can change the fillcharacter to any value and see the result
I think this is what you expect
regards,
Áshok
I think this is what you expect
regards,
Áshok
Look at the table values for Ascii
http://www.lookuptables.com/
http://www.lookuptables.com/
ASKER
Ashok,
In your case, you select the fillCharacter value to 35 and then display the result. What if the user want to enter "@" or "#" by themselves after:
System.out.println( "Enter a character to be the pattern: " );
Because user may not know what 35 means, they just want to put in "@" or "#" or "$" and see the pattern.
Thanks.
In your case, you select the fillCharacter value to 35 and then display the result. What if the user want to enter "@" or "#" by themselves after:
System.out.println( "Enter a character to be the pattern: " );
Because user may not know what 35 means, they just want to put in "@" or "#" or "$" and see the pattern.
Thanks.
You can
read the short value
from the user and then casting that short value as
(char)YourShortValue
and printing it.......
read the short value
from the user and then casting that short value as
(char)YourShortValue
and printing it.......
if the user enters the short value as 1 to 65 he gets the corresponding character value for the Short value
ASKER
I guess thats the part I don't know how to do. What is the syntax for that?
just (char)fillCharacter; ?
System.out.print( "Enter a character to be the pattern: " );
short fillCharacter = input.nextShort();
System.out.println();
for ( int rows = 1; rows <= side; rows++ )
{
for ( int cols = 1; cols <= side; cols++ )
System.out.printf( "%s", fillCharacter );
System.out.println();
}
just (char)fillCharacter; ?
System.out.print( "Enter a character to be the pattern: " );
short fillCharacter = input.nextShort();
System.out.println();
for ( int rows = 1; rows <= side; rows++ )
{
for ( int cols = 1; cols <= side; cols++ )
System.out.printf( "%s", fillCharacter );
System.out.println();
}
yES
CHANGE ALSO THE FOR LOOP
>>>>>>>for ( int rows = 1; rows <= side; rows++ )
<<<<<<<<for(int rows = 0; rows < side ; rows++)
>>>>>>>for ( int rows = 1; rows <= side; rows++ )
<<<<<<<<for(int rows = 0; rows < side ; rows++)
Hi Civicy2k,
This is a sample program to input three numbers and then output the average......
import java.util.Scanner;
public class Average3Scanner {
public static void main(String[] args) {
//... Declare local variables;
int a, b, c; // No meaningful names are possible.
int average;
//... Initialize Scanner to read from console.
Scanner input = new Scanner(System.in);
//... Read three numbers from the console.
System.out.print("Enter first number : ");
a = input.nextInt();
System.out.print("Enter second number: ");
b = input.nextInt();
System.out.print("Enter last number : ");
c = input.nextInt();
//... Compute their average.
average = (a + b + c) / 3;
//... Display their average on the console.
System.out.println("Averag e is " + average);
}
}
regards
Freedom.
This is a sample program to input three numbers and then output the average......
import java.util.Scanner;
public class Average3Scanner {
public static void main(String[] args) {
//... Declare local variables;
int a, b, c; // No meaningful names are possible.
int average;
//... Initialize Scanner to read from console.
Scanner input = new Scanner(System.in);
//... Read three numbers from the console.
System.out.print("Enter first number : ");
a = input.nextInt();
System.out.print("Enter second number: ");
b = input.nextInt();
System.out.print("Enter last number : ");
c = input.nextInt();
//... Compute their average.
average = (a + b + c) / 3;
//... Display their average on the console.
System.out.println("Averag
}
}
regards
Freedom.
import java.util.Scanner;
public class SquareModify
{
int side ;
char fillCharacter;
public void squareOfCharacter()
{
Scanner input = new Scanner( System.in );
System.out.print( "Enter a number to determine the size of the square: " );
side = input.nextInt();
System.out.println();
System.out.print( "Enter a character to be the pattern: " );
fillCharacter = (char)input.nextShort();
System.out.println();
for ( int rows = 0; rows < side; rows++ )
{
for ( int cols = 0; cols < side; cols++ )
{
System.out.println(fillCha racter );
System.out.print();
}
System.out.println();
}
}
public static void main(String[] args)
{
SquareModify cc = new SquareModify();
cc.squareOfCharacter();
}
}
Hope this should work
public class SquareModify
{
int side ;
char fillCharacter;
public void squareOfCharacter()
{
Scanner input = new Scanner( System.in );
System.out.print( "Enter a number to determine the size of the square: " );
side = input.nextInt();
System.out.println();
System.out.print( "Enter a character to be the pattern: " );
fillCharacter = (char)input.nextShort();
System.out.println();
for ( int rows = 0; rows < side; rows++ )
{
for ( int cols = 0; cols < side; cols++ )
{
System.out.println(fillCha
System.out.print();
}
System.out.println();
}
}
public static void main(String[] args)
{
SquareModify cc = new SquareModify();
cc.squareOfCharacter();
}
}
Hope this should work
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Hi CEHJ,
Why to use readInt() when there is readShort()
can u explain in brief..............
thanks,
Ashok
Why to use readInt() when there is readShort()
can u explain in brief..............
thanks,
Ashok
short has effectively half the range of char since it is a signed type of the same bit size. On a terminal supporting Unicode, any character above Short.MAX_VALUE would not be available
Thanks for your explanation........
ASKER
Thanks for all your help.
CHEJ, I changed mine per your advise:
import java.util.Scanner;
public class SquareModify
{
int side;
char fillCharacter;
public void squareOfCharacter()
{
Scanner input = new Scanner( System.in );
System.out.print( "Enter a number to determine the size of the square: " );
side = input.nextInt();
System.out.println();
System.out.print( "Enter a character to be the pattern: " );
fillCharacter = (char) input.nextInt();
System.out.println();
for ( int rows = 0; rows < side; rows++ )
{
for ( int cols = 0; cols < side; cols++ )
{
System.out.println( fillCharacter );
}
System.out.println();
}
System.out.println();
}
public static void main( String args[] )
{
SquareModify squareModify = new SquareModify();
squareModify.squareOfChara cter();
}
}
however, when I put "5", then "#", it gave me error. Can you advise please? Thanks.
CHEJ, I changed mine per your advise:
import java.util.Scanner;
public class SquareModify
{
int side;
char fillCharacter;
public void squareOfCharacter()
{
Scanner input = new Scanner( System.in );
System.out.print( "Enter a number to determine the size of the square: " );
side = input.nextInt();
System.out.println();
System.out.print( "Enter a character to be the pattern: " );
fillCharacter = (char) input.nextInt();
System.out.println();
for ( int rows = 0; rows < side; rows++ )
{
for ( int cols = 0; cols < side; cols++ )
{
System.out.println( fillCharacter );
}
System.out.println();
}
System.out.println();
}
public static void main( String args[] )
{
SquareModify squareModify = new SquareModify();
squareModify.squareOfChara
}
}
however, when I put "5", then "#", it gave me error. Can you advise please? Thanks.
You have to input a Number
say for example if you input 5 and 34
you can see the character for 34 printed 5 times....
Hope this is clear.......
Regards,
Freedom
say for example if you input 5 and 34
you can see the character for 34 printed 5 times....
Hope this is clear.......
Regards,
Freedom
You have your print and println calls in the wrong place in the loop. Look at the code i posted and reproduce those calls
i would like to have a split among myself and CEHJ
ASKER
Sorry for not accepting any comments yet.
Thanks!
Thanks!
should be
<<<<<<System.out.println(f