Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners HELP WITH ARRAY
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


danjim deleon
HELP WITH ARRAY
4 Answer(s)      a year and 10 months ago
Posted in : Java Beginners


Hi i would like this program:

public class ArrayApp { public static void main(String[]args) {

    int [] arr;
    arr= new int[100];
    int nItems=0;
    int j;
    int searchId;

    arr[0]= 11;
    arr[1]= 22;
    arr[2]= 33;
    arr[3]= 44;
    arr[4]= 55;
    arr[5]= 66;
    arr[6]= 77;
    arr[7]= 88;
    arr[8]= 99;
    arr[9]= 00;
    nItems= 10;

    for (j=0; j<nItems; j++)              
    System.out.print(arr[j] + " ");
    System.out.println(" ");

    searchId = 66;                       
    for (j=0; j<nItems; j++)           
    if (arr[j] == searchId)           
    break;                         
    if (j == nItems)                  
    System.out.println("Can't find" + searchId); 
    else
        System.out.print("Found" + searchId);   

        searchId= 55;                           
            for(j=0; j<nItems; j++)      
                if(arr[j] == searchId)
                    break;
                    for(int k=j; k>nItems; k--)       
                        arr[k] = arr[k+1];
                        nItems--;               

                        for(j=0; j<=nItems; j++)      
                        System.out.print( arr[j] + " ");
                        System.out.println(" ");




}

} have the user input a number in the boxes.. if the number that the user input didn't match match with the number in the array boxes.. the output should be "Invalid Input" if it matches the number in the array boxes ([]) it should find the number and print it "Found (number)" pls. help me with this :)


Advertisement
View Answers

November 22, 2012 at 5:42 PM


Here is a code that input a number from user and find whether it exists in the array of numbers or not.

import java.util.*;

public class ArrayApp { 
    public static void main(String[]args) {

    int [] arr;
    arr= new int[100];
    int nItems=0;
    int j;
    arr[0]= 11;
    arr[1]= 22;
    arr[2]= 33;
    arr[3]= 44;
    arr[4]= 55;
    arr[5]= 66;
    arr[6]= 77;
    arr[7]= 88;
    arr[8]= 99;
    arr[9]= 00;
    nItems= 10;

    for (j=0; j<nItems; j++)              
    System.out.print(arr[j] + " ");
    System.out.println(" ");
    Scanner input=new Scanner(System.in);
    System.out.print("Enter number to search: ");
    int searchId = input.nextInt();                      
    for (j=0; j<nItems; j++)           
    if (arr[j] == searchId)           
    break;                         
    if (j == nItems)                  
    System.out.println("Can't find: " + searchId); 
    else
        System.out.print("Found" + searchId);   

        searchId= 55;                           
            for(j=0; j<nItems; j++)      
                if(arr[j] == searchId)
                    break;
                    for(int k=j; k>nItems; k--)       
                        arr[k] = arr[k+1];
                        nItems--;               

                        for(j=0; j<=nItems; j++)      
                        System.out.print( arr[j] + " ");
                        System.out.println(" ");
    }
}


November 22, 2012 at 8:42 PM


Hello i already solved the problem my self.. But i didn't use scanner because i don't know it's use. so i just replace the searchId=66; with an input.reaLine And it works.. maybe you can explain what is the use of the scanner that u have put in there thanks



November 24, 2012 at 12:30 PM


The Scanner class is a class in java.util, which allows the user to read values of various types. There are far more methods in class Scanner than you will need in this course. We only cover a small useful subset, ones that allow us to read in numeric values from either the keyboard or file without having to convert them from strings and determine if there are more values to be read.



November 24, 2012 at 2:47 PM


Well, Thanks! :D the only one i need to knows is.. when i run the program i have made the output is this:

11 22 33 44 55 66 77 88 99 00 Enter number: 99 Found: 99 11 22 33 44 55 66 77 88 00

I know the program deleted the value i enter it is because it is made to do so.. but i don't know how does it work.. as you can see.. the first program i posted needs to be edited so the user can input a number and delete it in the array, so how come the program delete the number that the user input? I need to know that because no one in our class can answer the question.. I know that the deletion part happens in the lower part of the program which is the "search= 55;" is located but i need more information. Please help me :)



Related Tutorials/Questions & Answers:
HELP WITH ARRAY
HELP WITH ARRAY  Hi i would like this program: public class ArrayApp... that the user input didn't match match with the number in the array boxes.. the output should be "Invalid Input" if it matches the number in the array boxes
Help With an Array
Help With an Array  So what Im trying to do is write an array... trying to do is almost combine the two to create a larger array declaration... not figure out how to finish the code. Please HELP! A) students? names
Advertisements
Help With an Array
Help With an Array  // ******************** // Sales.java // // Reads... need help from this part on. I need that part corrected and add a portion..."'s that exceeded that number. Any help I would greatly appreciate. Thank you
Netbeans Array help
of rolls possible within the array Help plese!   Hi Friend, Try...Netbeans Array help  Ok here is my code- * * To change this template... array to tally the actual combinations of rolls. A roll of 2 and 7 would
Need help in constructing a 2 dimensionla array?
Need help in constructing a 2 dimensionla array?  Construct a 2 dimensional array that for each entry in the array, display the index of the first... array. import java.util.*; public class twoDimension{ public static void main
please help me to solve this question about array
please help me to solve this question about array  write a program thatt stores vowels (a,e,i,o and u)in an array. Ask the user to enter any character. The program should ignore the case of that character (uppercase or lowercase
How to create a long array with the help of long buffer.
How to create a long array with the help of long buffer.  In this tutorial, we will see how to create a long array with the help of long buffer...; long[] array() The array() method returns long array based
Create a int array with the help of int buffer.
Create a int array with the help of int buffer.  In this tutorial, we will see how to create a int array with the help of int buffer. IntBuffer API...[] array() The array() method returns int array based on int buffer
Create a short array with the help of short buffer.
Create a short array with the help of short buffer.  In this tutorial, we will see how to create a short array with the help of short buffer...; short[] array() The array() method returns short array based
Generating Random Numbers to Fill array. Java Beginner needing help!
Generating Random Numbers to Fill array. Java Beginner needing help!  ... methods: // Displays the array permutatedNumbers to the console public void... the permutated array public void generatePermutation(){} Make an array to hold
cannot find symbol class array queue--plzz somebody help..
cannot find symbol class array queue--plzz somebody help..  import java.util.*; public class Test { public static void main(String[] args) { ArrayQueue<String> q = new ArrayQueue<String>(6); System.out.println
in_array
in_array  in_array in php
is _array()
is _array()  is_array() in php   Hi Friend, This function is of Boolean type.It checks whether a variable is an array or not. Here is an example: <?php $yes = array('Hello', 'World'); echo is_array($yes) ? 'Array
is _array()
is _array()  is _array()   Hi Friend, This function is of Boolean type.It checks whether a variable is an array or not. Here is an example: <?php $yes = array('Hello', 'World'); echo is_array($yes) ? 'Array
Array's
Array's  I have to finish an "order page". that checks 2 dropdown... line at a time, then calculate a total. I know I need to create an array, but I'm having a hard time figuring out the loop. I just need a little help to get
need help with two dimensional array that takes input from user and bubble sorts and selections sorts
need help with two dimensional array that takes input from user and bubble... cannot figure out how i can declare a two dimensional array without knowing how... many words he/she types without using an array list.   Thank you. I
Array
Array  how can i use elements of an array in a circular form? that is if the searching of the element reach the last elements of the array, then it will start serching from the begining of the array
help
help  i need help with this code. write a java code for a method named addSevenToKthElement that takes an integer array, and an integer k as its arguments and returns the kth element plus 7. any help would be greatly
Array
Array  is it possible to define array like this..? int[] intArray = new int[] {4,5,6,7,8}; Explain...?   Yes, you can. Java Initialize Array
Array
Array  What if i will not declare the limit index of an array, how will I declare an array and store values with it using loop?   Hi Friend...(System.in); int array[]=new int[5]; System.out.println("Enter Array Elements
array
array  write and test a function named mirror that is passed an array of n floats and returns a newly created array that contains those n floats... the array {10.1,11.2,8.3,7.5,22} into{22,7.5,8.3,11.2,10.1
array
array  write and test a function named mirror that is passed an array of n floats and returns a newly created array that contains those n floats... the array {10.1,11.2,8.3,7.5,22} into{22,7.5,8.3,11.2,10.1
array
array  write and test a function named mirror that is passed an array of n floats and returns a newly created array that contains those n floats... the array {10.1,11.2,8.3,7.5,22} into{22,7.5,8.3,11.2,10.1
Array
Array  How do i insert elements into an array up to a limit from...("Enter Range: "); int size=input.nextInt(); int array[]=new int[size]; System.out.println("Enter Array Elements: "); for(int i=0;i<
array
array  WAP in java to store 6 element in array P and 4 element in array Q. Produce the third arra y R containing all element from p & q
array
array  array memory allocation is dynamic or static in java   Java Arrays have dynamic memory allocation
Array
Array  can we create an array of size 1 lakh in java programming
array
accepts a pointer to integer which represents an array of integer.After that this method prints the entire of the array numbers to the monitor. include using std::cout; using std::endl; void printArray(int *array, int count
array
array  take a 2d array and display all its elements in a matrix fome using only one for loop and ple explain the program in below
array
array  create an array in which no element is duplicate. if duplicate then tell to user duplicate. duplicacy is tell to user during when he enter the element
array
array  create an array in which no element is duplicate. if duplicate then tell to user duplicate. duplicacy is tell to user during when he enter the element
Advertisements
 

 

 

DMCA.com