Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Saving from csv file and copying contents to a 2D array problems

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Saving from csv file and copying contents to a 2D array problems

    So I think I am close; however, I am getting some weird output when I print the contents of my array. So the input file is just a text file not an actual csv file but here is what the contents of the input file look like:

    A,B,C,D,E,F
    ∞,1,∞,∞,∞,∞
    ∞,5,∞,1,11,∞
    ∞,∞,∞,∞,∞,∞
    5,∞,∞,∞,3,∞
    ∞,∞,∞,11,∞,7
    ∞,∞,3,∞,∞,∞

    Pay no attention to case 2 and 3. Also, I haven't actually built any of the algorithms I just want to build them from the contents of my 2D array but first i need to make sure those contents are correct. So here is my code:

    public class GraphAlgorithms {
     
        public static void main(String[] args) throws IOException {
     
            GraphAlgorithms newAlgorithm = new GraphAlgorithms();
     
            String inputFile = "src/input/input.txt";
     
            String[][] readCSV = newAlgorithm.readCSV(inputFile);
     
            Scanner sc = new Scanner(System.in);
     
            System.out.println("Please select the algorithm you wish to run: "
                    + "1 for Prim's algorithm, 2 for Kruskal's algorithm"
                    + "or 3 for Floyd-Warshall’s algorithm ");
     
            while (sc.hasNext()){
     
                int i = sc.nextInt();
     
                switch(i){
     
                    case 1:  
     
                        System.out.println("You chose Prim's algorithm.");
     
                        primsAlgorithm(readCSV);
     
                        break;
     
                    case 2:
     
                        System.out.println("You chose Kruskal's algorithm.");
                        break;
     
                    case 3:
     
                        System.out.println("You chose Floyd-Warshall's algorithm.");
                        break;
     
                    default:
     
                        System.out.println("Invalid entry");
                        break;
                }
     
                System.out.println();
                System.out.println("Please select the algorithm you wish to run: "
                    + "1 for Prim's algorithm, 2 for Kruskal's algorithm"
                    + "or 3 for Floyd-Warshall’s algorithm ");
            }
     
        }
     
        public static String[][] readCSV(String path) throws FileNotFoundException, IOException {
     
            String fName = path;
                String thisLine;
                FileInputStream fis = new FileInputStream(fName);
                DataInputStream myInput = new DataInputStream(fis);
     
                List<String[]> lines = new ArrayList<>();
     
                while ((thisLine = myInput.readLine()) != null) {
                     lines.add(thisLine.split(","));
                }
     
                // convert our list to a String array.
                String[][] array = new String[lines.size()][0];
                return lines.toArray(array);
     
        }
     
        public static void primsAlgorithm (String [][] array){
     
            for (String[] strings : array) {
     
                for (String string : strings) {
     
                    System.out.print("\t" + string);
                }
     
                System.out.println();
            }
        }
    }

    So the odd part is this is what is being printed when I select 1 as my first option in order to print the array contents:

    output.jpg

    So the only part of the output that is not expected for me is the two characters printed prior to A. Why is it printing those?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Saving from csv file and copying contents to a 2D array problems

    the two characters printed prior to A.
    How was the input file created? Writing a file as Unicode adds 2 hex characters at the start of a file: ff fe
    What program are you viewing the contents of the input file with?
    Look at the file with a hex editor to see what the first two bytes are.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Saving from csv file and copying contents to a 2D array problems

    Quote Originally Posted by Norm View Post
    How was the input file created? Writing a file as Unicode adds 2 hex characters at the start of a file: ff fe
    What program are you viewing the contents of the input file with?
    Look at the file with a hex editor to see what the first two bytes are.
    Okay yeah that makes sense. As you know the input file is:

    A,B,C,D,E,F
    ∞,1,∞,∞,∞,∞
    ∞,5,∞,1,11,∞
    ∞,∞,∞,∞,∞,∞
    5,∞,∞,∞,3,∞
    ∞,∞,∞,11,∞,7
    ∞,∞,3,∞,∞,∞

    However, when I tried saving the notepad file it told me I couldn't save it with the ∞ unless I saved it as Unicode. Should I attempt to find an ANSI ∞ symbol or is there a way to let Java know the incoming file is Unicode so it doesn't insert those two characters?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Saving from csv file and copying contents to a 2D array problems

    way to let Java know the incoming file is Unicode
    Yes. Several of the io package classes take a charset selector in its constructor. For example: InputStreamReader
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    vysero (April 4th, 2018)

  6. #5
    Member
    Join Date
    Sep 2012
    Posts
    56
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Saving from csv file and copying contents to a 2D array problems

    Quote Originally Posted by Norm View Post
    Yes. Several of the io package classes take a charset selector in its constructor. For example: InputStreamReader
    Alright great I figured it out. Thanks for your help Norm I appreciate that!

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Saving from csv file and copying contents to a 2D array problems

    I'm glad you got it working.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Copying a file to an array and getting exception thrown for input
    By KristopherP in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 1st, 2013, 12:07 AM
  2. Replies: 1
    Last Post: April 26th, 2013, 02:38 AM
  3. csv or text file to Multiple csv or text file
    By robert1994 in forum What's Wrong With My Code?
    Replies: 17
    Last Post: April 19th, 2013, 01:35 PM
  4. Using bufferedReader to write contents of text file into an object array.
    By aznenginerd in forum What's Wrong With My Code?
    Replies: 9
    Last Post: May 8th, 2012, 02:13 PM
  5. Copying Array Problems.. Not what you think
    By xXRedneckXx in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 5th, 2011, 12:01 PM