How does the program will ask 10 elements for the array with JOptionPane and then in the end it will show the 10 elements? It only ask one element and its done.
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Array1
{
public static void main(String[] args)
{
int n = 10;
Scanner sc=new Scanner(System.in);
int[] array = new int[10];
JOptionPane.showInputDialog(null, "Enter ten elements for the array: ");
for(int i=0; i<n; i++)
{
array[i]=sc.nextInt();
}
JOptionPane.showMessageDialog("The elements are: ");
for (int i=0; i<n; i++)
{
JOptionPane.showMessageDialog(null, array[i]);
}
}
}
Related
This is my post first here and I wanted to know how to remove elements in an array that have already been entered. for an assignment due Monday. The console will print all values entered in a distinct manner meaning it will only print values that have only been entered once.
People will be prompted with the message "Enter an integer" ten times and the system will take those values and remove any duplicates but only displaying one of the two entered. I.E if 17 is entered twice only display the first 17.
So here's my code I have so far
import java.util.Scanner;
import java.util.Arrays;
public class Unit03Prog2 {
public static void main(String[] args) {
//Array
int Numbers[] = new int[10];
Scanner Numb = new Scanner(System.in);
for(int i=0; i < Numbers.length; i++){
System.out.print("Enter an integer: ");
//Stores it, and moves to the next line
Numbers[i]= Numb.nextInt();
//Removes duplicates
//Code to remove duplicates goes here VVVV
//Ends it if i = 10
if(i == Numbers.length)
{
break;
}
//End of for statement
}
System.out.println("The number of distinct values is " + Arrays.toString(Numbers));
//end of main method
}
//end of Class
}
So the code works and all but it displays every value entered no matter what. I only need numbers that have only been entered once
I.E
Enter an integer : 11
Enter an integer : 12
Enter an integer : 13
Enter an integer : 14
Enter an integer : 15
Enter an integer : 16
Enter an integer : 16
Enter an integer : 17
Enter an integer : 19
Enter an integer : 19
It should say The number of distinct values is 11 12 13 14 15 16 17 19
Thanks guys,
Joshua.
You can use a Set to solve it:
import java.util.HashSet;
import java.util.Scanner;
import java.util.Arrays;
import java.util.Set;
public class Main {
public static void main(String[] args) {
//Array
int Numbers[] = new int[10];
Set<Integer> integerSet = new HashSet<>();
Scanner Numb = new Scanner(System.in);
for(int i=0; i < Numbers.length; i++){
System.out.print("Enter an integer: ");
//Stores it, and moves to the next line
Numbers[i]= Numb.nextInt();
//Removes duplicates
//Code to remove duplicates goes here VVVV
integerSet.add(new Integer(Numbers[i]));
if(i == Numbers.length) {
break;
}
}
System.out.println("The number of distinct values is " + Arrays.toString(integerSet.toArray()));
}
}
Replace the code to remove the duplicates. I suggest to before inserting into the array, check for duplicates. If the entered number is duplicate, skip it and read the next input.
import java.util.Scanner;
import java.util.Arrays;
public class Unit03Prog2 {
public static void main(String[] args) {
//Array
int Numbers[] = new int[10];
Scanner Numb = new Scanner(System.in);
int j = 0;
int inputNumber;
boolean isDuplicate;
for(int i=0; i < Numbers.length; i++){
isDuplicate = false;
System.out.print("Enter an integer: ");
//Stores it, and moves to the next line
inputNumber = Numb.nextInt();
//Removes duplicates
//Code to remove duplicates goes here VVVV //Ends it if i = 10
for (int k=0; k<j; k++) {
if(inputNumber == Numbers [k]) {
isDuplicate = true;
break;
}
}
if(! isDuplicate) {
Numbers[j++] = inputNumber;
}
//End of for statement
}
System.out.println("The number of distinct values is " + Arrays.toString(Numbers));
//end of main method
}
//end of Class
}
Yes, Use a Set, which only contains unique element, to achieve your purpose.
Maybe you need to keep the sequence of input, but Set does not keep sequence.
I think you want to have a logic to remove duplicated records in an array, right?
Try this:
import java.util.HashSet;
import java.util.Scanner;
import java.util.Arrays;
import java.util.Set;
public class Main {
public static void main(String[] args) {
//Array
int Numbers[] = new int[10];
Set<Integer> integerSet = new HashSet<>();
Scanner Numb = new Scanner(System.in);
for(int i=0; i < Numbers.length; i++){
System.out.print("Enter an integer: ");
//Stores it, and moves to the next line
Numbers[i]= Numb.nextInt();
integerSet.add(new Integer(Numbers[i]));
}
//Array to keep unique items with same sequence of input
int uniqueNumbers[] = new int[integerSet.size()];
integerSet= new HashSet<>();
for(int i=0;i<Numbers.length;i++){
if(!integerSet.contains(Numbers[i])){
uniqueNumbers[integerSet.size()]=Numbers[i];
integerSet.add(Numbers[i]);
}
}
System.out.println("The number of distinct values is " + Arrays.toString(uniqueNumbers));
}
public static void main(String[] args) {
int a[] = {1,2,3,4,5,1,2};
for(int i=0; i<a.length;i++) {
int count = 0;
for(int j=0; j<a.length;j++) {
if(a[i]==a[j] && i!=j) {
count++;
break;
}
}
if(count == 0) {
System.out.println(a[i]);
}
}
}
Good Morning,
I'm trying to write a code where the user writes some numbers in the keyboard (casually), and the program should write them in order, from the lower to greater.
I don't remember in particular how to insert the number from the keyboard to the array or to the arraylist class if i don't want any limit.
This is my code. i know it is not correct.
import java.util.*;
class Ordine {
public static void main (String [] args) {
Scanner s = new Scanner (System.in)
System.out.println; ("insert the number which you need to order");
int n = s.nextInt();
int [] x = new int [200];
for (int i=0; i<x.length; i++) {
x [i] = s.nextInt();
for (int j=i+1; j<x.length; j++) {
if (x[i] > x[j]) {
//cambia ELEMENTI
int temp = x[i];
x[i] = x[j];
x[j] = temp;
}
}
}
}
}
Thank you!
Since array have fixed size you will have to use arraylist. Below is the code
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class test{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
ArrayList<Integer> a = new ArrayList<Integer>(Arrays.asList(1,2,3,4,8,9,10,13));
try{
while (true)
{
int l=0;int r=a.size()-1,m=0;
int p=in.nextInt();
while(l<=r){
m=(l+r)/2;
if(a.get(m)>p){
l=l;r=m-1;
}
if(a.get(m)<p){
l=m+1;r=r;
}
if(a.get(m)==p){
l=m;
break;
}
}
a.add(l,p);
System.out.println("Entered number is: "+p);
System.out.print("Sorted arraylist is: ");
for(int j=0;j<a.size();j++){
System.out.print(a.get(j)+" ");
}
System.out.println();
}
}
catch(Exception e){
}
}
}
I have intitialized the arraylist. And it will print the ordered arraylist everytime you enter a new number.
Below is a sample output
Im relatively new to programming and i was working with a problem that requires me to read space separated integers and input them into a two dimensional array but i dont seem to understand why I am not able to do so . any hint on possible approaches will be helpful
Eg. Input will look like this
2 //FOR DIMENSION
2 3 //ROW1
4 5 // ROW 2
import java.io.*;
import java.util.*;
import java.lang.*;
public class HelloWorld{
public static void main(String []args){
Scanner sc= new Scanner(System.in);
System.out.println("Hello World");
System.out.println("Enter Dimension of Matrix");
int N= sc.nextInt();
int[][] m=new int [N][N];
for (int i=0;i<N;i++)
{ System.out.println("Enter Elements of row "+ i);
for(int j=0;j<N;j++ )
{
m[i]=sc.nextInt();
}
}
System.out.println(m);
}
}
I tested the above code and got the following result on the console
HelloWorld.java:16: error: incompatible types: int cannot be converted to int[]
m[i]=sc.nextInt();
^
First of all, I would like to know is my thinking(approach) right?
Second, what are the possible ways to do this correctly?
here you go:
import java.io.*;
import java.util.*;
import java.lang.*;
public class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Dimension of Matrix");
int N = sc.nextInt();
int[][] m = new int[N][N];
for (int i = 0; i < N; i++) {
System.out.println("Enter Elements of row " + i);
for (int j = 0; j < N; j++) {
m[i][j] = Integer.valueOf(sc.next());//replaced line with proper code
}
}
sc.close();
System.out.println(Arrays.deepToString(m));//use this api to print arrays
}
}
m is a multidimensional array meaning you must specify m[i][?]. Like so
m[i][j]=sc.nextInt();
Your logic is a little off, you want to ask the user for input at row start i, and for the other columns j. Try this and happy coding :)
for (int i=0;i<N;i++){
for(int j=0;j<N;j++){
System.out.println("Enter Elements of row "+ i);
m[i][j]=sc.nextInt();
}
}
for (int i=0;i<N;i++){
for(int j=0;j<N;j++){
System.out.print(m[i][j]);
}
System.out.println();
}
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);
}
}
I'm trying to print out the frequency of each integer in an array
import java.util.*;
public class NumFrequency {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the amount of numbers your going to input, up to 50");
int num = input.nextInt();
int array[] = new int[num];
System.out.println("Enter the "+ num + " numbers now.");
for (int i=0 ; i<array.length; i++) {
array[i] = input.nextInt();
}
System.out.println("array created");
printArray(array);
}
public static void printArray(int arr[]){
int n = arr.length;
for (int i=0; i<n; i++) {
System.out.print(arr[i]+" ");
}
}
private static int[] intFreqArray = new int[51];
public static void FreqOfInt(int[] array, int num) {
for (int eachInt : array) {
intFreqArray[eachInt]++;
}
for (int m = 0; m<intFreqArray.length; m++) {
if (intFreqArray[m] > 1) {
System.out.println(m+ " occurs " + intFreqArray[m] + " times.");
}
}
}
}
It'll print out the array created by the user but nothing after that I'm lost as to why it wont print out the last part.
You need to call FreqOfInt before you print.
Note that we normally use lower case letters for the names of Java methods.
In main, the last method call is to printArray, but you never call FreqOfInt. That's why that output doesn't show up.
Call it after calling printArray.