Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Formated I/O

Posted on 1998-10-19
11
Medium Priority
?
157 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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 600 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

824 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