Hey i have to create a 2D array of rows and columns which will takes a row and a columns as a paremeter and enters a string into that positions.
I managed to create the array but i cant find a way of getting from the user input.

public class Grid
{
    // instance variables - replace the example below with your own
    private int rows;
    private int columns;
    

       public Grid(int columns, int rows)
    {
        
        double[][] Grid = new double [columns][rows];
    }

   
    public void storeString(String c, String r)
    {
        for (int i = 0; i < columns; i++){
            
            
            for (int j = 0; j< rows.length; j++){
               
        }
    }


    }
    
}

You have two ways of doing that actually, do you want the user to give it at the startup of the program or at runtime?
I'll assume that it'll be at runtime. You can use a Scanner witch reads from standard input to get the strings. Search in the API for specific details on how to attach that standard input stream to that scanner.

If you still have questions, just post them here.

Black Box

I still cant solve the problem :-/
I dunno what im doing wrong

thanks in advance

import java.util.Scanner;
public class Grid
{
    static Scanner sc = new Scanner(System.in);
    private int rows;
    private int columns;
    

       public Grid(int columns, int rows)
    {
        
        double[][] Grid = new double [columns][rows];
    }

   
    public void storeString(String c, String r)
    {
       for (int i = 1; i<= columns; columns++)
       {
           for (int j = 1; j<= rows; rows++)
           {
               System.out.print([c][r]);
               System.out.print("\t");
               
        }
        System.out.println();
        
    }

First of all, the constructor isn't right. This is the place where you should initialize your private variables rows and colums. The 2D grid array (which, considering it's just a variable, shouldn't be capitalized) is declared inside the constructor, therefore its scope is limited to that constructor... meaning you will not be able to access it from the rest of your application. So, you should make that grid into a private variable too.

Second, I'm not so sure what it is that you're trying to do in the method storeString when you receive two strings.

Third, there should be a main method (somewhere) that asks for input and scans it using your sc.

Black Box

Actually im trying to create a kind of loop that will prompt the user to enter the appropriate number of strings in the grid according to the rows and columns number .. that's what i cant figure out.

Member Avatar for iamthwee

Why can't you just prompt for user input within the for loop?

that's what i don't know how to do it.

thanks.

Read the API for Scanner . It shows basic usage right there in the javadocs. Try it out and repost your code if you are still having difficulties getting it to work.

i still cant find an appropriate solution
thanks in advance...

import java.util.Scanner;
public class grid
{
    
    private int rows;
    private int columns;
    //private grid grid;
    static Scanner sc = new Scanner(System.in);
    private int row;
    private int colu;
    
    public void RowColumns(int rows, int columns)
    {
        double[][] RowColumns = new double[rows][columns];
    }
    //public void write(String r, String c)
    //{
      //for (int i=0; i<= columns; i++){
    //}
    public void main(String args)
    {
        for (int i =1; i<= columns; i++){
           String inputString = sc.nextLine();
           if (i==columns){           System.out.println(inputString);
        }
    }
        for (int j = 1; j<=rows; j++){
            String inputString = sc.nextLine();
            if (j==rows){
            System.out.println(inputString);
        }
        }
        
}
}

//AL SALAMO 3alikom
Look
u can get the input u want from the console..and this have its method to read line line from the console and then handle this string...
there are another Method to get input
From file what ever the kind of it whether it ASCII ot xml or any kind ..

Easy way to get Strings

is to use JOptionPane _import Swing in ur code

put this line in the second loop and u will input in each time the value u want
String string =JOptionPane.showInputDialog("input ur String");
// then u must parse the string into double to input into ur grid
grid [j]=Double.parseDouble(string);
// I hope this is usefull to u

// U WOULD USE "2 for loops " to fill the array
// if u can try this tell me ,,,to tell u how to read from console i think it is more hard to ufrom this way

Zizo

create a object ab using InputBufferedReader method.then prompt the user to give an input.
Grid[j]=ab.read();
read() reads a character by character from the input.
this may be help to you.

create a object ab using InputBufferedReader method.
....
read() reads a character by character from the input.

If you mean BufferedInputStream then no. It reads byte by byte, which is not necessarily the same thing.

If you mean BufferedReader, ok.

But there is no such thing as an InputBufferedReader (which would have to be a class and not method BTW).

i thought of doing it in that way but it still doesnt works.
Anyone that can help me???
Thanks.

import java.util.Scanner;
import java.util.*;
public class grid
{
    
    private int rows;
    private int columns;
    private ArrayList<String> myArray;
    
    //static Scanner sc = new Scanner(System.in);
    private int row;
    private int colu;
    
    public void RowColumns(int rows, int columns)
    {
        ArrayList[][] myArray = new ArrayList[rows][columns];
    }
    //public void write(String r, String c)
    //{
      //for (int i=0; i<= columns; i++){
    //}
    public void addString (int row, int column, String myString) {
        for (int i=0; (i<columns);i++);{
            for (int j=0; j<rows;j++);{
                myArray[columns][rows]= Console.readLine();
            }
        }
        //myArray[rows][columns] = myString;
    }  
}

i thought of doing it in that way but it still doesnt works.
Anyone that can help me???
Thanks.

import java.util.Scanner;
import java.util.*;
public class grid
{
    
    private int rows;
    private int columns;
    private ArrayList<String> myArray;
    
    //static Scanner sc = new Scanner(System.in);
    private int row;
    private int colu;
    
    public void RowColumns(int rows, int columns)
    {
        ArrayList[][] myArray = new ArrayList[rows][columns];
    }
    //public void write(String r, String c)
    //{
      //for (int i=0; i<= columns; i++){
    //}
    public void addString (int row, int column, String myString) {
        for (int i=0; (i<columns);i++);{
            for (int j=0; j<rows;j++);{
                myArray[columns][rows]= Console.readLine();
            }
        }
        //myArray[rows][columns] = myString;
    }  
}

Ok, you got closer on some parts and further away on others. First off, you need a array of String[][] - not ArrayList[][], which is an array of ArrayList objects. So change the array definition to be String[][].

You do need the Scanner to read the input, uncomment that.
If you need to read the input for a single row-column cell in your addString method, then you don't need to loop all of the array. You just need to read the single entry

System.out.println("Enter string:");
myArray[row][col] = sc.readLine();

and that is all. You don't need the myString parameter on the method, just the row and column. Whatever method is calling addString() is the one that needs to loop through all rows and columns, calling addString() for each position.
I think you need to step back a little and think about the steps before you write them. You seem to be just tossing pieces in here and there without fully considering whether they belong there.
If it helps, write little comment notes as place holders for each logical step in your methods before you code them. Then fill in the code below your outline notes.

thank you very much !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.