Hi,
I writ a program that asks the user to input an integer and according to this integer the program should print astrisks equal to the input integer . The program does not compile ??
i need help fix this problem :

import java.util.Scanner;

public class Astrisksline {
   public static void main (String[] args) {

      do {
    int x=0;
    count=0;
    sum=0;
    Scanner keyboard= new Scanner(System.in);
    System.out.print("\nPlease enter an integer or(Exit) to stop:");
    x=keyboard.nextInt();
    while (x>=0) {
       count++;
       sum = sum + x;
       System.out.print("*");
       System.out.print("Please enter an integer or (Exit) to stop:");
       x=keyboard.nextInt();






     }
}

Recommended Answers

All 18 Replies

Hi nanna and welcome to DaniWeb :)

You have a line that reads

do {

If you remove that line I think this will solve your problem. Incidentally, a do-statement needs a condition at the end of it to let java know how long you want to do something for. Syntax should be:

do {
  // whatever it is you want to do
} while ( something == true )

But I don't think your do-statement is necessary for what you are trying to do, so just remove it.

Let us know if you have any other compiler errors and we will try to help.

hi,
thanks for your reply,but still I`ve encountered the same problem ? I need to use the loop here to repeat the integer input then print the astrisks that equal to the user iput .
pls if you have other sugestions ?

Hi nanna and welcome to DaniWeb :)

You have a line that reads

do {

If you remove that line I think this will solve your problem. Incidentally, a do-statement needs a condition at the end of it to let java know how long you want to do something for. Syntax should be:

do {
  // whatever it is you want to do
} while ( something == true )

But I don't think your do-statement is necessary for what you are trying to do, so just remove it.

Let us know if you have any other compiler errors and we will try to help.

What compiler errors are you getting?

hi,
I changed the structure by deleting (do) and using just (while) , but when run the program it just run the first statement which ask the user to input an integer and does not count the integer and convert it to equal astrisks .
the new structure as the following :

import java.util.Scanner;

public class AstrisksLine {
   public static void main (String[] args) {
    int x=1;
    int count=0;



    Scanner keyboard= new Scanner(System.in);
    System.out.print("Please enter an integer or (Exit) to stop:");
     x=keyboard.nextInt();
     while (x >=1);
    {
        count++;

       System.out.print("*" + count );

       x=keyboard.nextInt();



        }      


     }
}

You need a for-loop inside the while statement. The upper limit of the loop will be the input you give

And you need to use [code]

[/code] tags around the code that you post.

I include for loop in my program ,but it runs without compiling ?? it just ask the user to input an integer and when the user enter the integer the program stops and does not show the number of astrisks that equal to the input integer . pls any suggestions ?
my program structure as following :

import java.util.Scanner;

public class AstrisksLine {
   public static void main (String[] args) {
    int x=1;
    int count=0;

     Scanner keyboard= new Scanner(System.in);
    System.out.print("Please enter an integer or (exit) to stop:");
     x=keyboard.nextInt();
     while (x >=1);
    {
     System.out.print("Please enter an integer or (Exit) to stop:");
     x=keyboard.nextInt();
       for ( count = 0; count>=x; count++)
       {
       System.out.print("*" + count );

       x=keyboard.nextInt();



        }      
     } 

   } 
}

I guess you need to be told again USE CODE TAGS

import java.util.Scanner;

public class AstrisksLine {
   public static void main (String[] args) {
    int x=1;
    int count=0;
            
    Scanner keyboard= new Scanner(System.in);
    System.out.print("Please enter an integer or (exit) to stop:");
     x=keyboard.nextInt();
     while (x >=1);
     x++;
    {
     System.out.print("Please enter an integer or (Exit) to stop:");
     x=keyboard.nextInt();
       for ( count = 0; count>=x; count++)
       {
       System.out.print("*" + count );

       x=keyboard.nextInt();
 

               
        }      
     } 
                         
   } 
}
System.out.print("Please enter an integer or (Exit) to stop:");
     x=keyboard.nextInt();
     for ( count = 0; count>=x; count++)
     {
       System.out.print("*" + count );

       x=keyboard.nextInt();      
     }

Your for-loop is wrong. You are saying "start at count = 0 and while count >= x, do the following code and add one to count". Now, either count will not be greater than or equal to x at first (if x is greater than 0) or if it is it will ALWAYS be greater than or equal to x (if x is 0 or less).

Also, there is another problem that I can see with where you call for the next value of x, but I will let you try to figure that one out...

try out this program, i think this would satisfy your requirement.

import java.util.Scanner;


public class Test {
public static void main (String[] args) {


int x=1;
int count=0;
Scanner keyboard= new Scanner(System.in);


while (x >=1)  {
System.out.print("Please enter an integer or (Exit) to stop:");
x=keyboard.nextInt();


for ( count = 0; count<=x; count++) {
System.out.print("*" + count );
}


System.out.println();
x=1;


}


}
}

try out this program, i think this would satisfy your requirement.

import java.util.Scanner;

public class Test {
public static void main (String[] args) {

int x=1;
int count=0;
Scanner keyboard= new Scanner(System.in);

while (x >=1) {
System.out.print("Please enter an integer or (Exit) to stop:");
x=keyboard.nextInt();

for ( count = 0; count<=x; count++) {
System.out.print("*" + count );
}

System.out.println();
x=1;

}

}
}

It is wrong first to go inside the loop and then read the input. Doing this is not correct: x=1; at the end of the loop.
Try this:

System.out.print("Please enter an integer or (Exit) to stop:");
x=keyboard.nextInt();

while (x >=1)  {
       for ( count = 0; count<=x; count++) {
		System.out.print("*" + count );
	}
        System.out.println();	 
  
      System.out.print("Please enter an integer or (Exit) to stop:");
      x=keyboard.nextInt();
}

Plus I would suggest the message to say:
System.out.print("Please enter a positive integer in order to continue")

Because you cannot enter: "Exit" and read it using: keyboard.nextInt()

It is wrong first to go inside the loop and then read the input. Doing this is not correct: x=1; at the end of the loop.
Try this:

System.out.print("Please enter an integer or (Exit) to stop:");
x=keyboard.nextInt();

while (x >=1)  {
       for ( count = 0; count<=x; count++) {
		System.out.print("*" + count );
	}
        System.out.println();	 
  
      System.out.print("Please enter an integer or (Exit) to stop:");
      x=keyboard.nextInt();
}

Plus I would suggest the message to say:
System.out.print("Please enter a positive integer in order to continue")

Because you cannot enter: "Exit" and read it using: keyboard.nextInt()

Hi,
this does not work ? it gives the following results :

Please enter an integer or (Exit) to stop:4
*0
Please enter an integer or (Exit) to stop:2
*1
Please enter an integer or (Exit) to stop:5
*2
Please enter an integer or (Exit) to stop:

Hi,
this does not work ? it gives the following results :

Please enter an integer or (Exit) to stop:4
*0
Please enter an integer or (Exit) to stop:2
*1
Please enter an integer or (Exit) to stop:5
*2
Please enter an integer or (Exit) to stop:

it does exactly what you tell it to do..

try to replace the next for with this one:

for ( count = 0; count<=x; count++) {
		System.out.print("*" + count );
	}
for ( count = 0; count <=x;count ++){
  System.out.print("*");
}

and be sure to remove the

x = 1;

line at the end of your while. it makes the while go on forever (since your x will always agree with x => 1) and it makes sure that only one asterisk is shown (because x = automatically 1, no matter what value you enter

These are the result I get:

Please enter an integer or (Exit) to stop:3
*0*1*2*3
Please enter an integer or (Exit) to stop:0

You must be doing something wrong, or I am missing something, but most likely the problem is from your end

this is one way to do it:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Astrisksline {
   public static void main (String[] args) {
	//use BufferedReader to read from keyboard
	   BufferedReader keyboard= new BufferedReader(new InputStreamReader(System.in));
	//String that will store the input you must init it
	   String text="";
    //Your loop
	do {
    System.out.print("\nPlease enter an integer or(Exit) to stop:");
    try {
		text=keyboard.readLine();
		//Parse the input if its not a integer it throw an exception
		int x=Integer.parseInt(text.trim());
		//loop that will print right number of *
		for(int i=0;i<x;i++){
			System.out.print("*");
		}
		} 
    //here you handle the exception
    catch (Exception e) {
			//if input is not integer or "exit" you inform the user
			if (e instanceof NumberFormatException)
				{
					if (!text.equalsIgnoreCase("exit"))	System.out.println("Wrong number foramt");
				}
		}
    }
	//you check if input equals exit
    while (!text.equalsIgnoreCase("exit")); 
    System.out.println("program ended");
       }
}

I suggest you try some of free java tutorials on the web because your code had lots of errors, and you should download some IDE maybe Eclipse. It's great and its free you can download it here http://www.eclipse.org/downloads/download.php?file=/eclipse/downloads/drops/R-3.4.1-200809111700/eclipse-SDK-3.4.1-win32.zip

You have a much more complex solution than the original code that nanna wrote, and you didn't provide any explanation.
You think that this: if (e instanceof NumberFormatException) is a nice solution?? You should have used 2 catch blocks.

And it would be better to do this:

do {
text=keyboard.readLine();
if (!text.equalsIgnoreCase("Exit")) {
		//Parse the input if its not a integer it throw an exception
		int x=Integer.parseInt(text.trim());
}
}while (!text.equalsIgnoreCase("exit"));

rather than checking in the catch block whether the user entered Exit or something else

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.