Input formatting

Asked

Viewed 94 times

0

I am solving a paper on the problem of transportation. Only that all my entries are delimited by a ENTER I would very much like to

1.read two integers (separated by space)

2.Give a ENTER

3.Read an nxm matrix formatted in the correct way;

I would also like to do this without using the Scanner class;

The code below:

package problemadotransporte;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Scanner;

    public class ProblemaDoTransporte {
        public static class Cedula{
            int noroesteDaCedula;
            int valorDaCedula;
            boolean boleano;
            public void setValues()
            {
                Scanner entrada;
                entrada = new Scanner(System.in);
                this.noroesteDaCedula=entrada.nextInt();            
            }


        }


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

         BufferedReader entrada = new BufferedReader(new InputStreamReader(System.in));
         int n,m;
         n=Integer.parseInt(entrada.readLine());
         m=Integer.parseInt(entrada.readLine());
         System.out.printf("%d %d",n,m);
         Cedula[][] tableau = new Cedula[n][m];
         for(int i=0;i<n;i++)for(int j = 0;j<m;j++)tableau[i][j]=new Cedula();
         for(int i=0;i<n;i++){for(int j = 0;j<m;j++)tableau[i][j].setValues(); System.out.print("\n");}

        }

    }

The entrance you’re making:

2
2
2 21 1 
1 1 

1 1 
1 1 

The entrance that the teacher wants:

3 3
16 20 200
14 8 160
180 120 0

Got it? Something like:

inteiro inteiro
matriz de inteiroxinteiro

NOTE: I didn’t know how to assign a proper tag to this question, because they removed the JAVA TAG.

1 answer

0

I found your question a little confusing but I’ll try to help you with the first part, to "read two integers (separated by space)" I modified some of your code below:

BufferedReader entrada = new BufferedReader(new InputStreamReader(System.in));
    int n,m;
    String numeros[] = entrada.readLine().split(" ");
    n = new Integer (numeros[0]);
    m = new Integer (numeros[1]);
    System.out.printf("%d %d",n,m);

Instead of converting direct to int like you did I left in String And I split it, see if it helps you. With this you can take two integers separated by space.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.