I'm trying to compare two Array Ints.
This is what I have so far:
package array;
import java.util.Scanner;
import java.util.Arrays;
public class Array {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int [] lottery_number = new int[49];
int i;
int a = 0;
for (i=0; i<lottery_number.length; i++){
lottery_number[i]=i+1;
}
System.out.println("Please insert 6 numbers");
int [] Number = new int [6];
Number[0] = input.nextInt();
Number[1] = input.nextInt();
Number[2] = input.nextInt();
Number[3] = input.nextInt();
Number[4] = input.nextInt();
Number[5] = input.nextInt();
}
}
I'm trying to compare the user input to certain Lottery_number array.
I point out that I'm not sure of what you are asking, but it makes no sense to compare the lottery numbers array (all natural numbers from 1 to 50) with the player picks array (6 random numbers from 1 to 50.
Using the static method
Arrays.equals(int[] array1, int[] array2)
will return whether the arguments are equals (same number of elements, same value) but eventually this is not the case. Sorry if I have completely misunderstood what you asked.
numberInCommon is a variable that says how many numbers the arrays have in common. I hope this is what you're looking for. You have to import java.util.Arrays
List lotteryNumbers = Arrays.asList(lottery_numbers);
int numbersInCommon = 0;
for(int i : Number){
if(lotteryNumbers.contains(new Integer(i)))
numbersInCommon++;
}
EDIT: You'll also need change
int [] lottery_number = new int[49]; to
Integer [] lottery_number = new Integer[49];
Related
Java is my first programming language, and I'm still unfamiliar with how arrays work. However, I was able to make this program, which accepts user-input for an integer array; it then outputs indexes and values, to show how arrays store numbers. I would like to recreate this program using a string array, to make a table containing a list of friends.
The .length property also confuses me...
Could someone explain the .length property and help me make the string array program work?
Thank you very much.
Here is the working code for the integer array table program
import java.util.*;
public class AttemptArrayTable
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Let me show you how arrays are stored ");
System.out.println("How many numbers do you want your array to
store? ");
int arrayInput [] = new int[scan.nextInt()];
System.out.println("Enter numbers ");
for (int count = 0; count<arrayInput.length; count++)
arrayInput[count] = scan.nextInt();
System.out.println("");
System.out.println("Index\t\tValue");
for (int count2=0; count2<arrayInput.length; count2++)
System.out.println(" [" + count2 + "]"+"\t\t " + arrayInput[count2]);
}
}
Here is the code for the string array program I'm working on
import java.util.*;
public class ArrayTableofFriends
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("How many female friends do you have? ");
String arrayOfFriendsFem [] = new String [scan.nextInt()];
System.out.println("List the name of your female friends");
for(int countF = 0; countF<arrayOfFriendsFem.length; countF++)
arrayOfFriendsFem[countF]= scan.nextLine();
System.out.println("How many male friends do you have? ");
String arrayOfFriendsMale [] = new String [scan.nextInt()];
System.out.println("List the name of your male friends");
for(int countM = 0; countM<=arrayOfFriendsFem.length; countM++)
arrayOfFriendsMale[countM]= scan.nextLine();
System.out.println("How many alien friends do you have? ");
String arrayOfFriendsAliens [] = new String [scan.nextInt()];
System.out.println("List the name of your alien friends");
for(int countA = 0; countA<=arrayOfFriendsFem.length; countA++)
arrayOfFriendsAliens[countA]= scan.nextLine();
{
System.out.println("Female\t\t\t" + "Male\t\t\t" + "Aliens");
for(int countF2 = 0; countF2<arrayOfFriendsFem.length; countF2++)
System.out.println(arrayOfFriendsFem[countF2]);
for(int countM2 = 0; countM2<=arrayOfFriendsMale.length; countM2++)
System.out.println("\t\t\t" + arrayOfFriendsMale[countM2]);
for(int countA2 = 0; countA2<=arrayOfFriendsAliens.length; countA2++)
System.out.println("\t\t\t\t\t\t" +arrayOfFriendsAliens[countA2]);
}
}
.length property stores number of elements in the array. But elements are starting from 0. So, when .length = 1, then there is only one element in the array, with index 0.
It seems in your String arrays program in the for loop the <= should be changed to <
Like this:
for (int countA = 0; countA < arrayOfFriendsFem.length; countA++)
So I'm trying to figure out how to take user input thru the Scanner object, place it in each slot of an array, then read those numbers back to the user plus one. Problem is I have to use a a loop for the read back statement.Heres what I have so far. I figured out the first loop fine with the scanner, but I don't know how to modify each element in the array using a loop.
import java.util.Scanner;
public class Lab7
{
public static void main(String [] Args)
{
Scanner console = new Scanner(System.in);
System.out.println("Enter your 5 integers: ");
int index =0;
final int SIZE = 5;
int[] arrayOfSize = new int[SIZE];
while(index<arrayOfSize.length )
{
arrayOfSize[index]=console.nextInt();
index++;
}
System.out.println("Processing each array element...");
You can do the below, Here i am first taking the user input integers and increasing it by 1 and then storing it in the a[] array of integers code for it is int j = scanner.nextInt();
// store it in array as incremented by 1.
a[i]=j+1;,
which i am iterating later to get the user input value+1 for example if user input was 1, then in array it would be stored as 2 :-
Complete runnable code is below with the comments and sample input and output :-
public class SOTest {
public static void main(String[] args) {
// create scanner object
Scanner scanner = new Scanner(System.in);
// create an array of 10 integers
int a[] = new int[10];
for(int i=0;i<10;i++){
int j = scanner.nextInt();
// store it in array as incremented by 1.
a[i]=j+1;
}
// Now array of integers have the user input value+1.
for(int i=0;i<10;i++) {
System.out.println(" "+ a[i]);
}
}
}
My program input and output is below, which will make it easy to understand :-
1 2 3 4 5 6 7 8 9 11 printing user input value by adding 1 to it 2 3
4 5 6 7 8 9 10 12
I Think this code will easily understand by you
import java.util.Scanner;
public class Demo
{
public static void main(String [] Args)
{
Scanner console = new Scanner(System.in);
System.out.println("Enter your 5 integers: ");
int index =0;
final int SIZE = 5;
int[] arrayOfSize = new int[SIZE];
while(index<arrayOfSize.length )
{
arrayOfSize[index]=console.nextInt();
index++;
}
System.out.println("Processing each array element...");
for(int i=0;i<arrayOfSize.length;i++){
System.out.print((arrayOfSize[i]+1)+" ");//(arrayOfSize[i]+1)this will take current value stored in array and add 1 to it
}
}
}
I am trying to get an integer and a double value from the user using a scanner. Im having trouble setting the values i get to the array itself. Here is my code:
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
final int SIZE = 7;
int[] arr = new int[SIZE];
int[] arr2 = new int[SIZE];
double[] dub = new double[SIZE];
Scanner sc = new Scanner(System.in);
PayRoll p = new PayRoll();
for(String i: p.AccEmp()){
System.out.println("How many hours were worked by the this employee number: " + i);
arr = sc.nextInt();
p.SetHRS(arr);
System.out.println("What is the pay for this employee number: " + i);
dub = sc.nextDouble();
p.setPR(dub);
}
}
}
P is an instance, accEmp is an accessor from another class.
Also, i cant use Array List.
Your arr variable is an array of int. Scanner.nextInt will read and int, but not an array.
The line arr = sc.nextInt() won't compile. Either change arr to be an int or add the value to the array.
I believe, since you seem to be looping over employees, that you should keep a reference to the looping index and add to the array at that index :
for(int i = 0; i < p.accEmp().length/* or .size() if it's a list */; i++)
{
arr[i] = sc.nextInt();
}
and also incorporate this within the loop, the part where you get the double values:dub[i]=sc.nextDouble();
This is my code in java. There is a problem at System.out.println(averager(A)); . Java said that the Local variable A may not have been initialized.
import java.util.Scanner;
public class Averager {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("How many numbers do you want to average?(2-4 only");
int x;
int[] A;
x = input.nextInt();
int Array4[]={input.nextInt(),input.nextInt(),input.nextInt(),input.nextInt()};
int Array3[]={input.nextInt(),input.nextInt(),input.nextInt()};
int Array2[]={input.nextInt(),input.nextInt()};
if (x=='2'){
A =Array2;
}
else if (x=='3'){
A = Array3;
}
else if (x=='4'){
A= Array4;
}
else{
System.out.println("Error!");
}
System.out.println(averager(A)); // Error
}
public static int averager(int...numbers){
int total=0;
for(int x:numbers){
total+=x;
}
return total/numbers.length;
}
}
Look at your code and consider what happens if x is not '2', '3', or '4'. What is A as of the
System.out.println(averager(A));
line?
Right! You've never given it a value. That's what the compiler is warning you about.
Arrays in java can be initialized = null because if the code doesn't enter an if statements A is never set. Also if by chance you already know the dimensions of the array you can initialize it like this:
int[] A = new int[numberofelements];
you can change your code as
int x = input.nextInt();
int[] A= new int[x];
Your code is wrong.
It reads 9 (4 + 3 + 2) numbers always from STDIN (in Array4 to Array2 part).
What you want to do probably is
int x = input.nextInt();
int[] A = new int[x];
for ( int i = 0; i < x; ++i ) A[i] = input.nextInt();
But if you want to solve only "Local variable A may not have been initialized." warning, return (end the program) after printing the error
else {
System.out.println("Error!");
return;
}
Change: int[] A = null;
Change : int x to int x = 0;. You need to initialize variables in java.
Question: Write a method that takes as input a variable number of integers. The method should return the average of the integers as a double. Write a full program to test the method.
That code below creates an array of 10 integers and finds their average. However, I need it to create a variable number of arguments (int...a) where any number of integers entered can be averaged. Can someone help me.Thanks.
My code:
package average.pkgint;
import java.util.Scanner;
public class AverageInt {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
int [] number = new int [10];
Scanner b = new Scanner(System.in);
System.out.println("Enter your 10 numbers:");
for (int i = 0; i < number.length; i++) {
number[i] = b.nextInt() ;
}
System.out.println("The average is:"+Avnt(number));
}
public static int Avnt(int [] a){//declare arrays of ints
int result = 1;
int sum = 0;
//iterate through array
for (int num : a) {
sum += num;
}
double average = ( sum / a.length);//calculate average of elements in array
result = (int)average;
return result;
}
}
This is a way of getting as many variables as you want to a method and going through them all:
public static void testVarArgs(int... numbers){
for(double u: numbers) {
System.out.println(u);
}
}
If you don't want to read the number of integers as the first input you can declare a stack in stead of an array
Stack<Integer> numbers=new Stack<>()
and then you can add the numbers with the add method.
Just change your method declaration to a variable length parameter:
public static int Avnt(int ... a){//declare variable int arguments
When you use a variable length parameter, you supply the values in the method invocation. For example,
System.out.println(Avnt(1,2,3));
System.out.println(Avnt(1,2,3,4,5,6,9,9,9,10,10));
Actually, this looks like it meets your requirements. Just add method calls in your main() similar to these.