Pergunta

I'm pretty sure it has something to do with delimiters and I'm relatively new with Java and can't seem to find an answer. I have to read from a file called Simulation.Configuration which has this data in it:

    dimensionX: 100
    dimensionY: 100
    numberOfMobileObjects: 5
    durationOfSimulationTime: 10
    WarningGeotask: 1, 3
    WarningGeotask: 2, 3
    CounterGeotask: 1, 3
    PopuplationMonitoringGeotask: 2, 3, 3

I have a scanner reading through to find the data and do certain things with them. I can easily get past the labels (dimensionX, dimensionY, and so forth)but can't seem to get to the numbers. I've tried a lot of ways using delimiters to reach those numbers, but cannot seem to get to them.The most recent one I've tried is using

scanner.useDelimiter("[ ,:]");

to get through to the data. When I do this, it gives me an InputMismatchException. Here's how I am reading the file:

    Scanner scanConfig = new Scanner(new File("Simulation.Configuration"));

    scanConfig.useDelimiter("[ ,:]");

    int dimensionX = 0;
    int dimensionY = 0;
    int numberOfMobileObjects = 0;


    while(scanConfig.hasNextLine()){

        String nextLabel = scanConfig.next();
        if(nextLabel.equals("dimensionX")){

            dimensionX = scanConfig.nextInt();
        }
        if(nextLabel.equals("dimensionY")){

            dimensionY = scanConfig.nextInt();
        }
        if(nextLabel.equals("numberOfMobileObjects")){

            numberOfMobileObjects = scanConfig.nextInt();
        }

I've looked all over and couldn't find anything that helped my situation. Any tips on this would be lovely.

Foi útil?

Solução

The regex you've provided for the delimiter isn't doing what you think it is. I would take this approach instead.

String input = scanner.nextLine();
String[] array = input.split(":");
String label = array[0];
String data = array[1];    
String[] number = data.split(",");   
Integer x = new Integer(number[0]);

Documentation for String#split()

Outras dicas

Your useDelimiter argument is wrong. "[ ,:]" uses exactly one character as a delimiter. You want any set of those. Simply change it to: "[ ,:]+".

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top