First element of array is al;ways null - java - 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();

Related

Using while loop on a user specified condition

So, this below is my code:
public class StudentRun {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] names = new String[50];
int[] rolls = new int[50];
System.out.print("Do you want to register a student ?(yes/no): ");
String res = sc.nextLine();
while((res.toUpperCase()).equals("YES")) {
System.out.print("Enter the student's name: ");
String n = sc.nextLine();
for(int i=1; i<50; i++) {
names[i] = n;
}
System.out.print("Enter their roll number: ");
int r = sc.nextInt();
for(int j=0; j<50; j++) {
rolls[j] = r;
}
}
for(int a=0; a<50; a++) {
System.out.println(names[a]);
System.out.println(rolls[a]);
}
}
}
What I want is to happen is that, the program should keep registering students name and roll no. until the array is full or the user ends it. How do I do it ? I got that far
You need to have the "continue" question in the while loop, and you don't need the for loop every time you insert a name.
public class StudentRun {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] names = new String[50];
int[] rolls = new int[50];
int index = 0;
while(true) {
System.out.print("Do you want to register a student ?(yes/no): ");
String res = sc.nextLine();
if(res.toUpperCase().equals("NO") || index == 50)
break;
System.out.print("Enter the student's name: ");
String n = sc.nextLine();
names[index] = n;
System.out.print("Enter their roll number: ");
int r = sc.nextInt();
rolls[index] = r;
index++;
}
for(int i=0; i<50; i++) {
System.out.println(names[i]);
System.out.println(rolls[i]);
}
}
}
A common approach when using fixed sized arrays is to use a separate int variable to track the current index position for a new item, as well as the total used slots in the array:
import java.util.*;
class Main {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int size = 50;
String[] names = new String[size];
int[] rolls = new int[size];
int counter = 0;
String res = "";
do {
System.out.print("Do you want to register a student ?(yes/no): ");
res = sc.nextLine().toUpperCase();
if (res.equals("YES")) {
System.out.print("Enter the student's name: ");
names[counter] = sc.nextLine();
System.out.print("Enter their roll number: ");
rolls[counter] = sc.nextInt();
sc.nextLine(); // clear enter out of buffer;
counter++;
}
} while (counter < size && res.equals("YES"));
for(int a=0; a<counter; a++) {
System.out.print(names[a] + " : ");
System.out.println(rolls[a]);
}
}
}

Creating a Nth size grid using an array

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
}
}

Java Array 2D not working with parameters

I'm new in the community and I need help with Array 2d in Java
Is a project for school
this is my problem
I build Array 2D with static length and work but the same code with parameters not work.
First print the System.out.print("Insert Name");
after that not execute the statement matrix[i][0] = input.nextLine();
third print System.out.print("Insert Last Name");
now works but the index [0],[0] is empty
Example of print:
a
b b
c c
Thanks!!!
import java.util.*;
public class Students {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("Insert number of Students");
int numStudents = input.nextInt();
String[][] matrix = new String[numStudents][2];
for (int i = 0; i < numStudents; i++) {
System.out.print("Insert Name");
matrix[i][0] = input.nextLine();
for (int j = 1; j < 2; j++) {
System.out.print("Insert Last Name");
matrix[i][j] = input.nextLine();
}
}
for(int z=0; z<numStudents ;z++) {
System.out.println();
for(int h=0; h<2;h++) {
System.out.printf(matrix[z][h]);
System.out.printf(" ");
}
}
}
}
Use String value= input.next(); instead of input.nextLine();
or
use an extra input.nextLine(); after input.nextInt();
i.e.
int numStudents = input.nextInt();
input.nextLine()
This happens because input.nextInt() just reads one integer and does not finishes the line.
I think this should work for you.
No need of a nested for loop to read last name.
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("Insert number of Students");
int numStudents = input.nextInt();
input.nextLine();
String[][] matrix = new String[numStudents][2];
for (int i = 0; i < numStudents; i++) {
System.out.println("Insert Name");
matrix[i][0] = input.nextLine();
System.out.println("Insert Last Name");
matrix[i][1] = input.nextLine();
}
for (int z = 0; z < numStudents; z++) {
System.out.println();
for (int h = 0; h < 2; h++) {
System.out.print(matrix[z][h]);
System.out.print(" ");
}
System.out.println();
}
}

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!

Java Compile Error, cannot find symbol

Getting the error "Cannot Fin Symbol", but I don't know what I am doing wrong.
import java.util.Scanner;
public class Exercise6_1{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter number of students: ");
int numberStudents = input.nextInt();
int[] studentScores = new int[numberStudents];
System.out.print("Enter " + numberStudents + " Scores: ");
for (int i = 0; i < numberStudents; i++);{
studentScores[i] = input.nextInt();
}
}
}
You have semicolon after the "for" cycle.
Should look like this:
for (int i = 0; i < numberStudents; i++) {
studentScores[i] = input.nextInt();
}
you have an ; after the for loop.
Correct impl :-
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter number of students: ");
int numberStudents = input.nextInt();
int[] studentScores = new int[numberStudents];
System.out.print("Enter " + numberStudents + " Scores: ");
for (int i = 0; i < numberStudents; i++)
{
studentScores[i] = input.nextInt();
}
}
}
The last semicolon in the line
for (int i = 0; i < numberStudents; i++);{
should be removed:
for (int i = 0; i < numberStudents; i++) {
for (int i = 0; i < numberStudents; i++);{
studentScores[i] = input.nextInt();
}
Here You have ended the for loop with a semi-colon , which results in the termination of the loop at that point . That's why it shows it can't find symbol i , As it is out of the scope of the for loop.

Categories

Resources