Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: I have some java question

  1. #1
    Junior Member
    Join Date
    Jan 2020
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I have some java question

    i want to if Input size = 5 or Input size = 8
    output screen need like this
    Input size : 5
    #
    ##
    #.#
    #..#
    #####
    or
    Input size : 8
    #
    ##
    #.#
    #..#
    #...#
    #....#
    #.....#
    ########
    but I donít know how to write this code

    package q4;
     
    import java.util.Scanner;
    public class Q4 {
     
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.print("Input size: ");
            int t = input.nextInt();
     
            for (int n = 1; n <= t ; n++)
                line(n);
        }
        static void line(int n ) {
            String s = "#";
            for (int a = 2; a < n ; a++) 
                s += ".";
                s += "#";
         System.out.println(s);       
        }
    }

  2. #2
    Junior Member
    Join Date
    Jan 2020
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I have some java question

    See Code in this Example

    import java.util.Scanner;
    public class Q4 {
     
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.print("Input size: ");
            int t = input.nextInt();
     
            for (int n = 1; n < t; n++) {
                line(n);
            }
     
            lineLast(t);
        }
     
        static void line(int n) {
            String s = "#";
     
            if (n == 2) {
                s += "#";
            }
     
            if (n > 2) {
                for (int a = 2; a < n; a++) {
                    s += ".";
                }
                s += "#";
            }
     
            System.out.println(s);
        }
     
     
        static void lineLast(int t) {
            String s = "";
            for (int a = 1; a <= t; a++) {
                s += "#";
            }
            System.out.println(s);
        }
     
    }

    Scanner class in java has some methods: next(), nextInt(), nextLong(), nextLine() etc , but no nextChar(). Here is an example to take a next char from input by java.util.Scanner. For more about nextChar , click URL

Similar Threads

  1. Replies: 1
    Last Post: November 1st, 2012, 07:54 AM
  2. Challenging Java Question: Test your Java skill by grouping these terms
    By karthickk3 in forum Java Theory & Questions
    Replies: 3
    Last Post: July 18th, 2012, 10:10 PM
  3. Java EE Question
    By djl1990 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 12th, 2012, 12:42 PM
  4. JAVA RMI question
    By rlk in forum Java Networking
    Replies: 6
    Last Post: March 21st, 2012, 02:22 PM
  5. Java Question
    By NiCKYmcd in forum Java Theory & Questions
    Replies: 1
    Last Post: August 25th, 2010, 08:47 AM