+ 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

1st Feb 2022, 1:30 PM
Japheth
18 Answers
+ 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
1st Feb 2022, 1:57 PM
Jayakrishna 🇮🇳
+ 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...
1st Feb 2022, 1:44 PM
Jayakrishna 🇮🇳
+ 1
ooohhh thank you so muchhhhh
1st Feb 2022, 1:47 PM
Japheth
+ 1
I updated code after checking already.. You are using scanner instead of Scanner (capital S needed) .. Japheth Check again changes...
1st Feb 2022, 2:11 PM
Jayakrishna 🇮🇳
+ 1
ohh!! the 2nd scanner should also be lowercase edit: (i mean upper case!!)
1st Feb 2022, 2:13 PM
Japheth
+ 1
Manav Roy i always wanted to learn functions and the java progr. language in that way
24th Feb 2022, 3:29 PM
Japheth
+ 1
Manav Roy i noticed i think thats the main class? so it's okay to put it like on the end?
24th Feb 2022, 4:21 PM
Japheth
+ 1
i think i understood everything else
24th Feb 2022, 4:22 PM
Japheth
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
1st Feb 2022, 1:55 PM
Japheth
0
thank you so much u saved my lifee
1st Feb 2022, 2:04 PM
Japheth
0
there's a last error said. scanner can't be resolved to a type
1st Feb 2022, 2:09 PM
Japheth
0
Manav Roy i mean the main method is not on the top
25th Feb 2022, 4:07 AM
Japheth