public  void readArguments(String[]args ) {
         
         
        String [] readArray = new String[ 10 ]; 
        Scanner readscanner=new Scanner(System.in); 
         
        // read values from keyboard into array 
        System.out.println("Enter your arguments or type Exit to exit the loop");
         for ( int i = 0 ; i <  readArray.length ; i ++) { 
                 
                             
                 if(readArray[i] =="Exit" ) break;                                    
                 readArray[i] = readscanner.nextLine();
                 
                 
          }              
              
   //Determine if the number of Arguments is less or greater than 5
               if(readArray.length<=4 ){
                  
                  System.out.println("There are less than 5 arguments passed");
                }
                 else {
                       System.out.println("More than 5 arguments passed");         
              
              
             
                 
        
       } 
    }

Dani AI

Generated

A few concrete points to clear up what went wrong and give a safe, reusable pattern.

  • The comparison bug: use string equality methods, not ==. As noted, == checks reference identity; use equals or equalsIgnoreCase.
  • Check the input after you read it. In your original loop you tested readArray[i] before assigning it, so the test never worked (and would throw an NPE if you used equals).
  • readArray.length is the array capacity, not the number of entries actually typed. Track a count (or use a List) and compare that count to 5.

Example using a fixed-size array but tracking how many entries were filled:

public void readArguments() {
    String[] readArray = new String[10];
    Scanner scanner = new Scanner(System.in);
    int count = 0;
    System.out.println("Enter your arguments or type Exit to stop:");

    for (int i = 0; i < readArray.length; i++) {
        String input = scanner.nextLine().trim();
        if ("Exit".equalsIgnoreCase(input)) {
            break;
        }
        if (input.isEmpty()) {
            i--; // skip blank lines without consuming a slot
            continue;
        }
        readArray[count++] = input;
    }

    if (count < 5) {
        System.out.println("There are less than 5 arguments passed");
    } else {
        System.out.println("More than 5 arguments passed");
    }
    // avoid closing scanner if it wraps System.in here
}

If you don't want a fixed capacity, the while / ArrayList approach suggested by is cleaner:

List<String> inputs = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your arguments or type Exit to stop:");

while (true) {
    String line = scanner.nextLine().trim();
    if ("Exit".equalsIgnoreCase(line)) break;
    if (!line.isEmpty()) inputs.add(line);
}

int count = inputs.size();

Quick tips: prefer "Exit".equalsIgnoreCase(input) (literal first avoids NPE), use trim() to ignore stray spaces, don’t rely on array.length to know how many elements were entered, and avoid closing a Scanner that wraps System.in inside a helper method.

Recommended Answers

All 3 Replies

First, when you do "if(readArray =="Exit" ) break;", you don't actually check the value of the string, rather the position in the memory. To check the actual value, use readArray.equals("Exit").

Second, you need to place your if statement after your input. As it is now, at your if statement, readArray is null, since you haven't done anything with it yet. So your if-statement will never be true.

commented: Very helpful answer to beginner question. +4

Wow!! thank you very much. its working now.

Member Avatar for Member #647493

Another option would be to use a while loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.