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