-1

3 /**/ 17 39 82 108 117 8 25 47 58 63 72 99

I have these numbers. They read into the field int [][]rag by the program below.

public class RaggedArray {


   /**/
      private int [][] rag;
      /**/
      public static void main ( String [] arg ) {
        System.out.println( new RaggedArray() );
        return;
      }
      /**/
      public RaggedArray() {
        initFromFile("rag.txt");
      }
      /**/
      public String toString () {
        /**/
        String lf;
        /*
         *  lf == line-feed character
         */
        lf=System.lineSeparator();
        /*
         *  Provide needed code here.
         */ 

      }
      /**/
      private void initFromFile ( String fileName ) {
        /**/
        FileInput fi;
        int numRows;
        /**/
        fi=new FileInput(fileName);
        numRows=fi.readLineInt();
        rag=new int [numRows][];
        fi.readLine();
        for ( int i=0; i<numRows; ++i ) rag[i]=fi.readLineInts();
        fi.close();
        /**/
        return;
      }
    }

This is what I have so far

rag=new int [4][] 
    for (int i=0; i<rag.length; ++i ) {

Each int is supposed to be right justified in a field of width four. I honestly only know how to do this if the numbers are included in the code. But the numbers are in a different file. Does anyone have any idea?

Nicole
  • 1
  • @Dropout My question is very basic method Java coding, kinda classroom like. The answer you provided is very advanced Java, so no it didn't answer it. Thank you tho! – Nicole May 18 '22 at 22:33
  • The comment is automatically generated by the system when someone marks your question as a duplicate. It's not me actually asking if it answers it, I know it does. If you scroll a bit lower there's a trivial way of solving it with a `Scanner` written on 4 lines. You could do the same with a `BufferedReader` and just call `BufferedReader::readLine` once. That's literally 2 lines of code. That's also in the answer by the way. – Dropout May 18 '22 at 22:50
  • 1
    Please [edit] your question to ask a specific question about what you are having trouble with. See [ask] for some tips. – Code-Apprentice May 18 '22 at 23:12
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Sumit Sharma May 19 '22 at 04:15

1 Answers1

0

You can read the file contents into an array of numbers with which you can then fill the two-dimensional array.

private int[] readNums(File file){
    
    try(BufferedReader br = new BufferedReader(file)){
        
        String line;
        if(line = br.readLine() != null){
            String split = line.split(" ");
            int[] ints = new int[split.length()];
            for(int i = 0; i<split.length(); i++){
                ints[i] = Integer.parseInt(split[i]);
            }
            return ints;
        }

    } catch(Exception e){
        //catch what's needed
    }

    return null;
}

Then work with the numbers as needed. For example if I understood correctly if you want to put them on the right-most side of a two-dimensional array with the width of 4, you can do

private int[][] fillArray(int[] input){
    
    int[][] intArray = new int[4][input.length()];
    for(int i=0; i<input.length(); i++){
        intArray[4][i] = input[i];
    }

    return intArray;
}

This could also have been written much simpler with the use of streams or modern libraries, but I've omitted it for the sake of simplicity. If you want let me know I can add it as well.

Dropout
  • 13,653
  • 10
  • 56
  • 109