Solved

Formated I/O

Posted on 1998-10-19
11
154 Views
Last Modified: 2012-05-04
I need to create an output file with 4 columns. Is there a Java class that allows me to produce formated I/O ?

This is what I want my file to look like:
Block Address    Size     Status       ID
--------------------     ------    ----------       --
      0                     1024    free      
1024                        14     reserved  17
1038                        57     free
0
Comment
Question by:adubon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
11 Comments
 

Author Comment

by:adubon
ID: 1225930
Edited text of question
0
 

Author Comment

by:adubon
ID: 1225931
Edited text of question
0
 

Author Comment

by:adubon
ID: 1225932
Edited text of question
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:adubon
ID: 1225933
Edited text of question
0
 
LVL 6

Expert Comment

by:gadio
ID: 1225934
What you need is to use the formatters. Have a look into the java.text.NumberFormat and DecimalFormat. If you need some samples I'll paste them here later.
0
 

Author Comment

by:adubon
ID: 1225935
I would really appreciate some examples.   Would NumberFormat also allow me to format the first line of strings?
0
 
LVL 8

Accepted Solution

by:
diakov earned 200 total points
ID: 1225936
You can use my two procedures for alligning strings.
With allignString(s, c, size) you get as a result a string long size chars, starting with s and filled with character c up to size, if necessary. If s is longer than size it is cut.
With fillString(c, size) you get as a result a string filled with characters c and length size.
The showResultset() is an example how to use it.

      protected String allignString(String s, char c, int size)
      {
            String res = "";
            int len = s.length();
            if (len >= size)
            {
                  res = s.substring(0, size);
            }
            else
            {
                  String ccc = fillString(c, size - len);
                  res = s + ccc;
            }
            return res;
      }
      protected String fillString(char c, int size)
      {
            char data[] = new char[size];
            for (int i = 0; i < size; i++)
                  data[i] = c;
            return new String(data);
      }

      protected void showResultSet(ResultSet r)
      {
            try
            {
                  ResultSetMetaData md = r.getMetaData();
                  String ctitle, cval;
                  int ctype = 0, csize, ccount;
                  boolean first = true;
                  String displayrow = "";

                  ccount = md.getColumnCount();

                  while (r.next())
                  {
                        if (first)
                        {
                              first = false;
                              displayrow = "";
                              for (int i = 1; i <= ccount; i++)
                              {
                                    csize = md.getColumnDisplaySize(i);
                                    ctitle = md.getColumnLabel(i);
                                    displayrow += allignString(ctitle, ' ', csize);
                              }
                              writeln(displayrow);
                              writeln(fillString('-', displayrow.length()));
                        }
                        displayrow = "";
                        for (int i = 1; i <= ccount; i++)
                        {
                              ctype = md.getColumnType(i);
                              csize = md.getColumnDisplaySize(i);
                              if ((ctype == Types.BINARY) || (ctype == Types.LONGVARBINARY) || (ctype == Types.VARBINARY))
                              {
                                    cval = "BIN";
                              }
                              else
                              {
                                    if (ctype == Types.OTHER)
                                    {
                                    cval = "OBJ";
                                    }
                                    else
                                    {
                                          cval = r.getString(i);
                                          if (r.wasNull())
                                                cval = "NULL";
                                    }
                              }
                              displayrow += allignString(cval, ' ', csize);
                        }
                        writeln(displayrow);
                  }
            }
            catch (SQLException e)
            {
                  while (e != null)
                  {
                        writeln("[error] " + e.getMessage());
                        e = e.getNextException();
                        sqlerrors++;
                  }
            }
      }


0
 
LVL 8

Expert Comment

by:diakov
ID: 1225937
.
0
 

Author Comment

by:adubon
ID: 1225938
I think that there is an actual Java class that will acomplish this.
0
 
LVL 8

Expert Comment

by:diakov
ID: 1225939
Well, feel free to experiment with my functions. They work pretty well for me.

Reject the answer please, to unlock the question.
0
 

Author Comment

by:adubon
ID: 1225940
Thanks for all your help.  If you ever come across the Java class please let me know.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question