Question

I am trying to grab certain text using a scanner and delimiter but I keep failing.

Sample from from file:

CROSSPLANE_AXIS=X

DEPTH_AXIS=Depth

INPLANE_AXIS_DIR=GUN_TARGET

CROSSPLANE_AXIS_DIR=LEFT_RIGHT

DEPTH_AXIS_DIR=UP_DOWN

ENERGY=6.00

NOMINAL_DMAX=13.00

SSD=1000.00

SCD=450.00

BLOCK=0

WEDGE=App25x25

public static Double energy;
for (int i = 0; i < lengthOfFiles; i++) {
                Scanner readAndStore = new Scanner(content);
                int j = 0;

                while (readAndStore.hasNextLine()) {

                     if (readAndStore.hasNext("ENERGY=")) {
                        readAndStore.useDelimiter("=");
                        energy = readAndStore.nextDouble();
                        }
Was it helpful?

Solution

As all of your lines are the same format it would be easier to

 // read all lines
 while (readAndStore.hasNextLine()) {

     String line = readAndStore.readLine ();


     // split using =
     String [] words = line.split ("=");     // assuming you are going to parse all lines

     if (words[0].equals ("ENERGY")) {
         energy = Double.parseDouble (words[1]);
     }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top