Hello guys, i'm having trouble with creating a sequential
search algorithm for a two dimensional int array. not sure exactly how to go about augmenting the while loop so it works , the example i'm using does it exactly how i wrote it out, as you can see my compiler is complaining about the incompatibility with the way its written out!
import java.util.*;
import javax.swing.*;
public class pencilneck
{
public static void main(String []alex)
{
int ROWS = 6;
int COLS = 3;
int[][] chargeAcc = new int[ROWS][COLS];
Scanner keyboard = new Scanner(System.in);
for(int row = 0; row < ROWS; row++)
{
for(int col = 0; col < COLS; col++)
{
System.out.print("Enter Account");
chargeAcc[row][col] = keyboard.nextInt();
}
}
System.out.print("Enter an account to be Charged");
int input = keyboard.nextInt();
int results = SequentialSearch(chargeAcc,input);
if(results ==-1)
{
System.out.println("that is an invalid #");
}
else
{
System.out.println("the # is valid");
}
}
public static int SequentialSearch(int[][] array, int value)
{
int index1 = 0;
int element = -1;
boolean found = false;
while(!found && index1 == value)
{
if(array[index] == value)
{
found = true;
element = index;
}
index++;
}
return element;
}
}
Your question doesn't quite make sense, but the bug in your code is easy enough to find.
while(!found && index1 == value)
This says in plain english, do the stuff in the loop while these conditions are both true:
Found is false
index (index of array) is equal to value (number you want to find in array)
At the start of the loop, index == 0. Since value is likely nonzero, the second condition is false and the loop never runs, causing SequentialSearch to return -1 immediately.
Now that you know what the problem is, I'll leave it to you to take time to understand what you did wrong and figure out how to fix it.
Array[index] is not an int, is an array.
i think what you are looking for is something like this:
enter code here
import java.util.*;
import javax.swing.*;
public class pencilneck
{
public static void main(String []alex)
{
int ROWS = 6;
int COLS = 3;
int[][] chargeAcc = new int[ROWS][COLS];
Scanner keyboard = new Scanner(System.in);
for(int row = 0; row < ROWS; row++)
{
for(int col = 0; col < COLS; col++)
{
System.out.print("Enter Account");
chargeAcc[row][col] = keyboard.nextInt();
}
}
System.out.print("Enter an account to be Charged");
int input = keyboard.nextInt();
int results = SequentialSearch(chargeAcc,input);
if(results ==-1)
{
System.out.println("that is an invalid #");
}
else
{
System.out.println("the # is valid");
}
}
public static int SequentialSearch(int[][] array, int value)
{
int rowIndex = 0;
int element = -1;
boolean found = false;
while(!found && index1 != value)
{
for(int i =0;i<array[index].length;i++)
{
if(array[index][i] == value)
{
found = true;
element = index;
}
}
index++;
}
return element;
}
for each row you search in all of it's cols.
(you could break the for loop if found, to gain efficiency.
Related
I'm trying to make a lottery game in java to run in the console afterwards with user input. I have a [3][9] array of random numbers between 1-9 in column 1, 10-19 in column 2, until 90, with half the numbers being 0, meaning they aren't part of the game or simply blanks.
So far, I have the numbers created in the array and they output fine, but I need to allow the user to have input and the numbers being guessed to start as blanks (or x instead of the number) and when the user actually gets the right number, it would switch that with the number generated previously. This would repeat itself until all the numbers were right, and then a message indicating a win would show.
How can I compare the inputs with the values generated? And how do I hide these values until they are guessed by the user?
Final Edit: If a line is completed with correct numbers, how do I keep track of this to also display message if this happens?
This is the random array index:
import java.util.Arrays;
import java.util.Random;
class Loto {
public static void main(String[] args) {
int[][] cartao = new int[3][9];
Random rand = new Random();
for(int i = 0; i< cartao.length; i++){
for(int j = 0; j< 5; j++){
int x = rand.nextInt(89) + 1;
while(cartao[i][x / 10] !=0) {
x = rand.nextInt(89) + 1;
}
cartao[i][x / 10] = x;
}
}
for(int[] row : cartao){
System.out.println(Arrays.toString(row));
}
}
}
You have to store what has been guessed correctly or not. For example you can use an auxiliary boolean matrix though it is not necessary (u can use only your card array, storing correctly guessed guesses has -1 for example), but it is easier to the eye I would say
public class Lotto {
boolean correctlyGuessed[][];
int lottoCard[][];
public Lotto() {
correctlyGuessed = new boolean[3][9];
lottoCard = new int[3][9];
Random rand = new Random();
for(int i = 0; i< lottoCard.length; i++){
for(int j = 0; j< 5; j++){
int x = rand.nextInt(89) + 1;
while(lottoCard[i][x / 10] !=0) {
x = rand.nextInt(89) + 1;
}
lottoCard[i][x / 10] = x;
}
}
}
public boolean guess(int row, int col, int number) {
if(lottoCard[row][col] == number) {
correctlyGuessed[row][col] = true;
return true;
}
return false;
}
public boolean hasLottoEnded() {
for(boolean[] arr:correctlyGuessed) {
for(boolean guess: arr) {
if(!guess) //if a guess is still false the game hasn't ended
return false;
}
}
return true;
}
public int getNumber(int row, int col) {
return lottoCard[row][col];
}
#Override
public String toString() {
StringBuilder sb = new StringBuilder();
for(int row = 0; row < lottoCard.length; row++) {
for(int col = 0; col < lottoCard[row].length; col++) {
if(correctlyGuessed[row][col])
sb.append(lottoCard[row][col] + " ");
else
sb.append("X ");
}
//spacing
sb.append("\n");
}
return sb.toString();
}
public static void main(String[] args) {
Lotto card = new Lotto();
Scanner sc = new Scanner(System.in);
while(!card.hasLottoEnded()) { //loop whilst the game hasn't ended
System.out.println("Row:");
int row = sc.nextInt();
System.out.println("Column:");
int col = sc.nextInt();
System.out.println("Number:");
int number = sc.nextInt();
if(card.guess(row, col, number))
System.out.println("You have guessed correctly!");
else
System.out.println("Wrong guess :(");
System.out.println(card);
}
System.out.println("You win!");
sc.close();
}
}
my intend is to use simplest java (array and loops) to generate random numbers without duplicate...but the output turns out to be 10 repeating numbers, and I cannot figure out why.
Here is my code:
int[] number = new int[10];
int count = 0;
int num;
while (count < number.length) {
num = r.nextInt(21);
boolean repeat = false;
do {
for (int i=0; i<number.length; i++) {
if (num == number[i]) {
repeat = true;
} else if (num != number[i] && i == count) {
number[count] = num;
count++;
repeat = true;
}
}
} while (!repeat);
}
for (int j = 0; j < number.length; j++) {
System.out.print(number[j] + " ");
}
How about you use a Set instead? If you also want to keep track of the order of insertion you can use a LinkedHashSet.
Random r = new Random();
Set<Integer> uniqueNumbers = new HashSet<>();
while (uniqueNumbers.size()<10){
uniqueNumbers.add(r.nextInt(21));
}
for (Integer i : uniqueNumbers){
System.out.print(i+" ");
}
A Set in java is like an Array or an ArrayList except it handles duplicates for you. It will only add the Integer to the set if it doesn't already exist in the set. The class Set has similar methods to the Array that you can utilize. For example Set.size() is equivalent to the Array.length and Set.add(Integer) is semi-equivalent to Array[index] = value. Sets do not keep track of insertion order so they do not have an index. It is a very powerful tool in Java once you learn about it. ;)
Hope this helps!
You need to break out of the for loop if either of the conditions are met.
int[] number = new int[10];
int count=0;
int num;
Random r = new Random();
while(count<number.length){
num = r.nextInt(21);
boolean repeat=false;
do{
for(int i=0; i<number.length; i++){
if(num==number[i]){
repeat=true;
break;
}
else if(i==count){
number[count]=num;
count++;
repeat=true;
break;
}
}
}while(!repeat);
}
for(int j=0;j<number.length;j++){
System.out.print(number[j]+" ");
}
This will make YOUR code work but #gonzo proposed a better solution.
Your code will break the while loop under the condition: num == number[i].
This means that if the pseudo-generated number is equal to that positions value (the default int in java is 0), then the code will end execution.
On the second conditional, the expression num != number[i] is always true (otherwise the code would have entered the previous if), but, on the first run, when i == count (or i=0, and count=0) the repeat=true breaks the loop, and nothing else would happen, rendering the output something such as
0 0 0 0 0 0...
Try this:
int[] number = new int[10];
java.util.Random r = new java.util.Random();
for(int i=0; i<number.length; i++){
boolean repeat=false;
do{
repeat=false;
int num = r.nextInt(21);
for(int j=0; j<number.length; j++){
if(number[j]==num){
repeat=true;
}
}
if(!repeat) number[i]=num;
}while(repeat);
}
for (int k = 0; k < number.length; k++) {
System.out.print(number[k] + " ");
}
System.out.println();
Test it here.
I believe the problem is much easier to solve. You could use a List to check if the number has been generated or not (uniqueness). Here is a working block of code.
int count=0;
int num;
Random r = new Random();
List<Integer> numbers = new ArrayList<Integer>();
while (count<10) {
num = r.nextInt(21);
if(!numbers.contains(num) ) {
numbers.add(num);
count++;
}
}
for(int j=0;j<10;j++){
System.out.print(numbers.get(j)+" ");
}
}
Let's start with the most simple approach, putting 10 random - potentially duplicated - numbers into an array:
public class NonUniqueRandoms
{
public static void main(String[] args)
{
int[] number = new int[10];
int count = 0;
while (count < number.length) {
// Use ThreadLocalRandom so this is a contained compilable unit
number[count++] = ThreadLocalRandom.current().nextInt(21);
}
for (int j = 0; j < number.length; j++) {
System.out.println(number[j]);
}
}
}
So that gets you most of the way there, the only thing you know have to do is pick a number and check your array:
public class UniqueRandoms
{
public static void main(String[] args)
{
int[] number = new int[10];
int count = 0;
while (count < number.length) {
// Use ThreadLocalRandom so this is a contained compilable unit
int candidate = ThreadLocalRandom.current().nextInt(21);
// Is candidate in our array already?
boolean exists = false;
for (int i = 0; i < count; i++) {
if (number[i] == candidate) {
exists = true;
break;
}
}
// We didn't find it, so we're good to add it to the array
if (!exists) {
number[count++] = candidate;
}
}
for (int j = 0; j < number.length; j++) {
System.out.println(number[j]);
}
}
}
The problem is with your inner 'for' loop. Once the program finds a unique integer, it adds the integer to the array and then increments the count. On the next loop iteration, the new integer will be added again because (num != number[i] && i == count), eventually filling up the array with the same integer. The for loop needs to exit after adding the unique integer the first time.
But if we look at the construction more deeply, we see that the inner for loop is entirely unnecessary.
See the code below.
import java.util.*;
public class RandomDemo {
public static void main( String args[] ){
// create random object
Random r = new Random();
int[] number = new int[10];
int count = 0;
int num;
while (count < number.length) {
num = r.nextInt(21);
boolean repeat = false;
int i=0;
do {
if (num == number[i]) {
repeat = true;
} else if (num != number[i] && i == count) {
number[count] = num;
count++;
repeat = true;
}
i++;
} while (!repeat && i < number.length);
}
for (int j = 0; j < number.length; j++) {
System.out.print(number[j] + " ");
}
}
}
This would be my approach.
import java.util.Random;
public class uniquerandom {
public static void main(String[] args) {
Random rnd = new Random();
int qask[]=new int[10];
int it,i,t=0,in,flag;
for(it=0;;it++)
{
i=rnd.nextInt(11);
flag=0;
for(in=0;in<qask.length;in++)
{
if(i==qask[in])
{
flag=1;
break;
}
}
if(flag!=1)
{
qask[t++]=i;
}
if(t==10)
break;
}
for(it=0;it<qask.length;it++)
System.out.println(qask[it]);
}}
public String pickStringElement(ArrayList list, int... howMany) {
int counter = howMany.length > 0 ? howMany[0] : 1;
String returnString = "";
ArrayList previousVal = new ArrayList()
for (int i = 1; i <= counter; i++) {
Random rand = new Random()
for(int j=1; j <=list.size(); j++){
int newRand = rand.nextInt(list.size())
if (!previousVal.contains(newRand)){
previousVal.add(newRand)
returnString = returnString + (i>1 ? ", " + list.get(newRand) :list.get(newRand))
break
}
}
}
return returnString;
}
Create simple method and call it where you require-
private List<Integer> q_list = new ArrayList<>(); //declare list integer type
private void checkList(int size)
{
position = getRandom(list.size()); //generating random value less than size
if(q_list.contains(position)) { // check if list contains position
checkList(size); /// if it contains call checkList method again
}
else
{
q_list.add(position); // else add the position in the list
playAnimation(tv_questions, 0, list.get(position).getQuestion()); // task you want to perform after getting value
}
}
for getting random value this method is being called-
public static int getRandom(int max){
return (int) (Math.random()*max);
}
I'm trying to write a program that displays all prime numbers below the one entered by the user. The only requirement is that it must be multi threaded. This is my first time using Java and multiple threads. Can you help? It compiles, but the output is strange. Maybe it's an address?
import java.util.Scanner;
import java.util.Arrays;
public class prime {
public static void main(String[] args){
// get number from user
System.out.println("Enter a number: ");
Scanner keyboard = new Scanner(System.in);
int num = keyboard.nextInt();
RunPrime runprime1 = new RunPrime (num);
runprime1.start();
Thread.yield();
runprime1.SmallerPrimeNumbers();
}
}
class RunPrime extends Thread {
private int given_number;
RunPrime (int n) {
given_number = n;
}
public void SmallerPrimeNumbers() {
int count = 0;
for (int i = 0; i <= given_number; i++) {
if (CheckPrime(i)) {
count++;
}
}
for (int i = 0; i < count; i++) {
for (int j = 2; j <= given_number; j++) {
if (CheckPrime(j)) {
number[i] = j;
}
}
}
System.out.println(number);
}
public static boolean CheckPrime (int n) {
for (int i=2 ; i<n ; i++) {
if (n%i == 0)
return false;
}
return true;
}
}
Your SmallerPrimeNumbers() function has a second for-loop that doesn't look necessary. You're also assigning and printing the variable number that wasn't declared anywhere (this is probably what's causing you trouble). Since you're only printing them and not saving them, you can simplify the function like this:
public void SmallerPrimeNumbers() {
int count = 0;
for (int i = 0; i <= given_number; i++) {
if (CheckPrime(i)) {
System.out.println(i);
}
}
}
I'm doing a magicsquare program that allows a user to input numbers >0 to form a magicsquare. What a magic square is, is pretty much a square, meaning that n has to have n(squared) numbers. Much like ticTacToe, all the rows, columns, and diagonals each have the same sum to be considered a magic square When I run my program, It always confuses the 2D array set and claim that the set of numbers are a magicsquare when usually, it isnt necessarily so. Please help!
import java.util.Scanner;
public class SquareRunner
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
Square test = new Square();
System.out.println("Enter a row of integers. When you are finished, type 'n' in a new line");
boolean flag = false;
while(!flag)
{
String numbers = in.next();
if(numbers.equals("n"))
flag = true;
else
test.add(numbers);
}
test.isMagic();
}
}
public class Square
{
private int[][] values;
private int row;
public Square()
{
row = 0;
}
public void add(String numbers)
{
int b = 1;
int amount = numbers.length();
values = new int[amount][amount];
for(int j =0;j<amount;j++)
{
String a = numbers.substring(j,b);
int convert = Integer.parseInt(a);
values[row][j] = convert;
b++;
}
row++;
}
public Boolean isMagic()
{
int checkAmountColumns = values[0].length;
int checkAmountRows = values.length;
int isSquare = checkAmountColumns * checkAmountRows;
for(int q = 0;q<values.length;q++)
{
for(int w=0;w<values[0].length;w++)
{
int checkZero = values[q][w];
if(checkZero == 0)
{
System.out.print("To be a perfect square, your number of rows and columns, n must be a perfect ");
System.out.println("Square i.e. 9 total numbers is 3 numbers per row");
return false;
}
}
}
if(checkAmountColumns != checkAmountRows || Math.sqrt(isSquare) != checkAmountColumns)
{
System.out.print("To be a perfect square, your number of rows and columns, n must be a perfect ");
System.out.println("Square i.e. 9 total numbers is 3 numbers per row");
return false;
}
else
{
int magicNumber = 0;
int counter = 0;
int compareTo = 0;
//row to row
for(int i =0;i<values.length;i++)
{
for(int j = 0;j<values[0].length;j++)
{
values[i][j] += compareTo;
if(counter == 0)
values[i][j] += magicNumber;
}
counter ++;
compareTo = 0;
if(compareTo != magicNumber)
{
System.out.println("This Selection of numbers is not a perfect square");
return false;
}
}
//column to column
for(int i =0;i<values[0].length;i++)
{
for(int j = 0;j<values.length;j++)
{
values[j][i] += compareTo;
if(counter == 0)
values[j][i] += magicNumber;
}
counter ++;
compareTo = 0;
if(compareTo != magicNumber)
{
System.out.println("This Selection of numbers is not a perfect square");
return false;
}
}
System.out.println("This selection of numbers is a MagicSquare!");
return true;
}
}
}
The first thing I notice is that your add() method probably won't work correctly. Every time you call it, you overwrite the previous values member with a newly allocated array. This throws away the previous row that you entered.
Same answer as Greg.
Add this to your add method and delete the initialisation of your values array.
if(values == null){values = new int[amount][amount];}
How do I create a method that takes a two-dimensional array as a parameter and displays the index of the row with the most zeros? The program I have does compile. It just displays an incorrect result. The method countZeros() counts the number of zeros in each row. I need to compare each count with the next, so I created count and count2. The location of the greater count will be stored in rowNum. I'm not sure what I am doing wrong. I think it may be indexing incorrectly.
Here is my code:
public class P118
{
public static void main(String[]args)
{
int[][]num = {{0,3,6,0,0}, {1,3,8,9,8}, {9,9,9,0,8}, {3,7,9,9,9}};
System.out.print(rowWithMostZeros(num));
}
public static int rowWithMostZeros(int[][]arr)
{
int count = 0, count2 = 0, rowNum = -1;
for(int row = 0; row<arr.length;row++)
{
count = countZeros(arr[row]);
if(count>count2)
{
rowNum = row;
}
}
for(int i=0; i<arr.length;i++)
{
count2 = countZeros(arr[i]);
}
return rowNum;
}
public static int countZeros(int[]x)
{
int count = 0;
for(int i = 0; i<x.length;i++)
{
if(x[i]==0)
{
count++;
}
}
return count;
}
}
Try this:
public static int rowWithMostZeros(int[][] arr) {
if (arr == null || arr.length < 1) {
return -1;
}
int rowWithMostZeros = 0;
int count = countZeros(arr[0]);
for (int i = 1; i < arr.length; i++) {
int count2 = countZeros(arr[i]);
if (count2 > count) {
rowWithMostZeros = i;
}
}
return rowWithMostZeros;
}
public static int countZeros(int[] arr) {
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 0) {
count += 1;
}
}
return count;
}
Any code that counts occurrences could work with the following method:
Start with with either using the first row, or invalid data as the 'max'
Check if the other values are greater. If they are, replace
the 'max' count ( the amount) and the 'row number' with that one.
Here is the code that first sets the values to the first row being the max, and then checks if any of the other rows have more zeros.:
public static int rowWithMostZeros(int[][]arr)
{
int mostZeroCount = countZeros(arr[0];
int rowNum = 0;
for(int row = 1; row<arr.length;row++)
{
int count = countZeros(arr[row]);
if(count>mostZeroCount)
{
rowNum = row;
mostZeroCount = count;
}
}
return rowNum;
}
The problem with the original method was that it always set count to be the next value which had more then one 0, without comparing to the actual maximum amount of 0's previously seen. Therefore, at the end, it would simply return the last row that had at least one 0 (Which in this case was Row 2, the third row)
You should use the variable count2 to store the maximum number of zeros in all rows so far, in order to compare it with the next row. In order to do this, you need to assign it the current count inside the if block, in case the "count" is bigger than count2. I have redesigned your code below using maxCount and currentCount, for extra clarity. The second loop in your code is unnecessary, I didn't really understood why you did it.
public static int rowWithMostZeros(int[][]arr){
int currentCount = 0, maxCount = 0, rowNum = -1;
for(int row = 0; row maxCount){
maxCount = currentCount;
rowNum = row;
}
return rowNum;
}
public static int countZeros(int[]x){
int count = 0;
for(int i = 0; i<x.length;i++){
if(x[i]==0){
count++;
}
}
return count;
}