How do I do this program? I'm new to Java programming...

Ads
 

How do I do this program? I'm new to Java programming...

Suppose you want to print out numbers in brackets, formatted as follows: [1] [2] [3] and so on. Write a method that takes two parameters: howMany and lineLength. The method should printout line numbers from 1 to howMany in the previous format, but it should not output more than lineLength characters on any one line.

View Answers

January 27, 2011 at 11:33 AM

Hi Friend,

Try this:

import java.util.*;
class NumberExample1
{
    public static void main(String[] args) 
    {
        int count=0;
        Scanner input=new Scanner(System.in);
        System.out.print("How many numbers do you want to enter: ");
        int nums=input.nextInt();
        System.out.print("Length of Line: ");
        int len=input.nextInt();
        for(int i=1;i<=nums;i++){
            if(count==len){
                System.out.print("\n");
            }
            System.out.print("["+i+"]"+" ");
       count++;
        }
    }
}

Thanks


January 27, 2011 at 6:40 PM

Thanks so much! I have 1 doubt, the question says it should not output more than lineLength "CHARACTERS" on any one line. That means it counts the entire [1] [2] [3] as a character. Any help with this?

Ads









Related Tutorials/Questions & Answers:
How do I do this program? I'm new to Java programming...
How do I learn Java programming in one day from zero?
Advertisements
How do I learn Java programming in one day from zero?
How do I compare strings in Java?
How do I decompile Java class files?
How do I initialize a byte array in Java?
How do i do the coding for 'leaving a comment' in java
how do i write a java program for this??
how do i begin a two dimensional array?
How do I set environment variables from Java?
How do I write a correct micro-benchmark in Java?
I am new to programming and I am not sure what to do next? Any ideas?
How do I upgrade mysql?
How do I handle the reaction of a circle and a semi-circle colliding in java?
How do I start machine learning if I am a beginner in the programming language?
How can I do it? .click();
How do I read a large file quickly in Java?
How do I resolve this Java Class not found exception?
How do I throw a 404 error from within a java servlet?
how do i solve this problem?
How do I start learning MongoDB?
How do I learn data analysis with Python?
how do i solve this question?
How do I get started with Bootstrap
How do I get started with Bootstrap
How do I learn Spring Framework?
How do I learn Spring Framework?
How do I learn Spring Framework?
How do I learn Spring Framework?
How do I learn Spring Framework?
How do I compile the registration form?

Ads