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: Taking a txt file in and making it a 2d int array

  1. #1
    Junior Member
    Join Date
    Nov 2020
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Taking a txt file in and making it a 2d int array

    So I am trying to take a txt file that is laid out like this

    651111111117
    200000000002
    200000000002
    200000000002
    200001000002
    200000000002
    200000000002
    200000000002
    200000000002
    200000000002
    200000000002
    911111111118

    (each 12 numbers are a new line)

    and pull it into a method in java as a 2d integer array with each digit as a new integer. I thought this would work for it but it gave me errors.

    Scanner scan = new Scanner(new File("Filename.txt"));
    int row=0, col=0;

    while (scan.hasNextLine() == true){
    while (scan.hasNextInt() == true ){
    Array[row][col] = scan.nextInt();
    col++;
    }
    row++;
    }

  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: Taking a txt file in and making it a 2d int array

    it gave me errors.
    Please copy the full text of the error message and paste it here. It has important info about the error.


    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 7
    Last Post: March 10th, 2014, 04:28 PM
  2. Help making a method inloving a 2d array and file IO
    By new2.java in forum Object Oriented Programming
    Replies: 1
    Last Post: February 12th, 2013, 08:48 PM
  3. Taking Input in String Array
    By syehjafa in forum Collections and Generics
    Replies: 8
    Last Post: July 25th, 2011, 07:18 AM
  4. taking information from one array into another
    By Stn in forum Object Oriented Programming
    Replies: 1
    Last Post: January 18th, 2011, 11:56 AM
  5. Taking an element out of an array
    By SlckP in forum Collections and Generics
    Replies: 3
    Last Post: May 5th, 2010, 02:26 PM