static Scanner scan = new Scanner(System.in);
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Enter number of arrays: ");
int noOfArrays = scan.nextInt();
String students[] = new String[noOfArrays];
System.out.println("----------------------");
while (true){
System.out.println("Enter Student names and scores:");
for (int idx = 0; idx < students.length; idx++){
int scores[] = new int[noOfArrays];
System.out.println("\t"+ (idx+1)+ ". ");
String studName = sc.nextLine();
students[idx] = studName;
for (int indx = 0; indx < scores.length; indx++){
System.out.println("\t Score: ");
int score = sc.nextInt();
scores[indx] = score;
}
}
}
}
I need to get this output, can somebody out there help me?
Enter number of arrays: 3
"----------------------
Enter Student names and scores:
1. Name
Score: 81
2. Name2
Score: 82
3. Name3
Score: 83
Your code have lots of bugs.
public static void main(String[] args) throws IOException {
Scanner scan = new Scanner(System.in);
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of arrays: ");
int noOfArrays = scan.nextInt();
String students[] = new String[noOfArrays];
int idx=0;
System.out.println("----------------------");
System.out.println("Enter Student names and scores:");
int scores[]=new int[noOfArrays];
for ( idx = 0; idx < students.length; idx++){
System.out.print("\t"+ (idx+1)+ ". Name ");
sc.next(); // this is to skipping next line
students[idx] = sc.nextLine();
System.out.print("\tScore: ");
scores[idx] =sc.nextInt();
}
for (int i=0;i<students.length;i++) {
System.out.println("Name="+students[i]+"\tScore="+scores[i] );// for printing student name and score
}
}
output:
Enter number of arrays: 3
----------------------
Enter Student names and scores:
1. Name raja
Score: 54
2. Name gita
Score: 99
3. Name sita
Score: 88
The problem is here
for (int indx = 0; indx < scores.length; indx++){
System.out.println("\t Score: ");
int score = sc.nextInt();
the loop iterates until scores.length but scores is undefined.
Related
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]);
}
}
}
How can I use a for loop asking for student[0]'s grade, [1]'s etc? As of now, my for loop is pointless.
public static void main(String[] args) {
//declare grade and student array
int[] grades = new int[5];
String[] students = { "Bill", "Tom", "Mark", "Nic", "Miguel"};
Scanner sc = new Scanner(System.in);
for(int i = 0; i < grades.length; i++) {
grades[i] = i + 1;
//user input of student's grade
System.out.print("Enter grade for " + students[0] + ": ");
grades[0] = sc.nextInt();
System.out.print("Enter grade for " + students[1] + ": ");
grades[1] = sc.nextInt();
}
}
There must be a better way than hardcoding each student and grade array.
You can take the input and assign it in the loop itself.
for(int i = 0; i < grades.length; i++)
{
System.out.print("Enter grade for " + students[i] + ": ");
grades[i] = sc.nextInt();
}
That's all. You do not need to repeat code as that is what the loop will do by accessing each element with the index i.
However, I am not sure what the line grades[i] = i + 1; accomplishes in your code, since you anyways overwrite that value with the one you get as input.
If i am interpreting your question correct, what you want to do is accept grade values from user using for loop? My answer is as below, I have also printed grades which are accepted.
public static void main(String[] args) {
//declare grade and student array
int[] grades = new int[5];
String[] students = { "Bill", "Tom", "Mark", "Nic", "Miguel"};
Scanner sc = new Scanner(System.in);
System.out.println("please enter the grades of students");
for(int i=0; i<students.length; i++){
grades[i]= sc.nextInt();
}
for(int i=0; i<grades.length; i++){
System.out.println(grades[i]);
}
}
Please help me..I've been working on this nonstop and i still cant get this to run the way i want it to.I am new to programming. i am not very good at using methods. i have a sample of my work here and my problem is how do i get the average of the grades and how do i print it on the console. when i run this code the last values inside the variable grade are the only values that display on the console. I also need to check the attendance...how do i do it?
i need this code for my finals.
package studentmonitoring;
import java.util.*;
public class StudentMonitoring {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
Date date=new Date();
int[] attend1={},attend2,attend3,grade={};
String line="-------------------------------------------------------------------";
String[] idNum1={},idNum2={},gender={}, name1={},name2={},name3={},name4={},address={},sub={};
Scanner input=new Scanner(System.in);
int check=0,num=0;
System.out.println(line);
System.out.println("\t\t****STUDENT MONITORING****");
System.out.println(line);
System.out.println("\nGood day teacher, to start using this program input the following information first.\n");
System.out.println(line);
System.out.print("NAME OF SCHOOL: ");
String sName=in.nextLine();
System.out.print("ADDRESS OF SCHOOL: ");
String sAdd=in.nextLine();
System.out.print("SCHOOL IDENTIFICATION NUMBER: ");
String sId=in.nextLine();
System.out.println(line);
System.out.println("STUDENTS INFORMATION");
System.out.println("Enter the year and section of this class");
String yearSec=in.nextLine();
System.out.println("How many student information do you want to input? ");
int numberStudents=in.nextInt();
idNum1=new String[numberStudents];
name1=new String[numberStudents];
name2=new String[numberStudents];
name3=new String[numberStudents];
name4=new String[numberStudents];
attend1=new int[200];
attend2=new int[200];
attend3=new int[200];
address=new String[numberStudents];
grade=new int[100];
gender=new String[numberStudents];
idNum2=new String[numberStudents];
System.out.println("Enter the number of subjects you have: ");
int numSub=in.nextInt();
sub=new String[numSub];
in.nextLine();
for(int x=0;x<numSub;x++) {
System.out.print("Subjec #"+(x+1)+": ");
sub[x]=in.nextLine();
}
for(int x=0;x<numberStudents;x++) {
in.nextLine();
System.out.println(line);
System.out.println("Student #"+(x+1));
System.out.print("LAST NAME: ");
name1[x]=in.nextLine();
System.out.print("FIRST NAME: ");
name2[x]=in.nextLine();
System.out.print("MIDDLE INITIAL: ");
name3[x]=in.nextLine();
System.out.print("GENDER: ");
gender[x]=in.nextLine();
System.out.print("ADDRESS: ");
address[x]=in.nextLine();
System.out.print("ID NUMBER: ");
idNum1[x]=in.nextLine();
System.out.println(line);
System.out.println("Enter the grades here.");
System.out.println(line);
for (int a=0;a<numSub;a++) {
System.out.print(sub[a]+": ");
grade[a]=in.nextInt();
}
}
System.out.println("This is the list of the students info you've inputted.");
System.out.println("ID Number\t Name\t\t GENDER\t\t ADDRESS");
for(int x=0;x<numberStudents;x++) {
System.out.println(idNum1[x]+"\t\t"+name1[x]+" "+name2[x]+","+name3[x]+".\t\t"+gender[x]+"\t\t"+address[x]);
}
System.out.println("Subject\t\tGrades");
for(int x=0;x<numSub;x++) {
System.out.println(sub[x]+"\t\t"+grade[x]);
}
System.out.println(line);
System.out.println();
}
}
public static void main(String[] args) {
System.out.println("Enter the number of students you want: ");
Scanner scanner = new Scanner(System.in);
int numOfStudents = scanner.nextInt();
double[] grades = new double[numOfStudents];
double total = 0;
double currentGrade = 0;
for(int i=0; i<grades.length; i++) {
System.out.println("Grade achieved: ");
currentGrade = scanner.nextDouble();
grades[i] = currentGrade;
total += currentGrade;
}
scanner.close();
System.out.println(Arrays.toString(grades));
System.out.println("Average grade " + (total / numOfStudents));
}
Here we first set the size of the double array (which will populate the grades). Then we fill in the double array using a for loop and getting user input via scanner.nextDouble().
Here's an example:
double[] values = { 2.2, 3.6, 6.3, 3.6, 2.8, 8.5, 8.3, 8.3, 1.2, 10, 2.2, 5.6 };
double sum = 0;
for (int i = 0; i < values.length; i++)
sum += values[i];
System.out.println("Average: " + sum / values.length);
That answers your initial question.
But if you also are having problems to populate the array, you can do it:
int MAX = 3;
double[] values = new double[MAX];
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < MAX; i++) {
System.out.print("Type the grade: ");
values[i] = scanner.nextDouble();
}
scanner.close();
I am building an array that ask how many different inputs you have, then allowing you to enter each input. At the end I want to sum them up, but I keep getting an error.
Also when I go above 5 inputs, I lose one..... For example when I respond to the first question: Enter "10". When I start adding different numbers in it stops at nine. Please help.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the number of courses you have left: ");
int size = input.nextInt();
int[] numArr = new int[size];
System.out.println("Enter the number of CUs for each course: ");
for (int i=0; i<numArr.length; i++)
{
numArr[i] = input.nextInt();
}
int totalSum = numArr + totalSum;
System.out.print("The sum of the numbers is: " + totalSum);
Change your logic and code to the following:
Scanner input = new Scanner(System.in);
System.out.println("Please enter the number of courses you have left: ");
int size = input.nextInt();
System.out.println("Enter the number of CUs for each course: ");
int totalSum = 0;
for (int i = 0; i < size; i++) {
totalSum+=input.nextInt();
}
System.out.print("The sum of the numbers is: " + totalSum);
try
System.out.println("Please enter the number of courses you have left: ");
int size = input.nextInt();
int totalSum = 0;
for (int i=0; i< size; i++)
{
System.out.println("Enter the number of CUs for each course: ");
int cuNum = input.nextInt();
totalSum += cuNum ;
}
System.out.print("The sum of the numbers is: " + totalSum);
Pretty sure your intended result is
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the number of courses you have left: ");
int size = input.nextInt();
int[] numArr = new int[size];
int totalSum = 0;
System.out.println("Enter the number of CUs for each course: ");
for (int i=0; i<numArr.length; i++)
{
numArr[i] = input.nextInt();
totalSum += numArr[i];
}
System.out.print("The sum of the numbers is: " + totalSum);
}
Your other code didn't work mainly because of
int totalSum = numArr + totalSum;
You can't define totalSum to defines itself! And you can't just use numArr... numArr is an array - you have to access indexes, not the array as a whole!
does anyone know how to set a user input for an array, I cant find the command anywhere. my array 'grades' have 20 locations. im not so sure about 'grades.length' function but I think it prompts 20 times. BUT I added a while statement to override BUT ITS TOTALLY IGNORING THE FOR STATEMENT. if I could set user input for array I could get rid of the while statement...
program has to accept grade for number of students the user inputs btw..
import java.util.Scanner;
public class gradesaverage {
public static void main(String[] args) {
int [] grades = new int [20];
int i;
int numStudents;
System.out.print("Enter number of students: ");
Scanner scanint = new Scanner (System.in);
numStudents = scanint.nextInt();
for ( i = 1; i <= grades.length; ++i)
{
System.out.println("Enter grade: ");
grades[i] = scanint.nextInt();
}
while(i <= numStudents );
}
}
Not sure what you mean, but assuming all input is correct,
int [] grades = new int [numStudents ];
Should work if you move this line after declaration and assignment of numStudents. There is no problem in java with variable length arrays.
Also note - your iterator i starts from 1, while in java arrays start from 0.
public static void main(String[] args) {
int i;
int numStudents;
System.out.print("Enter number of students: ");
Scanner scanint = new Scanner (System.in);
numStudents = scanint.nextInt();
int [] grades = new int [numStudents]; //the size we wanted
for ( i = 0; i < grades.length; ++i) //starting from 0, not 1.
{
System.out.println("Enter grade: ");
grades[i] = scanint.nextInt();
}
//print the array - for checking out everyting is ok
System.out.println(Arrays.toString(grades));
}