Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: hello,my problem is

  1. #1
    Member
    Join Date
    Nov 2010
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default hello,my problem is

    hello,
    my problem is that, I want to print out the data in certain output format and the data contain numerical and literal values
    the out put should be formatted as the following:

    1 xxx 2010 1000 12
    100 yyyyy 2011 2222 333
    20000 zzz 3333 9494 11
    21 eder 4545 1211 010

    public void print (Worker[] wor)
            	      {
            		    for (int i=0;i<wor.length;i++)
            		    {
            			if(wor[i].id<=9)
            				System.out.print(wor[i].id+" ");
            				       else if(wor[i].id<=99)
            				           System.out.print(wor[i].id+"  ");
            				                  else System.out.print(wor[i].id+" ");
            		    if(wor[i].name.length()==1)
            				System.out.print(wor[i].name+"           ");
            				       else if(wor[i].name.length()==2)
            				           System.out.print(wor[i].name+"          ");
            				                  else if (wor[i].name.length()==3)
            				                  	System.out.print(wor[i].name+"         ");
            				                  	       else if (wor[i].name.length()==4)
            				                  	         System.out.print(wor[i].name+"        ");
            				                  	                else if (wor[i].name.length()==5)
            				                  	                  System.out.print(wor[i].name+"       ");
            				                  	                         else if (wor[i].name.length()==6)
            				                  	                           System.out.print(wor[i].name+"      ");
            				                  	                                  else System.out.print(wor[i].name+"     ");
            		    if(wor[i].entryYear.length()<=9)
            				System.out.print(wor[i].entryYear+"    ");
            				       else if(wor[i].entryYear.length()<=99)
            				           System.out.print(wor[i].entryYear+"   ");
            				                  else System.out.print(wor[i].entryYear+"  ");
            		    if(wor[i].calcSalary()<=9)
            				System.out.print(wor[i].calcSalary()+"   ");
            				       else if(wor[i].calcSalary()<=99)
            				           System.out.print(wor[i].calcSalary()+"  ");  
            				           	      else System.out.print(wor[i].calcSalary()+" "+"\n");
          		       }
            	     }       
     
            }
     
    public Manager(int id,String name,String entryYear,double salary,double commission)
            	      {
            	      	     super(id,name,entryYear,salary);
            	      	     this.commission=commission;
            	      }
            	      public double calcSalary()
            	      {
            	      	return super.salary+((commission/100)*super.salary);
            	      }
     
            	      public void print (Manager[] mgr)
            	{
            		for (int i=0;i<mgr.length;i++)
            		{
            			if(mgr[i].id<=9)
            				System.out.print(mgr[i].id+"   ");
            				       else if(mgr[i].id<=99)
            				           System.out.print(mgr[i].id+"  ");
            				                  else System.out.print(mgr[i].id+" ");
            		    if(mgr[i].name.length()==1)
            				System.out.print(mgr[i].name+"           ");
            				       else if(mgr[i].name.length()==2)
            				           System.out.print(mgr[i].name+"          ");
            				                  else if (mgr[i].name.length()==3)
            				                  	System.out.print(mgr[i].name+"         ");
            				                  	       else if (mgr[i].name.length()==4)
            				                  	         System.out.print(mgr[i].name+"        ");
            				                  	                else if (mgr[i].name.length()==5)
            				                  	                  System.out.print(mgr[i].name+"       ");
            				                  	                         else if (mgr[i].name.length()==6)
            				                  	                           System.out.print(mgr[i].name+"      ");
            				                  	                                  else System.out.print(mgr[i].name+"     ");
            		    if(mgr[i].entryYear.length()<=9)
            				System.out.print(mgr[i].entryYear+"    ");
            				       else if(mgr[i].entryYear.length()<=99)
            				           System.out.print(mgr[i].entryYear+"   ");
            				                  else System.out.print(mgr[i].entryYear+"  ");
            		    if(mgr[i].calcSalary()<=9)
            				System.out.print(mgr[i].calcSalary()+"   ");
            				       else if(mgr[i].calcSalary()<=99)
            				           System.out.print(mgr[i].calcSalary()+"  ");        				                  
            				           	      else System.out.print(mgr[i].calcSalary()+" ");
          		    }
            	 }


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: hello,my problem is

    This may be off-topic but why are you
    if(wor[i].id<=9)

    checking a variable of a class? Does your Worker class have a getId() and setID(int id) method?

    If so, use that here instead of calling the variable id.

    Also, as for your print method, put a System.out.println() after your else blocks.

    That way it'll go onto the next line for your next block of if...else if...else statements.