12 Replies - 426 Views - Last Post: 04 February 2018 - 09:54 AM Rate Topic: -----

#1 ProgrammerXYZ   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 30-May 16

Adding User Input to the program

Posted 01 February 2018 - 06:45 PM

I need help with designing a java library that can handle csv files. Can you please help me on how to go about creating a library that can do so? as well as creating a test application for the library. Please help. Thanks a lot.
Is This A Good Question/Topic? 0
  • +

Replies To: Adding User Input to the program

#2 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 691
  • View blog
  • Posts: 5,268
  • Joined: 25-December 13

Re: Adding User Input to the program

Posted 01 February 2018 - 07:13 PM

Quote

how to go about creating a library

The apache group has a set of programs that do that. If you are looking for design specifications you could look at what they have.
Was This Post Helpful? 0
  • +
  • -

#3 wseng92   User is offline

  • D.I.C Regular

Reputation: 21
  • View blog
  • Posts: 428
  • Joined: 23-September 15

Re: Adding User Input to the program

Posted 01 February 2018 - 08:40 PM

Also posted here
https://www.codingfo...tml#post1546314
Was This Post Helpful? 0
  • +
  • -

#4 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 801
  • View blog
  • Posts: 3,271
  • Joined: 13-June 14

Re: Adding User Input to the program

Posted 01 February 2018 - 11:34 PM

For testing, you can write unit tests using, e.g. JUnit (I've not actually used JUnit 5; all my projects are using 4).
Was This Post Helpful? 0
  • +
  • -

#5 ProgrammerXYZ   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 30-May 16

Re: Adding User Input to the program

Posted 04 February 2018 - 08:58 AM

I am trying to include user input for when they can enter the delimiter but I am not sure how to go about that as I keep getting an error with the following:

Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at demo.CSV.main(CSV.java:19)

package demo;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

	public class CSV {

	    public static void main(String[] args) throws FileNotFoundException {
	    	
	       	Scanner x = new Scanner(System.in);
	       	System.out.println("Enter the File");
	       	String s = x.next();
	       	x.close();	 
	       	Scanner scanner = new Scanner(new File(s));
	        scanner.useDelimiter(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
	        System.out.println("Enter delimiter");
	        Scanner scanner1 = new Scanner(System.in);
	        String format = scanner1.nextLine();
	        while(scanner.hasNext()){
	        System.out.println(scanner.next()+format);        
	        }
	        scanner.close();
	       }

}

Was This Post Helpful? 0
  • +
  • -

#6 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 691
  • View blog
  • Posts: 5,268
  • Joined: 25-December 13

Re: Adding User Input to the program

Posted 04 February 2018 - 09:04 AM

Do not close the Scanner object if it has been created with System.in.
If System.in is closed, it can NOT be used again in the same program.

Only create one Scanner object with System.in and use it through out the program.
Was This Post Helpful? 1
  • +
  • -

#7 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3542
  • View blog
  • Posts: 16,145
  • Joined: 20-September 08

Re: Adding User Input to the program

Posted 04 February 2018 - 09:05 AM

You need to make x (not a good name for a variable) read the delimiter as well then apply it to the Scanner that is going to read the file. I would warn you that CSV parsing is not trivial and recommend that you use an existing library to do it
Was This Post Helpful? 1
  • +
  • -

#8 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7164
  • View blog
  • Posts: 14,933
  • Joined: 16-October 07

Re: Adding User Input to the program

Posted 04 February 2018 - 09:29 AM

Try more methods and less ambiguous names.

Something like:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class CSV {
    // do this once and never again
    private static final Scanner scanner = new Scanner(System.in);
    
    private static String getFilenameFromUser() {
        System.out.println("Enter the File");
        return scanner.next();
    }
    private static String getDelimeterFromUser() {
        // your code here
    }
    
    private static Scanner getFileScanner(String filename) {
        // your code here
    }

    private static void showFile(Scanner fileScanner, String delimeter) {
        // your code here
    }

    public static void main(String[] args) throws FileNotFoundException {
        Scanner fileScanner = getFileScanner(getFilenameFromUser());
        showFile(fileScanner, getDelimeterFromUser());
        fileScanner.close();
    }

}



Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#9 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3542
  • View blog
  • Posts: 16,145
  • Joined: 20-September 08

Re: Adding User Input to the program

Posted 04 February 2018 - 09:38 AM

It's probably a hostage to fortune asking a user to enter a delimiter (for one thing it's not just delimiters but factors like quoting and escapement that complicate CSV) but if you want to allow this then i'd probably just accept one or two command line parameters:

Usage: java Foo [delimiter] <csv file path>

This post has been edited by g00se: 04 February 2018 - 09:39 AM
Reason for edit:: typo

Was This Post Helpful? 1
  • +
  • -

#10 ProgrammerXYZ   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 30-May 16

Re: Adding User Input to the program

Posted 04 February 2018 - 09:46 AM

I want to create a test application using the following CSV file parsing library, but do not know how to go about that. Please help.

Thank you

 package demo;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.lang.String;


	public class CSV {

	    public static void main(String[] args) throws FileNotFoundException {
	    	
	       	Scanner input = new Scanner(System.in);
	       	System.out.println("Enter the File");
	       	String s = input.next();
	       	Scanner scanner = new Scanner(new File(s));
	        scanner.useDelimiter(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
	        System.out.println("Enter delimiter");
	        Scanner v = new Scanner(System.in);
	       	String y = v.nextLine();
	       	String format = y ;
	        while(scanner.hasNext()){
	        System.out.print(scanner.next()+format);        
	        }
	        scanner.close();
	        input.close();
	        v.close();
	        
	       }

}


Was This Post Helpful? 0
  • +
  • -

#11 ProgrammerXYZ   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 30-May 16

Re: Adding User Input to the program

Posted 04 February 2018 - 09:47 AM

Thanks for everyone's reply. They were a great help.
Was This Post Helpful? 0
  • +
  • -

#12 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 801
  • View blog
  • Posts: 3,271
  • Joined: 13-June 14

Re: Adding User Input to the program

Posted 04 February 2018 - 09:53 AM

Please don't create multiple threads on essentially the same topic. I've merged your three threads.

You haven't created a library - a library provides a set of components (classes and interfaces) that one can use in their own applications to perform some task (reading and writing CSV files, in this case). I also mentioned how you can test such a thing earlier on: by writing unit tests.

This post has been edited by ndc85430: 04 February 2018 - 11:22 AM

Was This Post Helpful? 0
  • +
  • -

#13 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 691
  • View blog
  • Posts: 5,268
  • Joined: 25-December 13

Re: Adding User Input to the program

Posted 04 February 2018 - 09:54 AM

The code has two new Scanner(System.in); There should only be one.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1