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

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?









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 do the coding for 'leaving a comment' in java
How do I decompile Java class files?
How do I initialize a byte array in Java?
How do I compare strings in Java?
I am new to programming and I am not sure what to do next? Any ideas?
how do i begin a two dimensional array?
How do I upgrade mysql?
how do i write a java program for this??
How do I start machine learning if I am a beginner in the programming language?
How can I do it? .click();
How do I set environment variables from Java?
How do I write a correct micro-benchmark in Java?
How do i create the node for target SMO in java..???
How do I handle the reaction of a circle and a semi-circle colliding in java?
How do i start to create Download Manager in Java - JSP-Servlet
I am new to ML/AI. Where do I start learning?
How do I learn data analysis with Python?
how do i solve this problem?
how do i solve this question?
How do I start learning MongoDB?
How do I get started with Bootstrap
How do I get started with Bootstrap
How do I compile the registration form?
How do I download urllib3 for python 2.7
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 read a large file quickly in Java?
How do I choose a data science course?
How do I throw a 404 error from within a java servlet?
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING
How do I study big data?
How do I import a CSV file in R?
How do I generate random number?
How do I resolve this Java Class not found exception?
what is java and why do i need it?
how do i grab the url in php?
How do I start learning AI?
How do I become a machine learning engineer?
How do I start data mining?
How do I learn Python data science?
How do I start a data mining company?

Ads