2 Replies - 262 Views - Last Post: 03 May 2013 - 09:48 PM Rate Topic: -----

#1 sayham  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 73
  • Joined: 21-March 13

Read negetive value to print astericks...

Posted 03 May 2013 - 09:23 PM

Hi friends, Im trying to read negative value by my code, but don't know how to do it. Any hint will be appreciated.
Here is my code that can only read positive int.
   import java.util.ArrayList;
   import java.util.Scanner;
   public class displayAsterisksNeg
   {//Start class
      public static void main(String[] args) 
      {//Start main
         ArrayList<Integer> sjb = new ArrayList<Integer>();      
         System.out.println("Enter integers and Q to quit: ");
         Scanner in = new Scanner(System.in);
         while (in.hasNextInt())
         {
            sjb.add(in.nextInt());
         }

         for (int element : sjb)
         {
            for (int i = 0; i < element; i++)
            {
               System.out.print(" *");
            }
				System.out.println();
         }
      
      }//End main
   }//End class


Is This A Good Question/Topic? 0
  • +

Replies To: Read negetive value to print astericks...

#2 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: Read negetive value to print astericks...

Posted 03 May 2013 - 09:41 PM

This only deals with positive numbers. You have to check whether or not the number is negative, and then iterate your counter down using the -- operator.

      for (int element : sjb)
      {
    	  if(element < 0)
    		  for (int i = 0; i > element; i--)
    			  System.out.print(" *");
    	  System.out.println();
      }


Was This Post Helpful? 1
  • +
  • -

#3 sayham  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 73
  • Joined: 21-March 13

Re: Read negetive value to print astericks...

Posted 03 May 2013 - 09:48 PM

Thanks Gungnir, its working now. I appreciate your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1