Having trouble taking user inputs and forming arrays - java

I need to take user inputs and and post them to a two dimensional array. This is what I have so far:
System.out.print("How many students? ");
Scanner scan = new Scanner(System.in);
int input = scan.nextInt();
int col = input;
int row = 5;
String[][] y = new String[col][row];
for (col = 0; col < y.length; col++) {
for(row = 0; row < y[col].length; row++){
int n = col + 1;
System.out.println("Enter the name and grades of student " + n);
y[col][row] = scan.next();
}System.out.println();
}
for(row = 0; row< y.length; row++){
for(col = 0 ;col< y[row].length; col++){
System.out.println(y[row][col]);
}
System.out.println();
}
The only problem is that it asks the same question 5 times over before moving on to the next student. Do I stay on this same path? Or would it be easier to scrap it and go at it with a different approach?

this loop of yours goes trough each student,
for (col = 0; col < y.length; col++)
and this loop of yours goes trough each note and asks to input them,
for(row = 0; row < y[col].length; row++)
so, it's logical that you want to ask the user only one time to input the note, saying that you only need to ask it each time you iterate a student in the array, simply change this:
for (col = 0; col < y.length; col++) {
for(row = 0; row < y[col].length; row++){
int n = col + 1;
System.out.println("Enter the name and grades of student " + n); //you are asking each time you iterate a note
y[col][row] = scan.next();
}System.out.println();
to this:
for (col = 0; col < y.length; col++) {
System.out.println("Enter the name and grades of student " + col); //you are asking each time you iterate a student
for(row = 0; row < y[col].length; row++){
int n = col + 1;
y[col][row] = scan.next();
}System.out.println();
}

What you could do is that you can ask all the details for a particular student and then proceed for the next student rather than asking the same question for each student.

Try this:
System.out.print("How many students? ");
Scanner scan = new Scanner(System.in);
int input = scan.nextInt();
int col = input;
int row = 5;
String[][] y = new String[col][row];
for (col = 0; col < y.length; col++) {
System.out.println("Enter the name and grades of student " + col);
for(row = 0; row < y[col].length; row++){
int n = col + 1;
y[col][row] = scan.next();
}System.out.println();
}
for(row = 0; row< y.length; row++){
for(col = 0 ;col< y[row].length; col++){
System.out.println(y[row][col]);
}
System.out.println();
}
This will ask to enter the details once per student.

if you like to accept name and 4 grades input then this is your
code,else elaborate your problem
for (col = 0; col < y.length; col++) {
>
> int n = col + 1;
>
> System.out.println("Enter the name and grades of student " + n);
>
> for(row = 0; row < y[col].length; row++){
>
> y[col][row] = scan.next();
> }System.out.println();
> }

Related

Can someone tell me how to pass this array into my main method so it prints?

I am trying to pass my array and it does not print out the sumAllColumnsRows and I have searched alot of sites and I am not sure how to print what the code is doing. being passed from the method below.
public static void main(String[] args){
Scanner input= new Scanner(System.in);
System.out.print("Enter dimension of an nxn matrix ");
int x=input.nextInt();
double[][] nxn=new double[x][x];
for(int i=0;i<x;i++) {
System.out.print("Enter row " + (i) + ": ");
for(int j=0;j<x;j++){
nxn[i][j]=input.nextDouble();
}
}
System.out.println(sumAllColumnsRows(m, column, rowColumn));
}
public static double sumAllColumnsRows(double[][] m, boolean column, int rowColumn)
{
double total=0;
for (int col = 0; col < m[0].length; col++) {
int colSum = 0;
for (int row = 0; row < m.length; row++) {
colSum += m[row][col];
}
System.out.println("Sum of the elements at col " + col + " is: " + colSum);
}
for (int row = 0; row < m.length; row++) {
int rowSum = 0;
for (int col = 0; col < m[row].length; col++) {
rowSum += m[row][col];
}
System.out.println("Sum of the elements at row " + row + " is: " + rowSum);
}
return total;
}
Okay, your problem is probably identified here, but I think you might have other problems. First:
double[][] nxn=new double[x][x];
...
System.out.println(sumAllColumnsRows(m, column, rowColumn));
You are defining your variable non but passing m. You also have no variables column and rowColumn. I imagine this code doesn't compile.
Note, however, that your sumAllColumnsRows doesn't actually use those second two arguments.
The error messages from javac should have helped figure this out.
You are not adding to the total variable in the sumAllColumnsRows. It will always return 0.
public static double sumAllColumnsRows(double[][] m, boolean column, int rowColumn) {
double total = 0;
for (int col = 0; col < m[0].length; col++) {
int colSum = 0;
for (int row = 0; row < m.length; row++) {
colSum += m[row][col];
}
total += colSum;
System.out.println("Sum of the elements at col " + col + " is: " + colSum);
}
for (int row = 0; row < m.length; row++) {
int rowSum = 0;
for (int col = 0; col < m[row].length; col++) {
rowSum += m[row][col];
}
total += rowSum;
System.out.println("Sum of the elements at row " + row + " is: " + rowSum);
}
return total;
}

Trying to input values into a 2d array and print it, code not running

Code not running??
import java.util.Scanner;
public class Array2dNightPractice
{
int[][] studentmarks;
studentmarks = new int[3][3];
Scanner kb = new Scanner(System.in);
System.out.println("Enter 9 integers");
for(int row = 0;row<3;row++){
for(int col=0;col<3;col++){
studentmarks[row][col] = kb.nextInt();
}
}
for(int row = 0; row < 3; row++) {
for(int col = 0; col < 4; col++) {
System.out.print(studentmarks[row][col] + " ");
}
System.out.println();
}
}
you're getting IndexOutOfBoundsException because of this --> col < 4.
for(int col = 0; col < 4; col++)
change to this:
for(int col = 0; col < studentmarks[row].length; col++)
side note - make use of the length property to prevent errors such as the one you've just encountered.
full solution:
for(int row = 0; row < studentmarks.length; row++) {
for(int col = 0; col < studentmarks[row].length; col++) {
System.out.print(studentmarks[row][col] + " ");
}
System.out.println();
}
the error comes from exceeding the bound of the array studentMarks in the second for loops at the number 4 it should be 3
A good habit to develop is to create constant variables as required with the size of each of the rows and columns, so you will not fall in such mistake.
Something like this:
import java.util.Scanner;
public class Array2dNightPractice{
public static void main(String[] agrs){
final int ROW =3, COL=3; // constant variable to determine the size of the 2d array
int[][] studentMarks = new int[ROW][COL];
Scanner in = new Scanner(System.in);
System.out.println("Enter 9 integers");
for(int row = 0;row<ROW;row++){ //note how I don't need to memorize and rewrite the numbers every time
for(int col=0;col<COL;col++){
studentMarks[row][col] = in.nextInt();
}
}
// print the marks as matrix
for(int row =0; row <ROW; row++) {
for(int col =0; col <COL; col++) {
System.out.print(studentMarks[row][col] + " ");
}
System.out.println();
}
}
}

Can not figure out how to increment a two dimensional array

So, basically what I want to do with my code is make it so that a user can enter in a value, and then the program will add whatever value that would be to ALL the values in the array
//my code
{
//creates array called table
int[][] table = new int [10][10];
//load the table with values
for (int row=0; row < table.length; row++)
for (int col=0; col < table[row].length; col++)
table[row][col] = row * 10 + col;
//Print the table
for (int row=0; row < table.length; row++)
{
for (int col=0; col < table[row].length; col++)
System.out.print (table[row][col] + "\t");
System.out.println();
int incr;
Scanner scan = new Scanner(System.in);
System.out.println("");
System.out.println("What do you want to increment by?");
incr = scan.nextInt();
for (int row=0; row < table.length; row++)
{
for (int col=0; col < table[row].length; col++)
System.out.print (table[row][col] + "\t");
System.out.println();
}
}
That is my code so far, and I'm not sure where to go from here. If someone could help me figure this out, that would be great.
inside the loop add this line of code:
table[row][col] += incr;

Display lottery winners

I created a lottery program to randomly generate 3 numbers between 0-9 and then randomly generate the 3 winning numbers. I need help on how to make the program display the winners (if there are some) and display the number of how many won the lottery.
So something like:
Winners:
Person1
person5
Number of winners: 2
Here is my program
import java.util.Random;
public class TwoDArray
{
public static void main(String[] args)
{
int[][] table = new int[50][3];
int[][] win = new int[1][3];
Random rand = new Random();
int i = 1;
// Load the table with values
for (int row=0; row < table.length; row++)
for (int col=0; col < table[row].length; col++)
table[row][col] = rand.nextInt(7-0 +1)+0 + col;
// Load the winning Values
for (int row=0; row < win.length; row++)
for(int col=0; col < win[row].length; col++)
win[row][col] = rand.nextInt(7-0 +1)+0 + col;
// Print the table of People
for (int row=0; row < table.length; row++)
{
System.out.print("Person" + i++ +":\t");
for (int col=0; col < table[row].length; col++)
System.out.print(table[row][col] + "\t");
System.out.println();
}
//Print the Winning Numbers
for (int row=0; row < win.length; row++)
{
System.out.print("\nThe winning numbers are:\t");
for(int col=0; col < win[row].length; col++)
System.out.print(win[row][col] + "\t");
System.out.println();
}
}
}
You want another for loop. Something like:
int counter = 0;
for (int i =0; i < table.length; i++){
if (table[i][0] == win[0][0] && table[i][1] == win[0][1] && table[i][2] == win[0][2])
{
counter++;
System.out.println("Person " + i);
}
}
System.out.println("There were " + counter + " winners.");

Arrays and 'null' can I remove it and will it affect a random number generator

I'm just wondering is there a way to make the 'null' that is outputted when using a for loop output white space?
For example, I have a 2d array of an type object and when displaying the array I want to display the first three elements and then skip say 10 empty spaces then print another three elements.
Also if I was to use a random generator to pick array elements at random would the empty elements affect this or not?
Main:
public class CinemaSystem {
public static void main(String[] args) {
Seat cinemaactual = new Seat("Cinema");
Seat[][] cinema = new Seat[12][23];
Ticket ticket = new Ticket();
Scanner scan = new Scanner(System.in);
String answer, contiune;
int number, check = 0, category, id;
do {
System.out.print("Welcome to the Theatre Booking System. (QUIT to exit)"
+ "\nWould you like to purchase tickets or list available seats?"
+ "(/Purchase/List/Help)");
answer = scan.nextLine();
if (answer.equalsIgnoreCase("purchase")) {
do {
System.out.println("Please choose the cateogry of ticket "
+ "you would like, followed by who the ticket is for"
+ "and the amount required. (separated by a space)\n"
+ "1. Gold\n2. Silver\n3. Bronze\n\n1. Adult\n2."
+ " Child\n3. Concession");
category = scan.nextInt();
number = scan.nextInt();
id = scan.nextInt();
if (category == 1 || category == 2 || category == 3 && id == 1 || id == 2 || id == 3) {
ticket.SetType(category);
if (category == 1) {
ticket.SetName("Gold");
} else if (category == 2) {
ticket.SetName("Siler");
} else {
ticket.SetName("Bronze");
}
ticket.SetNumber(number);
ticket.SetID(id);
if (id == 1) {
ticket.SetCategory("Adult");
} else if (id == 2) {
ticket.SetCategory("Child");
} else {
ticket.SetCategory("Bronze");
}
System.out.print("You have selected"
+ ticket.GetNumber() + " " + ticket.GetName()
+ " ticket(s) at the" + ticket.GetCategory() + " price .");
ticket.BuyTicket(category, id, number);
} else {
System.out.print("Sorry, incorrect input, please enter an apropriate value.");
check = scan.nextInt();
}
} while (check == 0 || check > 3);
do {
System.out.print("Would you like to perchase more tickets? (Yes/No)");
contiune = scan.nextLine();
} while (contiune.equalsIgnoreCase("Yes"));
} else if (answer.equalsIgnoreCase("list")) {
cinemaactual.CreateTheatre(cinema);
cinemaactual.DisplayTheatre(cinema);
} else if (answer.equalsIgnoreCase("help")) {
// Code for help
} else if (answer.equalsIgnoreCase("quit")) {
System.exit(-1);
}
System.out.print("Sorry, incorrect input please enter"
+ " a valid input (Purchase/List/Help or QUIT to exit");
answer = scan.nextLine();
} while (!answer.equalsIgnoreCase("purchase")
|| !answer.equalsIgnoreCase("List")
|| !answer.equalsIgnoreCase("help")
|| !answer.equalsIgnoreCase("quit"));
}
}
Method in the class that makes the array:
public Seat[][] CreateTheatre(Seat[][] x) {
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 4; col++) {
x[row][col] = new Seat("B");
}
}
for (int row = 8; row < 12; row++) {
for (int col = 0; col < 4; col++) {
x[row][col] = new Seat("S");
}
}
for (int row = 0; row < 8; row++) {
for (int col = 19; col < 23; col++) {
x[row][col] = new Seat("B");
}
}
for (int row = 8; row <12; row++){
for (int col = 19; col < 23; col++) {
x[row][col] = new Seat("S");
}
}
for (int row = 3; row < 5; row++) {
for (int col = 4; col < 9; col++) {
x[row][col] = new Seat("B");
}
}
for (int row = 3; row < 5;row++){
for (int col = 14; col < 19; col++) {
x[row][col] = new Seat("S");
}
}
for (int row = 9; row < 12; row++) {
for (int col = 7; col < 4; col++) {
x[row][col] = new Seat("S");
}
}
for (int row = 3; row < 5; row++) {
for (int col = 14; col < 20; col++) {
x[row][col] = new Seat("B");
}
}
for (int row = 5; row < 9; row++) {
for (int col = 4; col < 9; col++){
x[row][col] = new Seat("S");
}
}
for (int row = 5; row < 9; row++) {
for (int col = 14; col < 20; col++) {
x[row][col] = new Seat("S");
}
}
for (int row = 6; row < 9; row++) {
for (int col = 9; col < 14; col++) {
x[row][col] = new Seat("G");
}
}
for (int row = 9; row < 12; row++) {
for (int col = 7; col < 16; col++) {
x[row][col] = new Seat("G");
}
}
return x;
}
For the display of the array:
public void DisplayTheatre(Seat[][] x) {
for (int row = 0; row < x.length; row++) {
for (int col = 0; col < x[row].length; col++) {
System.out.print(x[row][col] + " ");
}
System.out.println();
}
}
Just wondering if I can remove the null since I want some objects display then blank space in the middle and then some more, on the same line.
Also do null elements interfere with randomly selecting elements from an array, like will a null element ever be randomly selected?
if i understand your requirement. below is something you are looking for .
String[] arr = new String[5];
arr[0]="2";
arr[1]="3";
arr[4]="5";
for(int i=0; i<arr.length; i++){
if(arr[i]==null){
System.out.println(' ');
}
else {
System.out.println(arr[i]);
}
}
Also if I was to use a random generator to pick array elements at random would the empty elements affect this or not?
If some of the elements were null, then picking an element at random would inevitably give you a null sometimes.
You could attempt to deal with this by trying again with a new random number ... until you get a non-null element. But that could be expensive if the array is sparse. And it could give you an infinite loop if all of the elements are null.
(But don't just search from the initial random position for a non-null element ... because that will give a biased selection.)
However a better solution would be to have a separate list containing just the unoccupied seats (or whatever).
And an ideal cinema booking system would not allocate seats at random. Rather, it would try to fill methodically ... so that Jim and his girlfriend have a good chance of sitting together even if they buy the last 2 tickets.

Categories

Resources