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 2 of 2

Thread: Create two dimensional array from a one dimensional array

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    26
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Create two dimensional array from a one dimensional array

    I am trying to add fractions from a file. I have managed to put each line into a string array.
    example file:
    3/2
    4/3
    5/6

    array: 3/2, 4/3, 5/6

    How can I split up these numbers so that a have a dimensional array that would be

    3 ,2
    4, 3
    5,6


    My code so far:

    import java.util.Scanner;
    import java.io.FileNotFoundException;
    import java.lang.String;
    import java.io.File;
    import java.io.FileInputStream;
     
    public class Assignment1{
     
        public static void main(String [] args){    
            //Reads file containing fractions
            Scanner inputFile = null;
            try {
                inputFile = new Scanner(new FileInputStream("fractions.txt"));
            }
            catch (FileNotFoundException e) {
                System.out.println("File not found or not opened.");
                System.exit(0);
            }
     
            String[]test = new String[100];
            String[][]fractions = new String [100][2];
            int count = 0;
     
            //
            //while(inputFile.hasNextLine()){
             //   count++;
            //    inputFile.nextLine();
            //}
     
            for(int i = 0; i<=31; i++){
               test[i]=inputFile.nextLine();
            }
     
            for(int i = 0; i<=31; i++){
                fractions[i][i+1]= test
     
            }
     
        }
     
     
     
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,571
    Thanks
    49
    Thanked 2,232 Times in 2,204 Posts

    Default Re: Create two dimensional array from a one dimensional array

    Is this the same problem: File reading issues
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Converting Two dimensional array into an Array list
    By NewbieJavaProgrammer in forum Object Oriented Programming
    Replies: 11
    Last Post: September 29th, 2012, 04:23 PM
  2. Help with looking through a two-dimensional array.
    By aesguitar in forum Algorithms & Recursion
    Replies: 14
    Last Post: June 1st, 2012, 11:10 AM
  3. Help with one-dimensional array
    By brandon66 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 26th, 2012, 04:59 PM
  4. How can I create a jigsaw puzzle with Array multi-dimensional?
    By ivan8 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: December 4th, 2011, 07:13 PM
  5. HELP: UNABLE TO CREATE AND PRINT A 3-DIMENSIONAL ARRAY
    By baraka.programmer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 3rd, 2011, 03:44 PM