Error when entering array from keyboard - java

package array;
import java.util.Scanner;
public class Array {
public static void main(String[] args) {
int n;
Scanner input = new Scanner(System.in);
n = input.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = input.nextInt();
}
System.out.println(a);
}
}

You cannot print an array directly, you have to iterate through it's values.
for (int j=0;j<a.length;j++){
System.out.println(a[j]);
}

Related

Need to print a string array

My task is to read the strings by input, and then display the strings that have more than 4 vowels in each. I have this code:
import java.util.Scanner;
public class Main {
static boolean vowelChecker(char a) {
a = Character.toLowerCase(a);
return (a=='a' || a=='e' || a=='i' || a=='o' || a=='u' || a=='y');
}
static int counter(String str) {
int count = 0;
for (int i = 0; i < str.length(); i++)
if (vowelChecker(str.charAt(i))) {
++count;
}
return count;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements you want to store: ");
int n;
n=scanner.nextInt();
String[] array = new String[100];
System.out.println("Enter the elements of the array: ");
for(int i=0; i<n; i++)
{
array[i]=scanner.nextLine();
}
String str = scanner.nextLine();
int b = counter(str);
if (b > 4) {
System.out.println("What do I write here?");
}
}
}
And my question is: how to correctly write the code so that the output would be strings from input that have more than 4 vowels?
import java.util.Scanner;
public class Test {
private static final char[] VOWELS = new char[]{'a', 'e', 'i', 'o', 'u', 'y'};
public static void main(String[] args) {
// Initialize and open a new Scanner
final Scanner scanner = new Scanner(System.in);
// Get the number of lines we want to analyze
System.out.print("Enter the number of elements you want to store: ");
final int n = Integer.parseInt(scanner.nextLine());
// Get all the lines from the user
System.out.println("Enter the elements of the array: ");
final String[] lines = new String[n];
for(int i = 0; i < n; i++) {
lines[i]=scanner.nextLine();
}
// Close the Scanner
scanner.close();
// Check each line, count the number of vowels, and print the line if it has more than 4 vowels.
System.out.println("\nInputs that have more than 4 vowels");
for(int i = 0; i < n; i++) {
if (countVowels(lines[i]) > 4) {
System.out.println(lines[i]);
}
}
}
private static boolean isVowel(char a) {
for (int i = 0; i < VOWELS.length; i++) {
if (Character.toLowerCase(a) == VOWELS[i]) {
return true;
}
}
return false;
}
private static int countVowels(final String str) {
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (isVowel(str.charAt(i))) {
count++;
}
}
return count;
}
}
Like others have pointed out, you never read from the array.
Read for each of the strings, if the counter() returns a value larger than 4, we want to print it. So, this could do the trick:
for(int i=0; i<n; i++)
{
if (counter(array[i]) > 4)
System.out.println(array[i]);
}
Using nextInt won't absorb the newline character \n, that's why you are inputting 1 string less. There are some workarounds that you can read about here.
So this first part makes sense :
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements you want to store:
");
int n;
n=scanner.nextInt();
String[] array = new String[100];
System.out.println("Enter the elements of the array: ");
for(int i=0; i<n; i++)
{
array[i]=scanner.nextLine();
}
after this part I would just do :
for (String s: array) {
if (Objects.isNull(s))
break;
if (count(s) >= 5) {
System.out.println(s);
}
}
import java.util.Arrays;
import java.util.Objects;
import java.util.Scanner;
class Main {
static long counter(String str) {
return Arrays.stream(str.split(""))
.filter(c -> Arrays.asList("a", "e", "i", "o", "u", "y").contains(c.toLowerCase()))
.count();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements you want to store: ");
int n;
n = scanner.nextInt();
scanner.nextLine();
String[] array = new String[n];
System.out.println("Enter the elements of the array: ");
for (int i = 0; i < n; i++) {
String str = scanner.nextLine();
if (counter(str) > 4) {
array[i] = str;
}
}
System.out.println(Arrays.toString(Arrays.stream(array).filter(Objects::nonNull).toArray(String[]::new)));
}
}

Runtime error in java about selection sort

I have learnt selection sort, and i try to code it with java. But it has an error, i think it a runtime error. I don't know what to fix in my code.
This is the code:
import java.util.Scanner;
public class Main {
public static void main(String args[])
{
int temp;
Scanner sc=new Scanner(System.in);
int number;
int input=sc.nextInt();
int [] carriage;
carriage=new int[input];
for(int i=0;i<input;i++)
{
number=sc.nextInt();
carriage[i]=number;
}
int n=carriage.length;
for(int i=0;i<n-1;i++)
{
for(int j=i+1;i<n;j++)
{
if(carriage[j]<carriage[i])
{
temp=carriage[i];
carriage[i]=carriage[j];
carriage[j]=temp;
}
}
System.out.println(carriage[i]+ " ");
}
sc.close();
}
}
I think you want to sort the number of integer provided by user. Your code is having 2 errors. One in the for loop starting with i, the condition should be i
public class Main {
public static void main(String args[]) {
int temp;
Scanner sc = new Scanner(System.in);
int number;
System.out.println("Enter the number of integers to be sorted - ");
int input = sc.nextInt();
int[] carriage;
carriage = new int[input];
for (int i = 0; i < input; i++) {
System.out.println("Enter the "+ i+1 +"number - ");
number = sc.nextInt();
carriage[i] = number;
}
int n = carriage.length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (carriage[j] < carriage[i]) {
temp = carriage[i];
carriage[i] = carriage[j];
carriage[j] = temp;
}
}
System.out.println(carriage[i] + " ");
}
sc.close();
}
}

Selection Sort String

I'm trying to have a program where the user enters names and they are put into an array. Then they should be ordered in ascending order by using Selection Sort. I tried doing this but it doesnt compile. Any ideas why?
Thanks!
import java.util.*;
public class SuperHeros{
public static void main(String[]args){
Scanner in=new Scanner(System.in);
System.out.println("Enter 5 Super Hero Names");
String name=in.nextLine();
String[] Super=new String[5];
int min=0;
for(int i=0; i<Super.length; i++){//Enter Names in unordered list
Super[i]=in.nextLine();
System.out.println(Super[i]);
min=i;
for(int j=i+1;j<Super.length;j++){// Have an ascending list
int temp=Super[i];
Super[i]=Super[j];
Super[i]=temp;
System.out.println(Super[i]);
}
}
}
}
You are attempting to assign a String into an int value at int temp = Super[i]. Also the way you setup your prints and in.nextLine() calls were causing issues when running the program.
import java.util.*;
public class Test{
public static void main(String[]args){
Scanner in=new Scanner(System.in);
String[] Super=new String[5];
for(int i=0; i<Super.length; i++){//Enter Names in unordered list
System.out.println("Enter Super Hero Name: ");
Super[i]=in.nextLine();
}
// Selection sort
for (int j = 0; j < Super.length - 1; j++){
int min = j;
for (int k = j + 1; k < Super.length; k++){
if (Super[k].compareTo(Super[min]) < 0){
min = k;
}
String temp = Super[j];
Super[j] = Super[min];
Super[min] = temp;
}
}
System.out.println(Arrays.toString(Super));
}
}

How do I Store 6 integers from Scanner Console into a Set

I know this is simple. How would I take input from my console and store the input into a Set that can later be used to be returned on a Method. This is what I have so far.
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class InputConsole {
public static void main(String[] args) {
Set<Integer> s = new HashSet<Integer>(6);
int[] numbers = new int[6];
Scanner input = new Scanner(System.in);
for (int i = 0; i < numbers.length; i++) {
System.out.print("Please enter number ");
numbers[i] = input.nextInt();
{
}
}
}
}
I am using and Array just to test with. The Array is set to 6 so if I type 6 numbers in the console it will stop. I have instantiated the HashSet but I don't know how to go about storing the numbers from the console into it.
Use method Set::add()
for (int i = 0; i < numbers.length; i++)
{
System.out.print("Please enter number ");
s.add(input.nextInt());
}
You don't need int[] array
EDIT:
Whole main()
public static void main(final String ... args)
{
final int inputs = 6;
final Set<Integer> s = new HashSet<Integer>(6);
final Scanner input = new Scanner(System.in);
for (int i = 0; i < inputs; i++)
{
System.out.print("Please enter number #" + (i + 1) + ":");
s.add(input.nextInt());
}
System.out.println("Well done!");
System.out.println(s);
}
import java.util.*;
class Hashsetdemo
{
public static void main(String args[])
{
HashSet h=new HashSet(6);
int [] no = new int[6];
Scanner s=new Scanner(System.in);
for (int i=0;i<no.length;i++)
{
System.out.println("please enter number");
h.add(s.nextInt());
}
System.out.println(h);
}
}

Using scanner to produce arrays

I am using java and want to use a Scanner to get user input and then output arrays.
I've created a file that works for one and a separate file for the other, but I want to ask the user questions and get custom array properties back (variable number of arrays, variable rows, variable columns, variable addition, subtraction, and multiplication of the array)
import java.util.Scanner;
import java.io.IOException;
public class ScannerDemoforNetbeans
{
public static void main (String [] args)throws IOException
{
//`enter code here`
System.out.println ("Okay, you got it! How many rows do you want");
Scanner rc= new Scanner (System.in);
public static rows;
rows= rc.nextInt();
System.out.println ("How many columns do you want?");
Scanner c = new Scanner (System.in);
public static columns;
columns = c.nextInt();
System.out.println ("Rows" + rows);
System.out.println ("Columns:" + columns);
}
// }
// public static void createarray (String [] args)
// {
int matWidth = ScannerDemoforNetbeans.columns;
int matHeight = ScannerDemoforNetbeans.rows;
int maxCellValue = 70;
int [][] mat1 = new int [matHeight][matWidth];
int [][] mat2 = new int [matHeight][matWidth];
int [][] sum = new int [matHeight][matWidth];
for (int i=0; i<matHeight; ++i)
{
for (int j=0; j<matWidth; ++j)
{
java.util.Random r = new java.util.Random();
mat1 [i][j]= r.nextInt (maxCellValue);
mat2 [i][j] = r.nextInt (maxCellValue);
}
}
print2Darray (mat1);
System.out.println ();
print2Darray (mat2);
for (int i=0; i<matHeight; ++i)
{
for (int j=0; j<matWidth; ++j)
{
sum [i][j]= mat1 [i][j] + mat2 [i][j];
}
}
System.out.println ();
print2Darray (sum);
}
public static void print2Darray (int[][]arr)
{
for (int [] i : arr)
{
for (int j : i)
{
System.out.print (j + " ");
}
System.out.println();
}
System.out.println ();
}
}
Any help is appreciated.
package araay;
import java.util.Scanner;
public class Araay {
public static void main(String[] args)
{
Scanner a=new Scanner(System.in);
int i=0;
int j=0;
int arr[][]=new int [3][3];
System.out.println ("Enter the numbers");
for (i=0;i<=3;i++)
for(j=0;j<=3;j++)
arr[i][j]=a.nextInt();>>>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at araay.Araay.main(Araay.java:18)//this error is occur in this code plz solve it
for (i=0;i<=3;i++)
for(j=0;j<=3;j++)
System.out.println("The matrix of array is "+arr[i][j]+"\t");
System.out.println();
}
}

Categories