Forum Stats

  • 3,852,361 Users
  • 2,264,096 Discussions
  • 7,905,046 Comments

Discussions

Using multiple variable names in a for statement?

808767
808767 Member Posts: 19
I have searched, and I just can't seem to find how to do something, I do not have the code atm, but easy enough to explain...

I have a GUI that has 3 rows of 3 fields...

emp1Name, emp1Phone, emp1Fax
emp2Name, emp2Phone, emp2Fax
emp3Name, emp3Phone, emp3Fax

I also have a tabbed file that has the input for each of these fields, it looks something like...

Mike 602-111-1111 602-111-1112
Dave 602-222-2222 602-222-2223
Bethany 602-333-3333 602-333-3334

Here is my method...
    private void getFile() {
        myFile = new File("C:/javainput.txt");
        msgField.setText("Welcome!");
        try {
            BufferedReader br = new BufferedReader(new FileReader(myFile));
            String s;
            while ((s=br.readLine())!=null){
                String f[] = s.split("\t");
                emp1Name.setText(f[0]);
                emp1Phone.setText(f[1]);
                emp1Fax.setText(f[2]);
            }
            br.close();
        } catch (IOException e) {
            msgField.setText("INPUT FILE NOT FOUND!");
        }
    }
My question is, how do I cycle through emp1, emp2 and emp3 in a for statement. I tried empName, but that obviously isn't correct.

Help, or point me to where I can read on this?

Thank you!

Edited by: 805764 on Oct 27, 2010 12:10 PM
«134

Answers

  • jschellSomeoneStoleMyAlias
    jschellSomeoneStoleMyAlias Member Posts: 24,877 Gold Badge
    Use code tags when you post code.

    Obviously if you have more than one of a single type of X (like emp name) then you are going to need something that contains more than one.

    Like a collection or an array.
  • 808767
    808767 Member Posts: 19
    edited Oct 27, 2010 3:11PM
    I guess I need to explain a bit further...

    I am trying to read the first line, and move the variables to emp1.

    Then read the next line and move variables to emp2, then to emp3 before I close the file. I can't do that within the while statement unless I can differentiate between emp1, emp2 and emp3. The only way I can think of doing that is with a for statement, but I don't know how to move each row while adding a 1 to the emp name.

    This is the best example I can use to explain it...
    while ((s=br.readLine())!=null){
        String f[] = s.split("\t");
        for(int i=0; i==2; i++){
            emp<em>Name.setText(f[0]);<br />
    emp[i]Phone.setText(f[1]);<br />
    emp[i]Fax.setText(f[2]);<br />
    }<br />
    }<br />
    <br />
    <br />
    That of course doesn't work, but how do I make it work?<br />
    <br />
    Edited by: 805764 on Oct 27, 2010 12:10 PM </em>
  • dadams07
    dadams07 Member Posts: 1,310
    As the other poster said, your design is likely wrong. Unless this is just a trivial class assignment where you are forced to use those three variables, parse your input fields into an object (whose class you have to write), then add the object to a collection (List, or whatever makes sense for your use). Then you refer to the objects as members of the collection, not as individual variables.

    The individual variable approach is not scalable. For example, what happens if the program has to change to accommodate a thousand input lines? Would you create a thousand individual variables? Clearly not a good or maintainable solution.
  • 808767
    808767 Member Posts: 19
    edited Oct 27, 2010 5:21PM
    The fields are defined in the user interface, so I have to lay them out on the form. There will never be more than 24 at a time. There is no way to show thousands of employees at one time on a GUI. I would simply use a textarea for that if I had to. This means of course, there will never be more than 24 lines of input in the data file.

    emp1Name, emp1Phone, and emp1Fax are simply jTextFields on the GUI.

    Make more sense?

    Edited by: 805764 on Oct 27, 2010 2:20 PM
  • 800185
    800185 Member Posts: 12
    An array or collection of object with those fields is still the solution.
    I don't know why you insist that it isn't.
  • 796440
    796440 Member Posts: 19,179 Gold Trophy
    805764 wrote:
    emp1Name, emp1Phone, and emp1Fax are simply jTextFields on the GUI.
    No, those are variables. JTextFields are objects, and objects don't have names. Perhaps those variables refer to JTextFields. So, why not do as already suggested? Define a class called Employee that holds fields like name, phone, etc. Then create an array of employee objects.

    Google for java array tutorial if you're not familiar with arrays.
  • 808767
    808767 Member Posts: 19
    I guess the reason I didn't, is because I am REAL new to Java, and trying to learn. I wanted to goof around and create something to learn. I am familiar with arrays, but most of my experience comes from top down programming.

    I understand the objects, I guess I do not understand how to actually code the gui to relate to the object.

    Again, I have looked all over, but maybe I am just not thinking oop style.
  • 808767
    808767 Member Posts: 19
    Still not 100% sure of my direction. So I create a class to get the information, I still do not understand how to fill out 24 separate fields on my GUI, even with an array or not.

    Am I just not making sense?
  • 800185
    800185 Member Posts: 12
    You have a set of 8 different groups of 3 GUI elements, right?

    Create an object with properties whose types are those GUI elements.
    class Thing {
      private JTextField name;
      private JWhatever whatever;
      // create a constructor or setters to set these, and a getter to get them.
    }
    Then you create a collection or array of those objects.

    When you set up your GUI, you create the GUI element objects, right? So you do that, and then you add them to the JPanel or whatever, and also add them to objects of type Thing, then add that thing to a list you've created:
    // in a loop:
    JTextField name = new JTextField();
    JWhatever whatever = new JWhatever();
    Thing t = new Thing(name, whatever);
    allThings.add(t);
    Then if you want to iterate through all these fields, you just have to iterate through your list of Thing objects, and get the individual GUI elements.

    Remember, an object and a variable referring to that object are two different things. So if you have a single JTextField, you can have two different references to it, one in the enclosing JPanel, and one in a Thing object.
    800185
  • 796440
    796440 Member Posts: 19,179 Gold Trophy
    Do one thing at a time. First, define your Employee class. Compile it, debug it. Test it. Make sure that you can create Employees, set their names, phone numbers, etc., and get them back.

    Once that works, familiarize yourself with arrays, if necessary, completely separately from your Employee class.

    Once you're comfortable with arrays and with defining a new class (i.e., Employee), write a tiny test program that just, say, creates an array of a certain size and populates it with Employees, and then iterates over it printing out each Employee's information to the console.

    Once that works, set it aside. Now work on a GUI. Start by just displaying one or two hardcoded names and phone numbers. Once that works, add a bit more to it--say by getting that hardcoded information from inside Employee objects.

    And so on.

    Right now you're biting off too much at once. Keep working on one very small new step at a time, only move on to the next step once the previous one is solid, and do the steps independently of each other first, and then combine their respective results. It may sound like a lot of work, but I promise you, in the end, it's faster, easier, and far less frustrating to do it this way.

    At any given step, if you get stuck, post a question here, providing an [url http://sscce.org]SSCCE that shows what you're trying to do, and clearly explain exactly what problems you're having.
    796440
This discussion has been closed.