I am a new learner of Java.

Thq question is "Write a Java program ShowStars.java so that java ShowStars numA1 numB1 .... display a sequence of stars "*", starting from position numA1 and ending at position numB1. If there are further such integer pairs, say, numA2 and numB2, do the same for the next line. For example,
java ShowStars 2 3 1 4
gives

**
****


my incomplete code is shown below:

import java.util.Scanner;
import java.lang.String;

public class ShowStars {
public static void main(String[] args) {
    String aString;
    
    for (int i = 0; i < num1.length; i++){
        for (int j = 0; j < num2.length; j++)
            System.out.print("*");
    }
}

Any suggestions what should I add?

Dani AI

Generated

The intent is to treat the command‑line numbers as (start, end) column positions and print each pair on its own line: print (start-1) spaces then (end-start+1) stars. The forum HTML can hide leading spaces, which is why the example in the thread looked like there were none. As pointed out, use args[] (not Scanner) and parse each string to an integer; correctly flagged the error of using a String as a loop bound.

Algorithm summary

  • Check that the arguments come in pairs; parse each pair with Integer.parseInt().
  • Validate inputs (positive integers). If start > end, swap them or report an error.
  • For each pair print (start-1) spaces followed by (end-start+1) asterisks.

Example (command and expected output)

> java ShowStars 2 3 1 4
 **     <-- one leading space then two stars
****     <-- no leading space then four stars

Sample implementation (works on older Java versions)

public class ShowStars {
    public static void main(String[] args) {
        if (args.length == 0 || args.length % 2 != 0) {
            System.err.println("Usage: java ShowStars numA1 numB1 [numA2 numB2 ...]");
            return;
        }
        for (int i = 0; i < args.length; i += 2) {
            int a, b;
            try {
                a = Integer.parseInt(args[i]);
                b = Integer.parseInt(args[i + 1]);
            } catch (NumberFormatException ex) {
                System.err.println("Non-integer argument: " + args[i] + " " + args[i + 1]);
                continue;
            }
            if (a <= 0 || b <= 0) {
                System.err.println("Positions must be positive: " + a + " " + b);
                continue;
            }
            if (a > b) { int t = a; a = b; b = t; }
            int spaces = a - 1;
            int stars = b - a + 1;
            StringBuilder sb = new StringBuilder(spaces + stars);
            for (int s = 0; s < spaces; s++) sb.append(' ');
            for (int s = 0; s < stars; s++) sb.append('*');
            System.out.println(sb.toString());
        }
    }
}

Troubleshooting tips

  • If you see no leading spaces in forum examples, remember HTML collapsing.
  • Test with simple pairs first (e.g., 1 1, 3 5) and print debug values for a and b if results are unexpected.
  • Avoid using a String directly as a numeric loop bound (as noted); always parse to int and catch NumberFormatException.

Recommended Answers

All 5 Replies

Ignore my incomplete code above.

Here is my code but it is still not finished.

import java.util.Scanner;

public class Stars {
    public static void main(String args[]) {
        System.out.println("Enter an int:");

        Scanner sc = new Scanner(System.in);
        int num1 = sc.nextInt();
        int num2 = sc.nextInt();
        int num3 = sc.nextInt();
        int num4 = sc.nextInt();

        for(int i = 0; i < num1; i++)
            System.out.print("*");
            for(int j = 0; j < num2; j++)
                System.out.print(" ");

        System.out.println();
    }
}

For example,
java ShowStars 2 3 1 4

First comment about your code is that it doesn't follow the example you show.
The String[] args passed to the main() method would contain the args shown by your command line:
args[0] is 2, args[1] is 3 etc

it is still not finished.

Do you have any specific problems? If so, ask some questions about them.

import java.util.Scanner;
import java.lang.String;

class Stars {
    public static void main(String[] args) {
        try {
            Scanner input = new Scanner(System.in);
            System.out.println("enter the number: ");
			String aString;
			aString = input.nextLine();
			int aInt = Integer.parseInt(aString);
            for(int i = 0; i < aString; i++){
                for(int j = 0; j < aString; j++) {
                    System.out.print("*");
                }
            System.out.print(""); }
        }
    catch(Exception e){}
}
}

Have you solved your problems?

You need to post what errors do you get. Don't expect that is easy for us to take your code and run it. Or that we read it line by line and expect to understand what you were thinking.

Also I happened to run the latest code and this is wrong: int i = 0; i < aString; i++ aString is a String. What is it doing there? You are suppose to put a boolean expression there in order to tell when the loop will stop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.