I want it to take 10 values from the keyboard and find out if any of the 10 inputs contain 0 or 1 and if so, What position is in the array?
example
Input = 9 15 91 1 0 22 31 67 88 33
output = 4 found number 1, at position 2 3 4 7
1 found number 0, at position 5
5 found others, at position 1 6 8 9 10
I can't write any further because I still don't understand. Advise me please I tried to write it but the output is still not correct.
public static int SequentialSearch(int number[], int key_1, int key_0) {
int looker;
for (looker = 0; looker < number.length; looker ) {
if (number[looker] == key_1)
return looker;
if (number[looker] == key_0)
return looker;
}
return -1;
}
public static void Loopcheck(int number[]) {
int key_1, key_0, others_key;
for (int count_check = 0; count_check < number.length; count_check ) {
if (number[count_check] / 10 == 1 || number[count_check] % 10 == 1) {
key_1 = 1;
break;
} else if (number[count_check] / 10 == 0 || number[count_check] % 10 == 0) {
key_0 = 0;
break;
}
}
}
public static int Print(int number[], int location) {
for (int loop = 0; loop < number.length; loop )
if (location > -1)
System.out.print(" 0 : " location);
return 0;
}
public static void main(String[] args) {
Scanner Sc = new Scanner(System.in);
int value1, value0, location, key1;
int[] number = new int[10];
for (int count = 0; count < number.length; count ) {
number[count] = Sc.nextInt();
}
int item1 = 1;
int item0 = 0;
location = SequentialSearch(number, item1, item0);
Loopcheck(number);
Print(number, item1);
}
}
CodePudding user response:
Since you are looking for a specific character, I would recommend working on String or char array instead. Some code you can consider that will probably give you an idea how to solve a problem:
Scanner sc= new Scanner(System.in); //System.in is a standard input stream
System.out.print("Enter first number- ");
int a= sc.nextInt();
System.out.print("Enter second number- ");
int b= sc.nextInt();
System.out.print("Enter third number- ");
int c= sc.nextInt();
String Input = String.join(" ",Integer.toString(a),Integer.toString(b),Integer.toString(c));
int i = 0;
System.out.println(Input);
while(i<Input.length()){
if(Input.charAt(i)=='0') System.out.println(String.join(" ","0 at position",Integer.toString(i 1)));
if(Input.charAt(i)=='1') System.out.println(String.join(" ","1 at position",Integer.toString(i 1)));
i ;
}
CodePudding user response:
The most impactful advice I would provide is:
store your input as string
or char[]
instead of int[]
.
To solve: Create a collection(like a list, or array) to hold your valid indexes, and iterate through your input one letter at a time, adding valid indexes to your collection as they satisfy your condition. Implement a 'PrettyPrint()' that converts your collection into a nice output.
CodePudding user response:
you can use a method like this,
public void haszero(int numbers[])
{
int position;
for(position = 0; position < numbers.size; position )
{
while(numbers[position] > 0)
{
if(numbers[position] % 10 == 0)
system.out.print("0 at " position)
number=number/10;
}
}
}
and then you can use same method as this for 1. or the you can also do something like this
for(int position = 0; position < array.size; position )
{
if (String.valueOf(array[position]).contains("0"))
system.out.print("0 at " position);
}
CodePudding user response:
I went ahead a coded a solution that used int arrays. Here are the test results from one of my later tests.
Type 10 values: 0 1 2 3 4 5 6 7 8 9
1 found number 1, at position 2
1 found number 0, at position 1
8 found others, at position 3 4 5 6 7 8 9 10
Type 10 values: 12 23 34 45 127 21 84 0 73 364
3 found number 1, at position 1 5 6
1 found number 0, at position 8
6 found others, at position 2 3 4 7 9 10
Type 10 values:
To exit the program, you just press the Enter key.
My process was to maintain three int arrays. One held the indexes of all the ones. One held the indexes of all the zeros. One held the indexes of all the other values.
I wrote this code step by step, testing each step along the way. I probably ran two or three dozen tests, each testing one small part of the code.
The first thing I did was to get the input loop working correctly. I didn't test for non-numeric input, but that test could be added easily. I didn't limit the input to 10 numbers either. You can type 15 or 20 numbers if you want. Finally, I didn't limit the input to two-digit numbers. The code that looks for a digit should work for any positive integer value.
Next, I wrote a method to determine whether a number contained a particular digit. The method works with any digit, not just zero or one.
After that, it was a matter of getting the output to look correct.
Here's the complete runnable code.
import java.util.Scanner;
public class ZeroAndOne {
public static void main(String[] args) {
new ZeroAndOne().processInput();
}
public void processInput() {
Scanner scanner = new Scanner(System.in);
String line;
do {
System.out.print("Type 10 values: ");
line = scanner.nextLine().trim();
String[] parts = line.split("\\s ");
if (!line.isEmpty()) {
int[] input = new int[parts.length];
for (int index = 0; index < parts.length; index ) {
input[index] = Integer.valueOf(parts[index]);
}
System.out.println(processArray(input));
}
} while (!line.isEmpty());
scanner.close();
}
private String processArray(int[] input) {
int[] zeros = new int[input.length];
int[] ones = new int[input.length];
int[] other = new int[input.length];
int zeroIndex = 0;
int oneIndex = 0;
int otherIndex = 0;
for (int index = 0; index < input.length; index ) {
boolean isOther = true;
if (isDigit(input[index], 0)) {
zeros[zeroIndex ] = index;
isOther = false;
}
if (isDigit(input[index], 1)) {
ones[oneIndex ] = index;
isOther = false;
}
if (isOther) {
other[otherIndex ] = index;
}
}
StringBuilder builder = new StringBuilder();
builder.append(oneIndex);
builder.append(" found number 1, at position ");
builder.append(appendIndexes(ones, oneIndex));
builder.append(System.lineSeparator());
builder.append(zeroIndex);
builder.append(" found number 0, at position ");
builder.append(appendIndexes(zeros, zeroIndex));
builder.append(System.lineSeparator());
builder.append(otherIndex);
builder.append(" found others, at position ");
builder.append(appendIndexes(other, otherIndex));
builder.append(System.lineSeparator());
return builder.toString();
}
private boolean isDigit(int value, int digit) {
if (value == 0 && digit == 0) {
return true;
}
while (value > 0) {
int temp = value / 10;
int remainder = value % 10;
if (remainder == digit) {
return true;
}
value = temp;
}
return false;
}
private StringBuilder appendIndexes(int[] array, int length) {
StringBuilder builder = new StringBuilder();
for (int index = 0; index < length; index ) {
builder.append(array[index] 1);
if (index < (length - 1)) {
builder.append(" ");
}
}
return builder;
}
}