2

Your program should display whether the inputted integer value is found in list, how many of it is in list, and what are their locations in list? sample

List : 1 3 2 5 7 8 5 6 9 4

Input value to search in List: 9
The value 9 is in List!
There are 4 of it in List.
Located at: list[4], list[5], list[6], list[8]

this is my code

import java.io.*; 
public class List{ 
  public static void main(String[] args){ 
    int list[] = new int[10]; 
    int i, num = 0, num1=0; 
    String input = " "; 
    BufferedReader in = new BufferedReader(new 
                               InputStreamReader(System.in)); 

    for(i = 0; i < 10; i++){ 
      list[i] = 0; 
    } 
    for(i = 0; i < 10; i++){ 
      System.out.print("Input value for list[" + i + "] = "); 
      try{ 
        input = in.readLine();         
        num = Integer.parseInt(input); 
        list[i] = num; 
      for(i = 0; i < 10; i++){ 
        System.out.println("list[" + i + "] = " +list[i]); 
      } 
      for (int b = 0; b < list.length; ++b) {
        if (num1 == list[b]) {
          returnvalue = b;
          break;
        }
      }
      System.out.print("Input value for list");
      input = in.readLine();
      num1 = Integer.parseInt(input);
    }catch(IOException e){} 
      System.out.println("the position is" + returnvalue);
    }
  } 
} 

I think the position that is returning is not accurate can you help me?

Hello23
  • 37
  • 2
  • 8

3 Answers3

0

This will do what you want... Modify the System.out.println(); part according to your need.

public static void main(String[] args) throws IOException {
int list[] = new int[10];
int i, num = 0, num1 = 0;
int returnvalue = 0;
String input = " ";
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

for (i = 0; i < 10; i++) {
    list[i] = 0;
    }
for (i = 0; i < 10; i++) {
    System.out.print("Input value for list[" + i + "] = ");    
    input = in.readLine();
    num = Integer.parseInt(input);
    list[i] = num;
    }

for (i = 0; i < 10; i++) {
    System.out.println("list[" + i + "] = " + list[i]);
    }
System.out.print("Input value for checking in  list ");
input = in.readLine();
num1 = Integer.parseInt(input);

boolean flag = false;
int[] arr = new int[10];
int count= 0;
for (int b = 0; b < list.length; ++b) {
  if (num1 == list[b]) {              
      flag = true;
      arr[count]=b;
      count++;
      }
}
if(flag)
{
   System.out.println("The value "+num1+" is in List!");
   System.out.println("There are "+count+" of it in List");
   System.out.print("Located at: ");
   for(int j=0; j<count;j++)
   {
    System.out.print(" List["+arr[j]+"]");
   }
} 
else {
System.out.print(" Element Not found");
}

}

The Console Part for input output is...

Input value for list[0] = 4
Input value for list[1] = 5
Input value for list[2] = 6
Input value for list[3] = 4
Input value for list[4] = 5
Input value for list[5] = 6
Input value for list[6] = 5
Input value for list[7] = 4
Input value for list[8] = 4
Input value for list[9] = 6
list[0] = 4
list[1] = 5
list[2] = 6
list[3] = 4
list[4] = 5
list[5] = 6
list[6] = 5
list[7] = 4
list[8] = 4
list[9] = 6
Input value for checking in  list 4
The value 4 is in List!
There are 4 of it in List.
Located at:List[0] List[3] List[7] List[8] 

Fixes in your code

  1. A try block is either with a catch block or with a finally block or both.
  2. Having two same variables in inner and outer loop is not good. In your case the outer loop will iterate just once. I think which is not needed.
CoderNeji
  • 2,056
  • 3
  • 20
  • 31
0

There are two nested for loop where the loop control variable is same:

for(i = 0; i < 10; i++){ 
    System.out.print("Input value for list[" + i + "] = "); 
    ... 
    for(i = 0; i < 10; i++){
         System.out.println("list[" + i + "] = " +list[i]);
    }
    ...

So, the outer for loop will never iterate what you are expecting. Use a different loop control variable for the inside for loop.

mazhar islam
  • 5,561
  • 3
  • 20
  • 41
0

here is my solution for your problem. no need to use that much of loops. you want to use only one foreach - How does the Java 'for each' loop work? (if you don't need to ask question again and agian). I was unable to test this lot :). But, I hope this will work.

import java.util.*;
class MyList{
public static void main(String arga[]){
    int array[] = {1,3,2,5,7,8,5,6,9,4};

    Scanner input = new Scanner(System.in);
    // recursively ask the question
    while(true){
        System.out.print("Enter the value to search in list: ");

        int value = input.nextInt();
        System.out.println();
        int i = 0;// to get the array index
        int count = 0; // to get how many 

        String list = "Located at: ";
        boolean isTrue = false; // to get entered value is in the list or not
        for(int a: array){
            if(a == value){
                isTrue = true;
                list += "list[" + i + "],";
                count++;
            }
            i++;
        }
        if(isTrue){
            //remove the last "," from the string
            list = list.substring(0, list.length() - 1);
            System.out.println("The value " + value +" is in List!");
            System.out.println("There are " + count +" of it in List.");
            System.out.println(list);
        }else{
            System.out.println("this value is not in the list");
        }
    }
}

}

Community
  • 1
  • 1
sushi
  • 139
  • 1
  • 9