Jump to content

Help - Read from a csv file and store data into an array [5][5] !

- - - - -

  • Please log in to reply
4 replies to this topic

#1
charlote

charlote

    Newbie

  • Members
  • PipPip
  • 29 posts
 Here is my program question : In this problem you are to write a program to explore the given array for a treasure. The values in the array are clues. Each cell contains an integer between 11 and 55; for each value the ten's digit represents the row number and the unit's digit represents the column number of the cell containing the next clue.
Starting in the upper left corner (at 1,1), use the clues to guide your search of the array. (The first three clues are 11, 34, 42).
i.e. Cell (1,1) contains 34, which means go to cell 3, 4
Cell(3,4) contains 42 which means go to cell 4,2…..
 
[B]The treasure is a cell whose value is the same as its coordinates.[/B]
 
Your program must first read in the treasure map data into a 5 by 5 array.
Your program should output the cells it visits during its search, and a message indicating where you found the treasure.


You will need to use arrays, methods, and objects.

import java.io.*;
 
public class TreasureHunt {
    public static void main(String[] args) {
        String[] lines = new String[0];
        String path = "map01.csv";
        BufferedReader br = null;
        try {
            File file = new File(path);
            br = new BufferedReader(
                 new InputStreamReader(
                 new FileInputStream(file)));
            String line;
            while( (line = br.readLine()) != null ) {
                lines = add(line, lines);
            }
            br.close();
        } catch(IOException e) {
            System.out.println("read error: " + e.getMessage());
        }
        print(lines);
    }
 
    private static String[] add(String s, String[] array) {
        int len = array.length;
        String[] temp = new String[len+1];
        System.arraycopy(array, 0, temp, 0, len);
        temp[len] = s;
        return temp;
    }
 
    private static void print(String[] data) {
        for(int i = 0; i < data.length; i++)
            System.out.println(data[i]);
    }
}



To go to the next cell, I know I will have to use row = num/10 and column = num%10; If I do this: clue = array[row=num/10][row%10], will it work? Is this the right way to do it? Please help!

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
2 ways for reading stuff from a csv into array:

public static void main(String[] args) throws FileNotFoundException {

        int[][] matrix = new int[5][5];

        

        Scanner scanner = new Scanner(new File("src/main/java/input.csv"));

        scanner.useDelimiter(Pattern.compile(",|\r\n"));

        for(int i=0 ; i<matrix.length ; i++){

            for(int j=0 ; j<matrix[i].length ; j++) {

                matrix[i][j] = scanner.nextInt();

            }

        }

        

        printMatrix(matrix);

        System.out.println("\n-----------------\n");

        

        Scanner fileScanner = new Scanner(new File("src/main/java/input.csv"));

        for(int i=0 ; i<matrix.length ; i++){

            Scanner lineScanner = new Scanner(fileScanner.nextLine()).useDelimiter(",");

            for(int j=0 ; j<matrix[i].length ; j++) {

                matrix[i][j] = lineScanner.nextInt();

            }

        }

        printMatrix(matrix);


    }


    private static void printMatrix(int[][] matrix) {

        for(int i=0 ; i<matrix.length ; i++){

            for(int j=0 ; j<matrix[i].length ; j++) {

                System.out.printf("%2d ", matrix[i][j]);

            }

            System.out.println();

        }

    }

The first way uses a regex to split the data in the file: ",|\r\n", which means comma OR [ENTER] .

The second way does not use a regex, but will just split on commas. Problem are the newlines, but this can be avoided by using an extra scanner to read just lines. And the 2nd one will split it on commas.

#3
charlote

charlote

    Newbie

  • Members
  • PipPip
  • 29 posts
Thanks. I compiled the code, and it is giving me 11 errors. All are "Scanner cannot be resolved to a type; FilenotFoundException cannot be resolved to a type; File cannot be resolved to a type; etc. I do get this a lot. Can you explain why this happens?

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Cause you don't have this bunch of imports on top :

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.regex.Pattern;


#5
charlote

charlote

    Newbie

  • Members
  • PipPip
  • 29 posts

wim DC said:

Cause you don't have this bunch of imports on top :

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.regex.Pattern;

Thanks, wim DC, It is working now. Now I will have to work on the second part of the program.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users