Solved

Formated I/O

Posted on 1998-10-19
11
155 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

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!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

726 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