Output of array only on one line - any ideas why?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Output of array only on one line - any ideas why?

Hybrid View

  1. #1
    Join Date
    Nov 2005
    Posts
    44

    Output of array only on one line - any ideas why?

    Hi,

    This is probably an incredibly simple question, but when I output the results of a sort method on an array list of 10,000 integers, it currently outputs the result as one long list of the 10,000 numbers on one line. They are sorted correctly, but the output of the sort is not suitable for easy presentation.

    I currently can't see why this would happen.

    Any ideas please!!!

    My code is as follows:

    Code:
    import simplejava.SimpleReader;
    
    public class NumberListTester {
        
    private int [] numbers;
    private int arraySize, size;
        
        public NumberListTester() 
        {
            int[] numbers = new int[10000];
            arraySize = numbers.length;
            size = 0;
        }
        
        public NumberListTester(String fileName)
        {   SimpleReader file = new SimpleReader(fileName);
            int size =0;
            int next = file.readInt();
            while (!file.finished())
            { size++;
              next = file.readInt();
            }
            
            file = new SimpleReader(fileName);
            numbers = new int[size];
            arraySize = size;
            for (int i = 0; i<size; i++)
                numbers[i] = file.readInt();
        }
        
        public void add() //this is a version of the standard Bubble Sort algorithm//
        {
          int out, in;
    
        for (out = numbers.length - 1; out > 1; out--)
          // outer loop (backward)
          for (in = 0; in < out; in++)
            // inner loop (forward)
            if (numbers[in] > numbers[in + 1]) // out of order?
              swap(in, in + 1); // swap them
        }
        
        private void swap(int one, int two) {
        int temp = numbers[one];
        numbers[one] = numbers[two];
        numbers[two] = temp;
      }
     
        
        public boolean contains( int n ) 
        {
         for( int i = 0; i < numbers.length; i++ )
              if( numbers[ i ] == n )
                   return true;
         return false;
        }
        
        public String toString()
    {
         String can = "";
          for(int i=0; i<numbers.length; i++)
         {
              can += numbers[i];
         }
     
         return can;
    }
    }
    Any help or advice would be appreciated!

  2. #2
    Join Date
    Nov 2005
    Posts
    44

    Re: Output of array only on one line - any ideas why?

    Hi,

    I'm glad to say the problem is now solved. A simple solution using + "\n" in the toString method.

    Have a great weekend!!

Similar Threads

  1. Replies: 0
    Last Post: 08-08-2005, 09:46 AM
  2. Replies: 0
    Last Post: 02-11-2005, 03:49 PM
  3. Command Line and console output
    By Scott in forum VB Classic
    Replies: 6
    Last Post: 05-11-2001, 02:56 PM
  4. Command Line and console output
    By Scott in forum VB Classic
    Replies: 0
    Last Post: 05-09-2001, 11:22 PM
  5. Sample Sites.
    By Murray Foxcroft in forum Web
    Replies: 5
    Last Post: 11-02-2000, 02:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center