Home > Blockchain >  My code automatically terminates immediately upon running in the console. Eclipse IDE. No errors in
My code automatically terminates immediately upon running in the console. Eclipse IDE. No errors in

Time:03-13

I am writing a java program for user input of a string representing an 8-digit binary number. If the input is valid the program should count the 1's and if its not valid it should prompt the user for a valid entry. I've found renditions of this code online but still can't find my error. I am using Eclipse IDE 2021-12 and Java 17.0.1.

import java.util.Scanner;

public class Chapter6PA1 {
    
    public static void main(String[]args){
        //initialize variables and input
        Scanner sc = new Scanner(System.in);
        String word = "";//user input word
        int count=0;//the number of 1s in the word
        boolean isValid = false;//initialize boolean to be false until conditions are met for valid binary number
        do {
            for(int i=0; i <word.length()-1; i  ) {
                System.out.println("Please enter a valid binary number: ");//prompts user input
                word = sc.nextLine();//determines input is equal to string word
                if(word.length()==7){
                    if(word.charAt(i)=='1'||word.charAt(i)=='0'){
                        if(word.charAt(i)=='1');
                        isValid = true;//when the above conditions are met then we have a valid binary number
                        count  ;//count will increment each time a character is equal to 1
                        System.out.println("The binary number you entered contains "   count   " ones.");
                    }
                                    
                }
                else {
                    System.out.println("Invalid binary number.Please enter an 8-digit word containing only 1s and 0s.");
                    break;
                }
            
            }
        }while(isValid);
        }
    }

this is where im at now, i initialize the String declaration inside the do loop and now the program output is incorrect. here is code.

import java.util.Scanner;

public class Chapter6PA1 {
    
    public static void main(String[]args){
        //initialize variables and input
        Scanner sc = new Scanner(System.in);
        int count=0;//the number of 1s in the word
        boolean isValid = false;//initialize boolean to be false until conditions are met for valid binary number
        do {
            System.out.println("Please enter a valid binary number: ");//prompts user input
            String word = sc.nextLine();//determines input is equal to string word//user input word
            for(int i=0; i <word.length()-1; i  ) {
                if(word.length()==8){
                    if(word.charAt(i)=='1'||word.charAt(i)=='0'){
                        if(word.charAt(i)=='1');
                        isValid = true;//when the above conditions are met then we have a valid binary number
                        count  ;//count will increment each time a character is equal to 1
                        System.out.println("The binary number you entered contains "   count   " ones.");
                    }
                                    
                }
                else {
                    System.out.println("Invalid binary number.Please enter an 8-digit word containing only 1s and 0s.");
                    break;
                }
            
            }
        }while(!isValid);
        }
    }

and here is output:

>Please enter a valid binary number: 
>0101
>Invalid binary number.Please enter an 8-digit word containing only 1s and 0s.
>Please enter a valid binary number: 
>01010101
>The binary number you entered contains 1 ones.
>The binary number you entered contains 2 ones.
>The binary number you entered contains 3 ones.
>The binary number you entered contains 4 ones.
>The binary number you entered contains 5 ones.
>The binary number you entered contains 6 ones.
>The binary number you entered contains 7 ones.

CodePudding user response:

Because the String word is empty and when it goes to the for loop it checks where i which is 0, is less than the length-1 of the String word which is 0 as well hence it does not go inside the for loop.

So replace the below line,

String word = "";//user input word

With these lines in the for loop above the loop

System.out.println("Please enter a valid binary number: ");//prompts user input
           String word = sc.nextLine();//determines input is equal to string word

CodePudding user response:

As already mentioned by @ bdavidson024 the word is initially empty so the loop breaks without executing. Also the condition inside the while loop should be !isValid
Try the following code:

public static void main(String[]args){
        //initialize variables and input
        Scanner sc = new Scanner(System.in);
        String word="";//user input word
        int count=0;//the number of 1s in the word
        boolean isValid = false;//initialize boolean to be false until conditions are met for valid binary number
        do {
            System.out.println("Please enter a valid binary number: ");//prompts user input
            word = sc.nextLine();//determines input is equal to string word
            if(word.length()==8){
                isValid = true;
                for(int i=0; i <word.length(); i  ) {                
                    if(word.charAt(i)=='1'||word.charAt(i)=='0'){
                        if(word.charAt(i)=='1'){
                            count  ;//count will increment each time a character is equal to 1
                        }
                    }
                    else break;
                }              
                System.out.println("The binary number you entered contains "   count   " ones.");
            }  
            else {
                System.out.println("Invalid binary number.Please enter an 8-digit word containing only 1s and 0s.");
                isValid = false;
            }
        } while(!isValid);
    }

CodePudding user response:

thanks guys here is the correct code @Naman just change the loop condition!

    public static void main(String[]args){
        //initialize variables and input
        Scanner sc = new Scanner(System.in);
        int count=0;//the number of 1s in the word
        boolean isValid = false;//initialize boolean to be false until conditions are met for valid binary number
        do {
            System.out.println("Please enter a valid binary number: ");//prompts user input
            String word = sc.nextLine();//determines input is equal to string word//user input word
            if(word.length()==8){
                isValid = true;//when the above conditions are met then we have a valid binary number
                for(int i=0; i <word.length()-1; i  ) {
                    if(word.charAt(i)=='1'||word.charAt(i)=='0'){
                        if(word.charAt(i)=='1') {
                            count  ;//count will increment each time a character is equal to 1
                        }
                    }
                    else break;             
                }
                System.out.println("The binary number you entered contains "   count   " ones.");

            }
            else {
                System.out.println("Invalid binary number.Please enter an 8-digit word containing only 1s and 0s.");
                isValid=false;
            }
        }while(!isValid);
    }
}
  • Related