Home > Blockchain >  how to handle in empty string from reading a file in java
how to handle in empty string from reading a file in java

Time:12-19

the project is to read from a file ex2. I used Delimiter to separate to two strings. but when I get to line 4(complex#) I get a NoSuchElementException(). I want to read that line from the file but, to make my own Exception. and not NoSuchElementException(). how can I do that?

     public static void main(String[] args) throws FileNotFoundException {
    
            List<Polynom<Double>> listDouble;
            List<Polynom<Complex>> listComplex;
            SortedSet<Polynom<Double>> setDouble;
            SortedSet<Polynom<Complex>> setComplex;
    
            int countLines =1;
            Scanner lineScan = null;
            String line = null;
            Scanner sc = new Scanner(new File("ex2.txt"));
            while (sc.hasNextLine()){
                try{
                    line = sc.nextLine();
                    lineScan = new Scanner (line);
                    lineScan.useDelimiter("#");
                    String type = lineScan.next();
                    String value = lineScan.next();
    
    } catch (Exception e) {
                    e.printStackTrace();
                }
'''
this is the file ex2:
complex#[7,(1 5i)]
double#[0,1.0][3,-2]
aaa#[0,1.0][3,-2]
complex#
double#[0,1.0][3,-2]#[-2,1.0]
double#[-2,1.0]
complex#[7,-(1 5i)]
complex#7,(1 5i)
'''

CodePudding user response:

You could do something like this:

    int countLines =1;
    Scanner lineScan = null;
    String line = null;
    Scanner sc = new Scanner(new File("ex2.txt"));
    while (sc.hasNextLine()){
        try{
            line = sc.nextLine();
            String[] arr = line.split("#");
            if(arr.length < 2) {
                throw MyCustomException;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

This checks if the array you got from line.split("#") has less than 2 values and throws your own exception if it does.

CodePudding user response:

When you've read a line from the file, you can use String.split ("delimiter") -> Returns an Array.

It is not necessary to scan a line that you already have in memory.

    int countLines = 1;
    Scanner lineScan = null;
    String line = null;
    Scanner sc = new Scanner(new File("ex2.txt"));
    while (sc.hasNextLine()) {
        try {
            line = sc.nextLine();
            
            String[] array = line.split("#");
            
            if(array.length == 1){
                System.out.println("Line (1 element): "   array[0]);
            }else if(array.length == 2){
                System.out.println("Line (2 elements): "   array[0]   " / "   array[1]);
            }
            else if(array.length == 3){
                System.out.println("Line (3 elements): "   array[0]   " / "   array[1]   " / "   array[2]);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
  • Related