0

I need some help, I've got a matrix 4x4 that shows a mountain, in the Mountain.txt there are the heights of mountain zones:

 1 1 1 1        
 1 2 3 1     
 1 2 2 1           
 1 1 1 1  

And the file Rocks.txt that has a type or rock for each zone:

stone stone stone stone
stone sand sand stone
stone sand sand sand
sand sand sand sand

Public class Mountain {       
int height;       
String typeRock;  
public Mountain (int height, String typeRock) {
this.height = height;
this.typeRock = typeRock;
};
}

How do I read that data from 2 different files and to make objects with it, like

Mountain zone00 = new Mountain(1, stone);
Mountain zone01 = new Mountain(1, stone);
Mountain zone11 = new Mountain(2, sand);

And so on...

1

2 Answers 2

1

Loading a file from a directory could be done like so:

public String loadFile(String path) {
    StringBuilder builder = new StringBuilder();

    try {
        BufferedReader br = new BufferedReader(new FileReader(path));
        String line;
        while((line = br.readLine()) != null) {
            builder.append(line + "\n");
        }

        br.close();
    } catch(IOException e ) {
        e.printStackTrace();
    }

    return builder.toString();
}

You could know use this method to load both files into strings:

String mountainData = loadFile("Mountain.txt");
String rockData = loadFile("Rpcks.txt");

You could now split these Strings:

String[] mountainsTokens = mountainData.split("\\s+");
String[] rockTokens = rockData.split("\\s+");

After that you just need to create your mountains. Therefore you go through each element of your matrix (the size seems to be 4 here):

Mountain[][] zones = new Mountain[4][4];
for(int y = 0; y < 4; y++) {
    for(int x = 0; x < 4; x++) {
        mountains[y][x] = new Mountain(Integer.parseInt(mountainData[x + y * 4]), rockData[x + y * 4]);
    }
}

For that you have to convert something one-dimensional into something two dimensional (x+y*4) Furthermore, you have to convert the String into an int using Integer.parseInt(). You might have to surround with try-catch too. Btw, I would definetivly save the mountains in a two dimensional arry like above. This makes everything much easier (instead of zone00 you write zones[0][0]).

I hoped that helped.

Sign up to request clarification or add additional context in comments.

Comments

0

You simply open and read two files at a time.

Btw you have a bit of a problem in that the size of your input is hard coded. Instead of assuming that the data is a 4 x 4 array of input, you should add the rows and columns to the input file and read those first. This allows your program to handle any sized data.

This code has not been tested, caveat emptor.

   public Mountain[][] read( String mountains, String rocks ) throws IOException {
      Reader br1 = Files.newBufferedReader( new File( mountains ).toPath() );
      Reader br2 = Files.newBufferedReader( new File( rocks ).toPath() );
      Scanner scan1 = new Scanner( br1 );
      Scanner scan2 = new Scanner( br2 );

      final int rows = 4;  // shouldn't hard code these
      final int columns = 4;

      Mountain[][] mountainArr  = new Mountain[rows][columns];

      for( int i = 0; i < rows; i++ ) {
         for( int j = 0; j < columns; j++ ) {
            int mountainHeight = scan1.nextInt();
            String rock = scan2.next();
            mountainArr[i][j] = new Mountain( mountainHeight, rock );
         }

      }
      scan1.close();
      scan2.close();
      return mountainArr;
   }

   public static class Mountain {
      public Mountain( int height, String rockType ) {
      }
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.