Scorpiorian83
Scorpiorian83

Reputation: 479

How to properly split a String input of a filename using the split()?

I am creating an ImageViewer application which prompts the user using showInputDialog to first input the name of the first image filename. However, I got stuck splitting the String by using the split(). Say for example the user inputs image1.gif and I have to make sure that I can split them into three elements. Firstly will be image followed by 1 and finally gif. So do you guys think you can help me with it? Thanks in advance!

public static void main(String [] args)
{
    //First input dialog. Example from user will be like:  image1.gif
    String userInput = JOptionPane.showInputDialog("Please input name of first image file");
    Scanner myScanner = new Scanner(userInput);
    String a = userInput.split(".");
    String fileName = myScanner.next();
    String fileNumber = myScanner.next();
    String fileFormat = myScanner.next();
}

Here's the trial part. But it says that the Array index is out of bound exception. Any advise?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

class TestImageViewer {

public static void main(String [] args)
{
    //First input dialog. Example from user will be like:  image1.gif
    String userInput = JOptionPane.showInputDialog("Please input name of first image file");
    Scanner myScanner = new Scanner(userInput);
    String[] a = userInput.split(".");


    System.out.println(a[0]);

}
}

Upvotes: 0

Views: 108

Answers (3)

M A
M A

Reputation: 72864

You could try using a regular expression and a Matcher for more flexibility over the pattern of your input file:

Pattern pattern = Pattern.compile("(.+)(\\d+)\\.(\\w+)");
Matcher matcher = pattern.matcher("image1.gif");
if(matcher.find()) {
    String fileName = matcher.group(1);
    String fileNumber = matcher.group(2);
    String fileFormat = matcher.group(3);
}

(.+)(\\d+)\\.(.+) denotes a pattern consisting of one or more characters, followed by one or more digit, then a dot, and finally one or more word characters that represent the file extension. Each of these are captured in groups inside parentheses.

matcher.group(1) returns image matched by (.+)

matcher.group(2) returns 1 matched by (\\d+)

matcher.group(3) returns gif matched by (\\w+)

Upvotes: 2

krodmannix
krodmannix

Reputation: 845

Whatever you split to must be returned to an array to store all the sections of the String that is split. So, like this:

String[] a = userInput.split(".");

a[0] will hold Image1 and a[1] will hold gif in this example. Knowing this, you can then get the letters from the first index of the array like this:

String number = a[0].replaceAll("[^0-9.]", "");

and the numbers like:

String letter = a[0].replaceAll("[0-9.]", "");

These are Regex functions, which may be good to look into to understand a little more.

EDIT: In response to OP's edits, I guess I didn't put my code in context. Here's the full code:

public static void main(String [] args) {
    //First input dialog. Example from user will be like:  image1.gif
    String directions = JOptionPane.showInputDialog("Please input name of first image file");

    // System.in will set up the scanner to read user input from the keyboard. 
    // myScanner.next() will grab the first "token" (a section of text without spaces) from this input
    Scanner myScanner = new Scanner(System.in);

    // Create variables to store name, number and extension
    String letters, number, ext;
    String fileName = myScanner.next();
    if (fileName.indexOf(".") > -1) {
        String[] a = userInput.split(".");
        letter = a[0].replaceAll("[0-9.]", "");
        number = a[0].replaceAll("[^0-9.]", "");
        ext = a[1];
    }
}

However, it doesn't seem like you grasp some basics about using the Scanner class in Java. Hopefully this helps, but try to look at other examples.

Upvotes: 2

Micah Smith
Micah Smith

Reputation: 4461

split returns an array with the different sections of the string. So if you use . as the delimiter, you will at least need to provide an array that will be set by your call to split. Second, you may want to try an alternate way of finding fileNumber as your files don't seem to be formatted as image.1.gif.

Upvotes: 1

Related Questions