2 Replies - 288 Views - Last Post: 06 May 2013 - 03:52 AM Rate Topic: -----

#1 sayham  Icon User is offline

  • D.I.C Head

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

Adding caption before astericks...

Posted 03 May 2013 - 10:26 PM

Hi friends, here is my code, but it doesn't read the input words. Any hints will be appreciated.
   import java.util.ArrayList;
   import java.util.Scanner;
   public class displayAsterisksCap 
   {//Start class
      public static void main(String[] args) 
      {//Start main
         ArrayList<Integer> sjb = new ArrayList<Integer>();      
         ArrayList<String> sjb1 = new ArrayList<String>();      
         System.out.println("Enter integers and Q to quit: ");
         Scanner in = new Scanner(System.in);
         while (in.hasNextInt())
         {
            sjb.add(in.nextInt());
         }
         System.out.println("Enter words and 0 to quit: ");
         while (in.hasNextLine())
         {
            sjb1.add(in.nextLine());
         }
         for (int i = 0; i < sjb1.size(); i++)
         {
            System.out.println(sjb1.get(i));
            for (int element : sjb)
            {
               for (int j = 0; j < element; j++)
               {
                  System.out.print(" *");
               }
               System.out.println();
               if(element < 0)
               { 
                  for (int k = 0; k > element; k--)
                  { 
                     System.out.print("*"); 
                  }
                  System.out.println();
               }  
            }
         }
      }//End main
   }//End class


Is This A Good Question/Topic? 0
  • +

Replies To: Adding caption before astericks...

#2 Gungnir  Icon User is offline

  • Your Imaginary Friend

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

Re: Adding caption before astericks...

Posted 03 May 2013 - 10:46 PM

You never actually check to see whether or not the user has entered a Zero -- You take all input with in.nextLine(). "0" is a String, too.
      System.out.println("Enter words and 0 to quit: ");
      while (in.hasNextLine())
      {
    	  String input = in.nextLine();
    	  if(input.equals("0"))
    		  break;
    	  else
    		  sjb1.add(input);
      }


This post has been edited by Gungnir: 03 May 2013 - 10:47 PM

Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Adding caption before astericks...

Posted 06 May 2013 - 03:52 AM

this will always be true

while (in.hasNextLine())

a Scanner on System.in will always wait until the user enters a line
the next statement will be executed when the user touches the <enter> key

You will have to do:

    while(true) {
       System.out.print("Enter bla.. enter an empty line to finish: ");
       String line = scanner.nextLine();
       if(line.length() == 0)
          break;
       ...
    }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1