0

I have been looking for this answer for the past 2 hours with no luck so I am posting my question now. I am sorry if it is similar to one already asked but I could not get an clear answer from what was already answered.

Here goes:

I am doing a school project in which we read in a text file of "Computer" objects (there are 8 fields in total in this form: manufacturer:model:memory:diskMemory:CPU:opticalDrive:OSVersion:RetailPrice) once the file is read you are supposed to separate the fields and construct an array of Computer objects using the constructor that accepts all the above parameters (separated) and store their reference in each position of the array.

Here is where my question is: Afterwards you display a menu and the user selects a few option, the simplest is just to display the list of computer object in the following form on a JOPtionPane:

Manufacturer1     model1     memory1     disk1     CPU1 optical1     OS1     retailPrice1
Manufacturer2     model2     memory2     disk2     CPU2 optical2     OS2     retailPrice2

and so on until you finish the array. I cannot figure out how to condense the array of objects into a single string that is in the form above. The Computer class has a getMethod for each of those fields I am just having trouble getting them to be aligned in that way. It has to be on a JOPtionPane the simple .INFORMATION_MESSAGE kind. If you all need to see the Computer class code let me know and I can post it. This is my first time posting on this website so I apologize if it is in improper form.

Thank you so much for your help, Bob

EDIT: This is what the output should look like: http://i229.photobucket.com/albums/ee38/Yukijin-Uchiha/ScreenShot2014-03-14at113759AM_zps05b5dbb5.png

1
  • Not sure if you considered returning it as a json string? There's this library in google call "gson" which is pretty easy to use. Commented Mar 14, 2014 at 15:29

2 Answers 2

4

If you implement the Computer classes toString() function properly, you should get just what you want. For example:

public class Computer  {
   public String toString()  {
      return  (new StringBuilder(getManufacturer()).append(":").
         append(getModel()).append(":").
         append(getMemory()).append(":").
         append(getDiskMemory()).append(":").
         append(getCPU()).append(":").
         append(getOpticalDrive()).append(":").
         append(getOSVersion()).append(":").
         append(getRetailPrice()).
      toString();
   }
}

Then call it with

String computerObjAsString = aComputerInstance.toString();

To do this with an array of Computer objects:

StringBuilder outBldr = new StringBuilder();
for(Computer cmptr : anArrayOfComputers)  {
   outBldr.append(cmptr).append(System.getPropetry("line.separator", "\r\n"));
}
System.out.println(outBldr);
Sign up to request clarification or add additional context in comments.

2 Comments

Perhaps use the StringBuilder class instead of + ":" +
I am not supposed to change the toString method in my Computer class that is the problem. Also I need a certain number of spaces between each field. I attempted to type that in the question but it would not let the format stay.
1

You should look to override the toString() method in your Computer objects to include the details that you want printed. Then, for each Computer object in your array, call <object name>.toString() to display the appropriate information.

public class Computer {
   private String name;
   private String type;

   public Computer(String n, String t) {
       this.name = n;
       this.type = t;
   }

   public String toString() {
       return "Name: " + this.name + " Type: " + this.type;
   }
   public static void main(String[] args) {
      Computer c = new Computer("atari","old-school");
      System.out.println(c.toString());
   }
}

1 Comment

See the edited post at the top to see what the output itself should look like. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.