Link to home
Create AccountLog in
Avatar of civicy2k
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!
Avatar of ashok3sep
ashok3sep

>>>>> System.out.printf( "%s", fillCharacter );
should be

<<<<<<System.out.println(fillCharacter );
The decimal value of `#`character is = 35.

Hexa Decimal value for the character '#' is  23

Octal value for the character '#' is  43
Avatar of civicy2k

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.
let me make it clear, if user enter 4 and @, it will display:
@@@@
@@@@
@@@@
@@@@

if 5 and $,
$$$$$
$$$$$
$$$$$
$$$$$

sorry, 5 and $ will be
$$$$$
$$$$$
$$$$$
$$$$$
$$$$$
System.out.print((char)fillCharacter);
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)fillCharacter);
                    //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
Look at the table values for Ascii
http://www.lookuptables.com/
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.
You can

 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
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();
               
               }
CHANGE ALSO THE FOR LOOP

>>>>>>>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("Average is " + average);
    }
}
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(fillCharacter );
                    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
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Hi CEHJ,

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........
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.squareOfCharacter();
      }

}


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
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
Sorry for not accepting any comments yet.
Thanks!