Results 1 to 5 of 5
  1. #1
    KAS
    KAS is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default Write a given number of lines and chararcters of a selected char

    Hi! I'm new to java, and trying to write a program that works like this:

    Ask user for number of lines(ie. 4)
    Ask user for number of characters(ie. 7)
    Ask user for a specific single character(ie. h)

    This should then display the following:
    hhhhhhh
    hhhhhhh
    hhhhhhh
    hhhhhhh

    I have written the following code, but unsure of how to finish it:
    Java Code:
    import java.io.StreamTokenizer;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    
    public class Main {
    
        public static void main(String[] args) throws Exception {
    
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);
            StreamTokenizer st = new StreamTokenizer(br);
            double line;
            int input = 0;
            double number;
            String chars;
    
            //Read number of lines
            do {
                do {
                    System.out.println("Choose number of lines(1-20):");
                    input = st.nextToken();
                } while (input != StreamTokenizer.TT_NUMBER);
                line = st.nval;
            } while (line < 1 || line > 20);
    
            //Read number of characters
            do {
                do {
                    System.out.println("Choose number of characters(1-10):");
                    input = st.nextToken();
                } while (input != StreamTokenizer.TT_NUMBER);
                number = st.nval;
            } while (number < 1 || number > 10);
    
            //Read character from terminal
            do {
                System.out.println("Choose character:");
                input = st.nextToken();
            } while (input != StreamTokenizer.TT_WORD);
            chars = st.sval;
    
            for (int lines = 0; lines < line; lines++) {
                System.out.print(chars.charAt(0));
            }
    
    
            System.out.println();
    
    
        }
    }
    Very grateful for any help, thanks in advance :)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    You're almost there; if you know how to print 'number' characters on a line you are ready.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    KAS
    KAS is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    Appreciate the hint, but I dont fully understand what you are getting at. Probably been thinking about it too long :)

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by KAS View Post
    Appreciate the hint, but I dont fully understand what you are getting at. Probably been thinking about it too long :)
    Ok, here's a spoiler:

    Java Code:
    for (int lines = 0; lines < line; lines++) { // print all lines
         for (int cols= 0; cols < number; cols++) // print 'number' characters
                System.out.print(chars.charAt(0));
         System.out.println();
    }
    I surprises me a bit because you managed to work with the StreamTokenizer for reading all the stuff ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    KAS
    KAS is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Ok, here's a spoiler:

    Java Code:
    for (int lines = 0; lines < line; lines++) { // print all lines
         for (int cols= 0; cols < number; cols++) // print 'number' characters
                System.out.print(chars.charAt(0));
         System.out.println();
    }
    I surprises me a bit because you managed to work with the StreamTokenizer for reading all the stuff ...

    kind regards,

    Jos
    Thanks a bunch.

    I tried something very similiar, but I just couldn't get it to work properly. Didn't save it so I could compare. Guess it was a minor typo somewhere, my mind goes blind when working with the same issue for too long.

    Thanks again!

Similar Threads

  1. check a string char by char
    By Sotsiak in forum New To Java
    Replies: 2
    Last Post: 10-23-2010, 10:24 PM
  2. replaceALL(char oldChar, char newChar) method
    By arson09 in forum New To Java
    Replies: 0
    Last Post: 04-28-2010, 06:48 AM
  3. counting number of lines of system.out
    By IYIaster in forum New To Java
    Replies: 1
    Last Post: 07-21-2009, 01:37 AM
  4. read char words lines
    By Drukqs in forum New To Java
    Replies: 6
    Last Post: 05-14-2009, 07:14 PM
  5. drawing char by char with Graphics
    By diggitydoggz in forum New To Java
    Replies: 5
    Last Post: 12-27-2008, 01:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •