How to Display the Prime Numbers in an Array using Scanner Class - java

I have recently started learning Java. My school teaches us this weird way that i haven't really seen in many places. I can't find any problems here but the code just won't work. Please point out what is wrong here. Here's the code that I wrote:
import java.util.*;
class Prime_array_attempt_infinity
{
public static void main ()
{
Scanner sc= new Scanner (System.in);
int i, j, counter=0, last;
int arr[]= new int [10];
System.out.println("Enter the values");
for (i=0; i<10;i++)
{
arr[i]= sc.nextInt();
}
for (i=0; i<10; i++)
{
last = arr[i];
for (j=2;j<last;j++)
{
if(arr[i]%j==0)
counter++;
}
if (counter == 0)
{
System.out.println(arr[i]+" is a Prime Number");
}
}
}
}

I guess your code does not run.
You need to replace your main method with
public static void main(String[] args) {
}
And paste all code in it. Like this
public static void main(String[] args) {
Scanner sc= new Scanner (System.in);
int i, j, counter=0, last;
int arr[]= new int [10];
System.out.println("Enter the values");
for (i=0; i<10;i++)
{
arr[i]= sc.nextInt();
}
for (i=0; i<10; i++)
{
last = arr[i];
for (j=2;j<last;j++)
{
if(arr[i]%j==0)
counter++;
}
if (counter == 0)
{
System.out.println(arr[i]+" is a Prime Number");
}
}
}
Then you are able to run your code snippet. Regarding the algorithm for finding, see e.g. example algorithm

Missed args for main method, and you need to reset counter to zero every time before inner for loop( also 1 is not a prime number by definition :) ):
import java.util.*;
class Prime_array_attempt_infinity
{
public static void main (String[] args)
{
Scanner sc= new Scanner (System.in);
int i, j, counter=0, last;
int arr[]= new int [10];
System.out.println("Enter the values");
for (i=0; i<10;i++)
{
arr[i]= sc.nextInt();
}
for (i=0; i<10; i++)
{
last = arr[i];
if(last <= 1)continue;
counter = 0;
for (j=2;j<last;j++)
{
if(arr[i]%j==0)
counter++;
}
if (counter == 0)
{
System.out.println(arr[i]+" is a Prime Number");
}
}
}
}
output:
Enter the values
1
2
3
4
5
6
7
8
9
10
2 is a Prime Number
3 is a Prime Number
5 is a Prime Number
7 is a Prime Number

Related

Java Bar Chart Printing Program

here is the question:
Write an application that reads five numbers between 1 and 30. For
each number that’s read, your program should display the same number of adjacent asterisks. For
example, if your program reads the number 7, it should display *******. Display the bars of asterisks
after you read all five numbers.
here is my code:
package Assignment.Q034;
import java.util.Scanner;
public class Q034_trial
{
public static void main (String[] args)
{
Scanner input = new Scanner (System.in);
int num;
num = 1-30;
for (int i = 0; i < 5; i++)// system asks for no more than 5 numbers
{
System.out.printf("Enter a number: ");
num = input.nextInt();
}
for (int j = 0; j < num; j++)
{
System.out.printf("*");
}
System.out.println();
}
}
program IDe used: Apache Netbeans IDE 12.4
the code does not sure any error but when I run and debug it, the output shows like this:
Enter a number: 1
Enter a number: 2
Enter a number: 3
Enter a number: 4
Enter a number: 5
*****
but the output I need is:
Enter a number: 1
Enter a number: 2
Enter a number: 3
Enter a number: 4
Enter a number: 5
*
**
***
****
*****
I am new to java programming. please help me t find the solution.
You can try to break them down individually and try to incorporate an approach like this or use these ideas for your project:
import java.util.Scanner;
public class Array {
public static void main(String[] args){
Array asteriskGenerator = new Array();
int nb[]=new int[5];
Scanner input = new Scanner (System.in);
for(int i=0;i<5;i++)
{
System.out.print("Please, Enter a number between 1 - 30 ");
nb[i]=input.nextInt();
}
input.close();
asteriskGenerator.asteriskGenerator(nb);
}
void asteriskGenerator(int nb[])
{
for(int i = 0; i < nb.length; i++)
{
for(int j=1;j<=nb[i];j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
I hope this helps in what you are trying to accomplish!
You need to read in five integers, and then when you are done, do something with them. This means you need some way to store all five integers.
The obvious solution is to store them in an array.
public class Q034_trial
{
public static void main (String[] args)
{
Scanner input = new Scanner (System.in);
int[] nums = new int[5];
for (int i = 0; i < 5; i++)
{
System.out.printf("Enter a number: ");
int num = input.nextInt();
nums[i] = num;
}
}
}
Having done that you merely need to iterate over each number in the array to print the correct number of asterisks.
public class Q034_trial
{
public static void main (String[] args)
{
Scanner input = new Scanner (System.in);
int[] nums = new int[5];
for (int i = 0; i < 5; i++)
{
System.out.printf("Enter a number: ");
int num = input.nextInt();
nums[i] = num;
}
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < nums[i]; j++)
System.out.printf("*");
System.out.println();
}
}
}

How to find an average of numbers in two-dimensional array

public class iDon'tRemember {
public static void main(String[] args) {
double average=0;
int sum = 0;
Scanner in = new Scanner(System.in);
System.out.println("Enter 2 numbers: ");
int a=in.nextInt();
int b=in.nextInt();
int[][]array=new int[a][b];
Random rnd=new Random();
for (int i=0;i<array.length;i++){
for (int j =0; j<array[i].length;j++){
array[i][j]=rnd.nextInt(10)+1;
sum+=array[i][j];
average=(float)sum/array.length;
System.out.print(array[i][j] + "\t");
}
System.out.println();
}
System.out.println(average+"is average of array!");
}
}
I'm trying to find an average of numbers but it's usually wrong in console, help to find the mistake!
I just corrected your code:
import java.util.Random;
import java.util.Scanner;
public class Average2DArray {
public static void main(String[] args)
{
double average=0;
int sum = 0;
Scanner in = new Scanner(System.in);
System.out.println("Enter 2 numbers: ");
int a=in.nextInt();
int b=in.nextInt();
int[][]array=new int[a][b];
Random rnd=new Random();
for (int i=0;i<array.length;i++)
{
for (int j =0; j<array[i].length;j++)
{
array[i][j]=rnd.nextInt(10)+1;
sum+=array[i][j];
System.out.print(array[i][j] + "\t");
}
System.out.println();
}
average=(float)sum/(a*b);//length of 2D array is a*b <--------- also moved it out of the loop
System.out.println(average+"is average of array!");
}
}
Keep the class name in the program and *class file name same!!
Don't use special character in Class name
import java.util.*;
public class Main{
public static void main(String[] args) {
double average=0;
int sum = 0;
Scanner in = new Scanner(System.in);
System.out.println("Enter 2 numbers: ");
int a=in.nextInt();
int b=in.nextInt();
int[][]array=new int[a][b];
Random rnd=new Random();
for (int i=0;i<array.length;i++){
for (int j =0; j<array[i].length;j++){
array[i][j]=rnd.nextInt(10)+1;
sum+=array[i][j];
average=(float)sum/(array.length * array.length);
System.out.print(array[i][j] + "\t");
}
System.out.println();
}
System.out.println(average+"is average of array!");
}
}
or more genericaly
average=(float)sum/(array.length * array[0].length);

Not output of the array value when used scanner

Below is the code. After entering the array, the console just goes blank and does not further output the array:
import java.util.Scanner;
public class advancedArrays {
public static void main(String[] args) {
System.out.println("Provide us the size of the array:");
Scanner scanner = new Scanner(System.in);
int value = scanner.nextInt();
int i = 0;
int[] array = new int[value];
System.out.println("Enter the array:");
Scanner input = new Scanner(System.in);
while(input.hasNextInt()) {
array[i] = input.nextInt();
i++;
}
System.out.println("Array entered:");
for(i=0;i<value;i++)
{
System.out.println(array[i]);
}
input.close();
scanner.close();
}
}
Output:
Provide us the size of the array:
5
Enter the array:
1 2 3 4 5
you are stuck in the "reading the array" part because of this
while (input.hasNextInt()) {
array[i] = input.nextInt();
hint: you know the size of the array, then why don't you do a for loop same as you did for printing out the array's content?? like:
for (int j = 0; j < value; j++) {
array[i] = scanner.nextInt();
i++;
}
The problem is that with the while loop that you keep it waiting for more integers, it is better to convert to a normal for loop with condition on the entered array size value.
Also there is no need to use two scanner objects
public static void main(String[] args) {
System.out.println("Provide us the size of the array:");
Scanner scanner = new Scanner(System.in);
int value = scanner.nextInt();
int i = 0;
int[] array = new int[value];
System.out.println("Enter the array:");
for (int j = 0; j < value; j++) {
if (scanner.hasNextInt()) {
array[i] = scanner.nextInt();
i++;
}
}
System.out.println("Array entered:");
for (i = 0; i < value; i++) {
System.out.println(array[i]);
}
scanner.close();
}
package Main;
import java.util.Scanner;
public class advancedArrays {
public static void main(String[] args) {
System.out.println("Provide us the size of the array:");
Scanner scanner = new Scanner(System.in);
int value = scanner.nextInt();
int i = 0;
int[] array = new int[value];
System.out.println("Enter the array:");
Scanner input = new Scanner(System.in);
while(input.hasNextInt()) {
array[i] = input.nextInt();
i++;
//Changed Code
if (i == value) {
break;
}
}
System.out.println("Array entered:");
for(i=0;i<value;i++)
{
System.out.println(array[i]);
}
input.close();
scanner.close();
}
}
Fixed code, you were stuck reading your inputs for your array because you never checked if your inputs were the length of your array.
Fixed code.
if (i == value) {
break;
}

comparision b/w elements of an array

in this code i want to take no. of testcases by keyboard input and rest is the same problem. only what i am doing is like. if i take 2 test cases then it shud be print the result based on both cases after taking the complete input. For example: INPUT testcases : 2 //case1// 5(no of building) 7 5 2 11 1 //case2// 3(no. of building) 1 2 3 OUTPUT 7//OUTPUT FOR 1ST CASE// 0//OUTPUT FOR 2ND CASE// HOPE, NOW PROBLEM IS CLEAR
import java.util.Scanner;
public class Komal {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("enter the test cases");
int T=sc.nextInt();
for(int i=1;i<=T;i++)
{
System.out.println("total no of building");
int n=sc.nextInt();
int ar[]=new int[n];
for(int j=0;j<n;j++)
{
System.out.println("enter the heights");
ar[j]=sc.nextInt();
}
for(int j=1;j<ar.length;j++)
{int sum=0;
if(ar[0]<ar[i])
{
break;
}else
{
sum += (ar[0]-ar[i]);
}
System.out.println(sum);
}
}
}
}
Here is the complete code. Luckily i had my laptop running and this isn't a very difficult program.
public class TillGreater{
public static void main(String args[]){
int[] ar = {5,4,2,7,1};
int sum=0;
for(int i = 1 ; i < ar.length;i++){
if(ar[0]<ar[i]){
break;
}else{
sum = sum + (ar[0]-ar[i]);
}
}
System.out.println(sum);
}
}
try this:
public static int SumUntilBigger(int[] a)
{
int sum=0;
for(int i=1;i<a.length;i++)
{
if(a[i]<=a[0])
sum+=a[0]-a[i];
else
return sum;
}
return sum;//will reach this statement if all the elements are bigger than the first one
}
this compares each element with the first element and when the element is bigger it just return the sum and exits from method, if all elements are bigger than the first one just return the sum of all differences.
you can use it in main like this:
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("enter the size of the array");
int size=s.nextInt();
int[] a=new int[size];
System.out.println("enter the array values");
for(int i=0;i<size;i++)
a[i]=s.nextInt();
System.out.println("The differences sum : "+SumUntilBigger(a));
}

Small addition to current code

import java.util.Scanner;
public class linecounter {
public static void main(String[] args) {
System.out.print("Enter a line of integers ");
Scanner chopper = new Scanner(System.in);
int x = chopper.nextInt();
while (chopper.hasNextInt()) {
System.out.println(chopper.nextInt());
}
}
}
I am in a CS1 class learning the basics of Java and have a quick question, on this code could anyone tell me how i could get it to keep count of how many integers were typed in?
Thank you
above your while loop, declare:
int count = 0;
then in your while loop use
count++;
This will start you at 0 and every time it increments the count
You could add a counter to the while loop.
int counter = 0;
while (chopper.hasNextInt()) {
counter++;
System.out.println(chopper.nextInt());
}
System.out.println(counter);
In the cases that you have integer numbers, double numbers and you only need count the integer numbers, you can use:
public class linecounter {
public static void main(String[] args) {
System.out.print("Enter a line of integers ");
Scanner chopper = new Scanner(System.in);
int x = chopper.nextInt();
int counter = 0;
while (chopper.hasNextInt()) {
System.out.println(chopper.nextInt());
String myCurrentArg = chopper.nextInt();
if(isInteger(myCurrentArg) ){
counter++;
}
}
System.out.println("The number of integer arguments are: " + counter);
}
public static boolean isInteger(String s) {
return isInteger(s,10);
}
}

Categories

Resources