Computer ForumsComputers  

Go Back   Computer Forums > Programmers Lounge > Programming Discussions

Reply
 
LinkBack Thread Tools Display Modes
Old 08-04-2004, 03:31 PM   #1 (permalink)
Newb Techie
 
Join Date: Mar 2004
Posts: 36
Send a message via AIM to DKasler Send a message via Yahoo to DKasler
Default Java programming problem with For Loop

Can anyone tell me what is wrong with this For Loop?

Code:
public class EmployeeApplication
{
   public static void main(String[] args)
    {
      //define variables
      Employee obj;   //the object of Employee class
      String sSource = args[0]; 
      /* if you have multiple inputs you can use for loop
      * to take all inputs one by one
      */
     for( int i = 0; i < sSource.length(); i++)
      {
        obj = sSource[];
      }
 
      String s1 = sSource.substring(0, 11);  //SSN
      String s2 = sSource.substring(12,sSource.length()); //Name
 
      //create an instance of each variable
      obj = new Employee();
 
      //calling the methods of the first object
      obj.setsEmpId(s1);
      obj.setsEmpName(s2);
 
      System.out.println(obj); //toString is automatically called
    }
}

DKasler is offline   Reply With Quote
Old 08-04-2004, 05:49 PM   #2 (permalink)
Super Techie
 
Join Date: Jul 2004
Posts: 398
Default

do you get any compile errors? if not, what is wrong with the output after you run it?
__________________

kostas is offline   Reply With Quote
Old 08-05-2004, 07:01 AM   #3 (permalink)
Ultra Techie
 
Join Date: Sep 2003
Location: Bamberg, Germany
Posts: 549
Send a message via ICQ to Iron_Cross Send a message via MSN to Iron_Cross Send a message via Yahoo to Iron_Cross
Default

Try this:
Code:
public class EmployeeApplication
{
   public static void main(String[] args)
    {
      //define variables
      Employee obj;   //the object of Employee class
      String sSource = args[0]; 
      /* if you have multiple inputs you can use for loop
      * to take all inputs one by one
      */
     for( int i = 0; i < sSource.length(); i++)
      {
        obj = sSource[i];
      }
 
      String s1 = sSource.substring(0, 11);  //SSN
      String s2 = sSource.substring(12,sSource.length()); //Name
 
      //create an instance of each variable
      obj = new Employee();
 
      //calling the methods of the first object
      obj.setsEmpId(s1);
      obj.setsEmpName(s2);
 
      System.out.println(obj); //toString is automatically called
    }
}
See if that works, all I did was change this line:
obj = sSource[i];
I added the i.
__________________

See today\'s Penny-Arcade!(May contain foul lanuage)
Pain is weakness leaving the body.

PM Me for my MSN
Iron_Cross is offline   Reply With Quote
Old 08-05-2004, 08:37 AM   #4 (permalink)
Super Techie
 
Join Date: Aug 2003
Posts: 332
Send a message via AIM to soundgarden
Default

obj = sSource[];

should be

obj=sSource[i];

edit...

lol Iron_Cross, i didnt read your post.
soundgarden is offline   Reply With Quote
Old 08-05-2004, 02:54 PM   #5 (permalink)
Ultra Techie
 
Join Date: Sep 2003
Location: Bamberg, Germany
Posts: 549
Send a message via ICQ to Iron_Cross Send a message via MSN to Iron_Cross Send a message via Yahoo to Iron_Cross
Default

It's all good, I've done that plenty of times 8)
__________________

See today\'s Penny-Arcade!(May contain foul lanuage)
Pain is weakness leaving the body.

PM Me for my MSN
Iron_Cross is offline   Reply With Quote
Old 09-14-2004, 09:49 AM   #6 (permalink)
Newb Techie
 
Join Date: Mar 2004
Posts: 36
Send a message via AIM to DKasler Send a message via Yahoo to DKasler
Default

Thanks alot guys. Seems good now.
DKasler is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 11:43 AM.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.1.0