Results 1 to 8 of 8
  1. #1
    jsoeung is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default Java file printing ArrayLists

    I can't get this to print in separate array lists like this
    "8
    9 10 5 20 100 100 100 90 80 84
    11 22 33 44 55 66 77 85 75 62
    48 52 29 100 50 29 70 80 90 70
    0 100 100 90 85 67 85 48 38 49
    100 90 95 98 100 97
    100 90 100 100 100 100
    100 100 100 100 100 100 100 100 0
    90 80 90 80 60"
    I need an arraylist so I can remove the lowest grade

    Java Code:
    import java.io.*;
    import java.util.*;
    public class prog603 {
    	public static void main (String [] args)throws IOException{
    Scanner file = new Scanner(new File("prog603.dat"));
    int size = file.nextInt();
    file.nextLine();  
       for(int i=0; i<size; i++)
    {
       String str = file.nextLine();
       String[] tokens= str.split(" ");
       List<String> bob = new ArrayList<String>();
       for(String x : tokens){
    	   bob.add(x);}
       for(int k = 0; k<tokens.length;k++){
    		System.out.println(bob.get(k));
    	}
    }
    	}
    }

    Output:

    9
    10
    5
    20
    100
    100
    100
    90
    80
    84
    11
    22
    33
    44
    55
    66
    77
    85
    75
    62
    48
    52
    29
    100
    50
    29
    70
    80
    90
    70
    0
    100
    100
    90
    85
    67
    85
    48
    38
    49
    100
    90
    95
    98
    100
    97
    100
    90
    100
    100
    100
    100
    100
    100
    100
    100
    100
    100
    100
    100
    0
    90
    80
    90
    80
    60

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,436
    Rep Power
    25

    Default Re: Java file printing ArrayLists

    The println() method writes an end of line character at the end of what it prints so the next output will be on the next line.
    The print() method does not add an end of line character so the next print out will be on the same line.
    Chose which method to use depending on if you want the next print out on the same line or the next line.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jsoeung is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default Re: Java file printing ArrayLists

    Quote Originally Posted by Norm View Post
    The println() method writes an end of line character at the end of what it prints so the next output will be on the next line.
    The print() method does not add an end of line character so the next print out will be on the same line.
    Chose which method to use depending on if you want the next print out on the same line or the next line.
    So my code is how it's supposed to be? It's just the way I'm printing it?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,436
    Rep Power
    25

    Default Re: Java file printing ArrayLists

    The print out from your program is the way it is because of the way you are printing it.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jsoeung is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default Re: Java file printing ArrayLists

    Quote Originally Posted by Norm View Post
    The print out from your program is the way it is because of the way you are printing it.
    What print should I use
    ?

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,436
    Rep Power
    25

    Default Re: Java file printing ArrayLists

    What print should I use?
    From post #2
    Chose which method to use depending on if you want the next print out on the same line or the next line.

    Write a test program that uses both and see what happens.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    jsoeung is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default Re: Java file printing ArrayLists

    I tried both but they both print all data on one line or all data in one column

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,436
    Rep Power
    25

    Default Re: Java file printing ArrayLists

    Yes that is how the methods work.
    What way do you want the output? If some of this and some of that then you will have to use one and/or the other of the methods to get the output that you want.
    See post#2 again
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Using ArrayLists. Very Very new to Java.
    By grandintegrator in forum New To Java
    Replies: 3
    Last Post: 04-07-2012, 05:50 PM
  2. printing arraylists in an table
    By abhilasha in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 10-01-2011, 08:36 AM
  3. Saving Several arraylists into one file?
    By Cemi in forum New To Java
    Replies: 2
    Last Post: 04-26-2010, 02:04 PM
  4. read and write arraylists to a file
    By corney_16 in forum New To Java
    Replies: 2
    Last Post: 04-20-2010, 11:50 AM
  5. Printing JPEG file using java
    By venkateshcoolmoon in forum Advanced Java
    Replies: 2
    Last Post: 02-18-2010, 05:50 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
  •