Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: reading csv by scanner

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default reading csv by scanner

    Hello,
    I'am reading a CSV file with a "," seperator.
    The problem is that when i have a string with a space in it, it splits
    By the way i put my data in a jtable by a defaulttablemodel.
    What is wrong in my code?

    	public CsvR()
    	{
     
    		String path = System.getProperty("user.dir") + "/src/reader.csv";
     
    		try {
    			Scanner inputStream = new Scanner(new File(path));
    			String line = null;
    			while(inputStream.hasNext())
    			{
    				line = inputStream.next();
    				String[] value = line.split(",");
    				model.addRow(value);
    				System.out.println(value[0]);
    			}
     
    		} catch (FileNotFoundException e) {
     
    			e.printStackTrace();
    		}
     
     
     
    		JPanel panel = new JPanel();
    		panel.add(new JScrollPane(table));
    		this.getContentPane().add(panel);
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.pack();
    		this.setVisible(true);
     
     
     
    	}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: reading csv by scanner

    can you post a sample String and the results of the split() that you are talking about?
    Explain what the problem is and say what you want the result to be.

    Use the Arrays class's toString() method to format the array for printing:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: reading csv by scanner

    How about using comma as the delimiter option in the Scanner itself rather than using the obvious split and accompanying array?
    Scanner (Java Platform SE 7 )

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: reading csv by scanner

    using comma as the delimiter option in the Scanner
    You lose the knowledge of what line/row the data came from.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. scanner reading
    By awan in forum Java SE APIs
    Replies: 2
    Last Post: June 18th, 2011, 11:39 AM
  2. Reading CSV file to take min, max, average of various stats
    By Annorax in forum Collections and Generics
    Replies: 1
    Last Post: March 3rd, 2011, 08:57 PM
  3. Reading CSV files into a program
    By Wrathgarr in forum Java Theory & Questions
    Replies: 2
    Last Post: April 15th, 2010, 10:41 AM
  4. Reading many files using a scanner
    By jayjames90 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: October 22nd, 2009, 04:35 PM
  5. reading a char with SCANNER
    By lotus in forum Java SE APIs
    Replies: 6
    Last Post: July 29th, 2009, 05:03 AM