Creating a Nth size grid using an array - java

How would I create an Nth size array where all the values are the same such that it can form a grid. I'm pretty new to coding and just started with arrays.
This is my code so far.
import java.util.Scanner;
public class Array_design {
public static void main(String[] args) {
Scanner Row = new Scanner(System.in);
System.out.println("Enter length of row");
int row = Row.nextInt();
Scanner Col = new Scanner(System.in);
System.out.println("Enter length of column");
int col = Col.nextInt();
// TODO Auto-generated method stub
String[][] GameBoard = new String [row][col];
GameBoard[][] = for (int J = 1; J <= row; J = J + 1) {
for (int I = 1; I <= col; I = I + 1) {
System.out.print("*");
}
// if user input equal declared variable
System.out.println("");
}
}
}
}

You don't need 2 scanners, one will do. Also, if you're only going to store a single character on each space, use a char instead of a String. You should move this to a GameBoard class and create an init() and output() method.
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter length of row");
int row = scanner.nextInt();
System.out.println("Enter length of column");
int col = scanner.nextInt();
char[][] gameBoard = new char [row][col];
//Init the board
for (int i = 0; i < gameBoard.length; i++) {
Arrays.fill(gameBoard[i], '*');
}
//output the board
for (int i = 0; i < gameBoard.length; i++) {
System.out.println(Arrays.toString(gameBoard[i]));
}
}

What is going on with your code?
GameBoard[][] = for (int J = 1; J <= row; J = J + 1)
Anyway, the way I would do this is probably
public class Array_design
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in); //initialize Scanner from System.in
System.out.println("Enter length of row");
int row = input.nextInt(); //input row
System.out.println("Enter length of column");
int col = input.nextInt(); //input column
String[][] GameBoard = new String[row][col]; //initialize gameboard
for (String[] arr : GameBoard) for (String s : arr) //enhanced-for loops
s = "*"; //assign a value to every element in GameBoard; change this to whatever
}
}

Related

Input numeric ranges with java.util.Scanner

I would input numeric ranges (int arrays with two elements) like this:
Enter a number: 3
Enter a range: -3 5
Enter a range: 0 4
Enter a range: 6 10
I use java.util.Scanner, but I do not know how to proceed:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number = input.nextInt();
for(int i=0;i<number;i++)
String str = input.nextLine();
}
}
I think, you just nead a solution for input a numeric array. Check out this: Java Scanner Array
You can read the whole line, then split it into a String array:
public static void main(String[] args) {
try (var scanner = new Scanner(System.in)) {
System.out.print("Enter the count of arrays: ");
int arrayCount = scanner.nextInt();
scanner.nextLine();
// System.out.print("Enter the length of an array: ");
int arraySize = 2;
int[][] ranges = new int[arrayCount][arraySize];
for (int i = 0; i < arrayCount; i++) {
System.out.printf("[%d] Enter the elements: ", i);
String line = scanner.nextLine();
String[] numbers = line.split("\\D+", arraySize + 1); // separator: 1+ non-digit
for (int j = 0; j < arraySize; j++) {
ranges[i][j] = Integer.parseInt(numbers[j]);
}
}
// Arrays.stream(ranges).map(Arrays::toString).forEach(System.out::println);
}
}
Or simply scan ints one-by-one:
public static void main(String[] args) {
try (var scanner = new Scanner(System.in)) {
System.out.print("Enter the count of arrays: ");
int arrayCount = scanner.nextInt();
// System.out.print("Enter the length of an array: ");
int arraySize = 2;
int[][] ranges = new int[arrayCount][arraySize];
for (int i = 0; i < arrayCount; i++) {
System.out.printf("[%d] Enter the elements: ", i);
for (int j = 0; j < arraySize; j++) {
ranges[i][j] = scanner.nextInt();
}
}
// Arrays.stream(ranges).map(Arrays::toString).forEach(System.out::println);
}
}

I have to print the array , but for each loop is not working

import java.util.Scanner;
public class TeacherCoins {
public static void main(String[] args){
int k;
System.out.print("Enter total number of coins :");
Scanner sc = new Scanner(System.in);
k =sc.nextInt();
int [] arr= new int[k-1];
System.out.print("Enter array :");
for(int i=0;i<=(k-1);i++)
{
Scanner sc1 = new Scanner(System.in);
arr[i] =sc1.nextInt();
}
for(int element :arr)
{
System.out.println("Print array");
System.out.println(element);
}
}
I am giving output using Scanner class. But is not printing the array.
You don't need to declare the Scanner again inside the loop. Another thing that you should do to be sure of your code, is to have this condition on the loop if i < arr.length. Lastly, I moved the "Print array" message outside the last for.
public class TeacherCoins {
public static void main(String[] args){
int k;
System.out.print("Enter total number of coins :");
Scanner sc = new Scanner(System.in);
k =sc.nextInt();
int [] arr = new int[k];
System.out.print("Enter array :");
for(int i=0; i<arr.length; i++) {
arr[i] =sc1.nextInt();
}
System.out.println("Print array");
for(int element :arr){
System.out.println(element);
}
}
}
There are two problems with this code,
1. You don't need to create a new Scanner object for each user input.
2. You are declaring an array size of k-1 and then asking user input k times.
public static void main(String[] args) {
int k;
System.out.print("Enter total number of coins :");
Scanner sc = new Scanner(System.in);
k = sc.nextInt();
int[] arr = new int[k]; // To store k elements, you need k sized array
System.out.print("Enter array :");
for (int i = 0; i <= (k - 1); i++) {
// Scanner sc1 = new Scanner(System.in); / / Not required here
arr[i] = sc.nextInt();
}
System.out.println("Print array");
for (int element : arr) {
System.out.println(element);
}
}
There is only one problem in your code you are declaring the size of array as k-1 instead declare it for k elements.You just need to give each inputs in new line. For more refer this :Scanner class.
And your for each loop is correct and working.
import java.util.Scanner;
public class TeacherCoins {
public static void main(String[] args){
int k;
System.out.print("Enter total number of coins :");
Scanner sc = new Scanner(System.in);
k =sc.nextInt();
int [] arr= new int[k];//it was giving java.lang.ArrayIndexOutOfBoundsException
System.out.print("Enter array :");
for(int i=0;i<=(k-1);i++)
{
Scanner sc1 = new Scanner(System.in);//not required
arr[i] =sc1.nextInt();//use arr[i] =sc.nextInt();
}
for(int element :arr)
{
System.out.println("Print array");
System.out.println(element);
}
}
}
Above code will work. But you don't need new scanner objects for taking input, creating only single object will work.
import java.util.Scanner;
public class TeacherCoins {
public static void main(String[] args) {
int k;
System.out.print("Enter total number of coins :");
Scanner sc = new Scanner(System.in);
k = sc.nextInt();
int[] arr = new int[k];
System.out.print("Enter array :");
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
sc.close();
System.out.println("Print array");
for (int element : arr) {
System.out.println(element);
}
for (int i = 0; i < arr.length; i++) {
int count = 0;
for (int j = 0; i < arr.length; j++) {
arr[i] = arr[j];
count++;
}
System.out.println(arr[i] + " " + count);
}
}
}

Creating a loop for Scanner for arrays

I want to create a loop that asks for the Scanner to input each number one after the another for a certain amount in an array (I'm thinking of 10). Any suggestions?
import java.util.Scanner;
public class AssignSeven
{
public static void main(String[] args)
{
int [] array1 = new int[10];
System.out.println("Enter 10 numbers");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
for (int i = 0; i < 9; i++)
{
array1[i] = a;
}
}
}
change to
for (int i = 0; i < 9; i++)
{
int a = sc.nextInt();
array1[i] = a;
}
or even
for (int i = 0; i < 9; i++)
{
array1[i] = sc.nextInt();
}
It's simple, you can just assign the value of the scanner object's input to the indices of the array:
import java.util.Scanner;
public class AssignSeven
{
public static void main(String[] args)
{
int [] array1 = new int[10];
System.out.println("Enter 10 numbers");
Scanner sc = new Scanner(System.in);
// Where you had the original input
// int a = sc.nextInt();
for (int i = 0; i < 9; i++)
{
// Instead of array1[i] = a; you have
array1[i] = sc.nextInt();
}
}
}
Hope this helped!

Arrays - static method to return sum, but nothing is returned

Create a Java program that reads 10 numbers from the console Scanner
input = new Scanner(System.in); .Store the numbers as Floats in the array. Create static methods to perform the following actions on the array and returns the result.Add all the items in the array and return the result. Name this method"add".
So this is my code, but when the user inputs the 10 numbers, nothing is returned. Any suggestions?
public static void main(String[] args) {
float[] myArray = new float[10];
Scanner input = new Scanner(System.in);
for (int i = 0; i < myArray.length; i++) {
System.out.println("please enter number");
myArray[(i)] = input.nextFloat();
}
}
public static float add(float[] array) {
float sum = 0;
for (int i = 0; i < array.length; i++) {
sum += array[i];
}
return sum;
}
You are not calling the method :
public static void main(String[] args) {
float[] myArray = new float[10];
Scanner input = new Scanner(System.in);
for (int i = 0; i < myArray.length; i++) {
System.out.println("please enter number");
myArray[(i)] = input.nextFloat();
}
System.out.println(add(myArray)); // need to make this call
}
At the end of for loop you need to call the add method.
System.out.println(add(myArray));

First element of array is al;ways null - java

Whenever i put the first students name in when it displays them it always comes up as null however nothing else does this.Why?
package school.register;
import java.util.Scanner;
public class SchoolRegister {
public static void main(String[] args) {
Scanner Input = new Scanner(System.in);
String[][] Students = new String[5][2];
int i = 0;
int j = 0;
for (i = 0; i < 5; i++) {
System.out.println("Please put in a students name.");
Students[i][j] = Input.nextLine();
for (j = 0; j < 1; j++) {
System.out.println("Please put in a students test score.");
Students[i][j] = Input.nextLine();
}
}
System.out.println("Score\tName");
display(Students);
}
public static void display(String x[][]) {
for (int row = 0; row < x.length; row++) {
for (int column = 0; column < x[row].length; column++) {
System.out.println(x[row][column]);
}
}
}
}
try this:
public static void main(String[] args) {
Scanner Input = new Scanner(System.in);
String[][] Students = new String[5][2];
int i;
int l = 0;
for (i = 0; i < 5; i++) {
System.out.println("Please put in a students name.");
Students[i][0] = Input.nextLine();
System.out.println("Please put in a students test score.");
Students[i][1] = Input.nextLine();
}
System.out.println("Score\tName");
Display(Students);
}
System.out.println("Please put in a students name.");
Students[i][0] = Input.nextLine();
System.out.println("Please put in a students test score.");
Students[i][1] = Input.nextLine();

Categories

Resources