user4220447
user4220447

Reputation:

not getting correct numbers in for loop

Hy there. Below you can see sagment of my code. So lets go to the problem. int i is not returning correct values and i cannot figure it out why.

LIST: [AGRFT, AGRFT, ARNES, ASCII, ASEAN, Aaron, Abdul, Abdul]

So for example. User inputs AS***, the program should return i is at 2. However i am getting i is at 0.

If i remember right it should go like this:

User_input= AS***

User_input.lenght() should be 5

first it should be user_input.charAt(0)=='*' NO

second it should be user_input.charAt(1)=='*' NO

third it should be user_input.charAt(2)=='*' YES

BREAK

i is at 2.

SO what am i missing?

I am getting 0.

Oh and also at

for(i=0; i < user_input.length();i++){

i am getting warning that i++ is Dead code?

 if (dolzina == 5) {

            for(i=0; i < user_input.length();i++){  
                if (user_input.charAt(i)=='*'); 
                break;
            }

            System.out.println("i is at "+ i);

this is my full code for refrence. What it does it reads from txt file add wor

public class proba {


public static void main(String[] args) {
    String izbira;
    int dolzina=0;
    int i=0;
    Scanner in = new Scanner(System.in);
    String user_input;
    Scanner input = new Scanner(System.in);
    String regex;
    List<String> list5 = new ArrayList<String>();
    int beseda;
    String prefix = null;
    try {

        File file = new File("sort.txt");
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String vrstica;

        while ((vrstica = bufferedReader.readLine()) != null) {

            if (vrstica.length() == 5) {
                list5.add(vrstica);

            }
        }
        System.out.println(list5);
        do{
            do {
                System.out.println("Enter lenght of word:");
                if (in.hasNextInt()) {
                    dolzina = in.nextInt();
                } else if (in.hasNextLine()) {
                    System.out.printf("Wrong entry!%n ",
                            in.nextLine());
                } 
            } while (dolzina <= 0);

        Collections.sort(list5);

        System.out.println("Enter a word for unknown character enter * :");
        user_input = input.nextLine();



        System.out.println("Sorted list: [length: " + list5.size() + "]");

        if (dolzina == 5) {

            for(i=0; i < user_input.length();i++){  
                if (user_input.charAt(i)=='*'); 
                break;
            }

            System.out.println("i je"+ i);
            prefix=user_input.substring(0,i);
            System.out.println(prefix);


        int start=binarySearchfirst(list5,prefix);
        int end=binarySearchlast(list5,prefix);
        System.out.println(start);
        System.out.println(end);
        for (int b=start;b<=end;b++)
        {
            user_input = user_input.replace("*", ".");
            String s = (String) list5.get(b);
            if (s.matches(user_input))
                System.out.println(s);
        }
        }
        dolzina=-1;
        System.out.println("Ponovni vnos (da/ne):");
        Scanner inn= new Scanner (System.in);
        izbira = inn.next();



        }while (izbira.equalsIgnoreCase("da"));
        bufferedReader.close();     
        } catch (IOException e) {
        e.printStackTrace();
        }}

public static int binarySearchfirst(List<String> integerList, String prefix) {

    int low = 0;
    int high = integerList.size() - 1;
    while (low <= high) {
        int mid = (low + high) / 2;
        if (integerList.get(mid).startsWith(prefix)) {
            if (mid == 0 || !integerList.get(mid - 1).startsWith(prefix)) {
                return mid;
            } else {
                high = mid - 1;
            }
        } else if (prefix.compareTo(integerList.get(mid)) > 0) {
            low = mid + 1;
        } else {
            high = mid - 1;
        }
    }
    return low;
}
public static int binarySearchlast(List<String> integerList, String prefix) {
        int low = 0;
        int high = integerList.size()-1;
        while (low <= high) {
            int mid = (low+high)/2;
            if (integerList.get(mid).startsWith(prefix)) {
                if (mid == integerList.size()-1 || !integerList.get(mid+1).startsWith(prefix)) {
                    return mid;
                }
                else {
                    low = mid+1;
                }
            }
            else if (prefix.compareTo(integerList.get(mid)) > 0) {
                low = mid+1;
            }
            else {
                high = mid-1;
            }
        }
        return high;
    }


}

Upvotes: 0

Views: 57

Answers (1)

mdnghtblue
mdnghtblue

Reputation: 1117

You have an extra semi-colon after your if statement:

for(i=0; i < user_input.length();i++)
{   if (user_input.charAt(i)=='*'); 
       break;
}

So the break is executed the first time through the loop no matter what. This is also why i++ is being reported as dead code...it's never being executed.

Upvotes: 5

Related Questions