+ 2
my activity. im still a beginner and i dont master scanner, how could i ask the user an input and make it a search id.
Activity 2 (50) 1. Create a class named ArrayApp with 20 elements. 2. The program prompts the user to input a number to search for and prints a message: “Found [search key]” if the number exists in the array elements, then print the remaining data on the next line eliminating the searched element. Otherwise, it prints: “[search key] not found”. this is how i tried https://code.sololearn.com/cFyka8PvK5Wm/?ref=app
18 Respuestas
+ 1
//
import java.util.Scanner;
public class laperajapheth {
public static void main(String[] args) {
int [] arr = new int [20] ;
int nItems = 20, j, searchID;
for(j=0;j<nItems;j++) //(1)
arr [ j ] = j*5+5;
for (j=0;j<nItems;j++)
System.out.print(arr[j] + " ");//(2)
Scanner input = new Scanner(System.in);
System.out.println("\nPlease enter number:");
int arraydel = input.nextInt();
input.close();
searchID = arraydel;
for (j = 0;j<nItems;j++) {
if (arr [ j ] == searchID)
break;
}
System.out.println();
if (j == nItems)
System.out.println("Can't find " + searchID );
else
{
System.out.println("Found " + searchID);
System.out.println("Deleting " + searchID);
for (int k = j;k<nItems-1;k++) {
arr [k] = arr [k+1];
}
nItems --;
}
for (j = 0;j<nItems;j++) {
System.out.print(arr [ j ] + " ");
}
}
}
//Japheth this is equalent to your's ,required code
+ 1
You can use Scanner object like this to take input :
Note : input required is in integer type so use
int searchId = input.nextInt() ;
Array values are int type so comparing int with string is false always... In are[j]==searchID
And you need to take this out of loop..
You can use a loop to assign values to array instead of lengthy manual writing...
edit: √
Hope it helps...
+ 1
ooohhh thank you so muchhhhh
+ 1
I updated code after checking already.. You are using scanner instead of Scanner (capital S needed) .. Japheth
Check again changes...
+ 1
ohh!! the 2nd scanner should also be lowercase
edit: (i mean upper case!!)
+ 1
Manav Roy i always wanted to learn functions and the java progr. language in that way
+ 1
Manav Roy
i noticed i think thats the main class? so it's okay to put it like on the end?
+ 1
i think i understood everything else
0
how should i do number 48 and 58. i mean, later i need to call out the user's input to perform the next ones
0
thank you so much u saved my lifee
0
there's a last error said. scanner can't be resolved to a type
0
Manav Roy i mean the main method is not on the top