Using while loop on a user specified condition - java

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]);
}
}
}

Related

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

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();
}
}

return array from one method to another method

This is my program and every time it is asking me to input the array.
I want to input a array once and process on that array.
But this program is asking me to input array again.
from method named "first" I just want to return array and use that array in two different methods add and delete. But it is always asking me to input all the elements of array that is every time the first method is running while i call any add or delete method from main method.
package Program;
import java.util.Arrays;
import java.util.Scanner;
public class Functionality {
public static int[] first( )
{
System.out.println("Enter the number of element in array");
Scanner num = new Scanner(System.in);
int data = num.nextInt();
//return data;
Scanner ar = new Scanner(System.in);
int arr[] = new int[data];
System.out.println("Enter "+data+" Numbers");
for(int i =0; i<data; i++){
System.out.println("Enter NUmber :"+(i+1));
arr[i] = num.nextInt();
}
System.out.println();
System.out.println(Arrays.toString(arr));
return arr;
}
public static void main(String[] args) {
add();
delete();
}
static void add(){
int arr[]=first();
System.out.println("Enter the number you want to add");
Scanner one = new Scanner(System.in);
int naya = one.nextInt();
for(int i = 0; i<=arr.length-1; i++){
arr[i]= arr[i] + naya;
}
System.out.println("The added array is");
System.out.println(Arrays.toString(arr));
}
static void delete(){
int arr[]=first();
System.out.println("Enter the number you want to substract");
Scanner two = new Scanner(System.in);
int arko = two.nextInt();
for(int i =0; i <= arr.length-1; i++ ){
arr[i]=arr[i]-arko;
}
System.out.println("The Substracted array is");
System.out.println(Arrays.toString(arr));
}
You should obtain reference to an array and passing it during subsequent methods invocations.
This should do the trick:
class Functionality {
static int[] first() {
System.out.println("Enter the number of element in array");
Scanner num = new Scanner(System.in);
int data = num.nextInt();
//return data;
Scanner ar = new Scanner(System.in);
int arr[] = new int[data];
System.out.println("Enter " + data + " Numbers");
for (int i = 0; i < data; i++) {
System.out.println("Enter NUmber :" + (i + 1));
arr[i] = num.nextInt();
}
System.out.println();
System.out.println(Arrays.toString(arr));
return arr;
}
public static void main(String[] args) {
int arr[] = first();
add(arr);
delete(arr);
}
static void add(int arr[]) {
System.out.println("Enter the number you want to add");
Scanner one = new Scanner(System.in);
int naya = one.nextInt();
for (int i = 0; i <= arr.length - 1; i++) {
arr[i] = arr[i] + naya;
}
System.out.println("The added array is");
System.out.println(Arrays.toString(arr));
}
static void delete(int arr[]) {
System.out.println("Enter the number you want to substract");
Scanner two = new Scanner(System.in);
int arko = two.nextInt();
for (int i = 0; i <= arr.length - 1; i++) {
arr[i] = arr[i] - arko;
}
System.out.println("The Substracted array is");
System.out.println(Arrays.toString(arr));
}
}
Your program is asking You every time to input array, because every time You invoke method first() new array is being created

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();

how to match entries and display entries

I do not know how to display entries entered by user, and also to match and sort them.
import java.util.Scanner;
public class Case1
{
public static void main(String[] args) {
Scanner in=new Scanner (System.in);
System.out.println("Input a size of an array: ");
int size = in.nextInt();
int num[]=new int[size];
int i=0;
for (i=0;i<num.length;i++) {
System.out.println("Input a number: ");
num[i]=in.nextInt();
}
for (int c=0;i<num.length;c++){
for (int a=0; a<num.length;a++){
if(num[c]>num[a]){
int temp = num[c];
num[c]= num[a];
num[a]=temp;
}
}
}
for (int d=0;i<num.length;d++){
int value = 0;
if(value==num[i]) {
System.out.println("Match Found!");
}
}
}
}
help please.
It should look like this ->
import java.util.Scanner;
public class Case1
{
public static void main(String[] args) {
Scanner in=new Scanner (System.in);
System.out.println("Input a size of an array: ");
int size = in.nextInt();
int num[]=new int[size];
int i=0;
for (i=0;i<num.length;i++) {
System.out.println("Input a number: ");
num[i]=in.nextInt();
}
// DISPLAY ENTRIES
System.out.println("You entered the following entries.");
for (int index=0; index<num.length; index++) {
System.out.print(index + ": " + num[index] + " ");
}
// END DISPLAY ENTRIES
for (int c=0;c<num.length;c++){ // Changed i to c
for (int a=0; a<num.length;a++){
if(num[c]>num[a]){
int temp = num[c];
num[c]= num[a];
num[a]=temp;
}
}
}
for (int d=0;d<num.length;d++){ // Changed i to d
int value = 0;
if(value==num[d]) { // Changed i to d
System.out.println("Match Found!");
}
}
}
}
This should work, but when you asked in your question about matching, did you want to match each entry with 0 (which makes no sense) or did you want to compare each entry to 0?

Categories

Resources