Home > Back-end >  How to display "Invalid Number"?
How to display "Invalid Number"?

Time:12-14

import java.util.Scanner;

public class SumofEven1 { ``private static Scanner sc; public static void main(String[] args) { int number, i, evenSum = 0; sc = new Scanner(System.in);

    System.out.print("Please input a positive integer :  ");
    number = sc.nextInt();                  
    System.out.print("Even numbers : ");
            for(i = 1; i <= number; i  )
    {
        
        if(i % 2 == 0)
        {
            evenSum = evenSum   i; 
                            System.out.print(i   " ");
        }
                    
    }
    System.out.println("\nSum of even numbers : "   evenSum);
         
}

}

I would want for the code to show an invalid number if the user inputs a number < 0

CodePudding user response:

After taking the input, just check the number if it meets the criteria to be invalid number or not. after this line number = sc.nextInt(); write:

 if(number<0)
      System.out.print("Invalid number");
    else 
       {
   System.out.print("Even numbers : ");
                for(i = 1; i <= number; i  )
        {
            
            if(i % 2 == 0)
            {
                evenSum = evenSum   i; 
                                System.out.print(i   " ");
            }
                        
        }
        System.out.println("\nSum of even numbers : "   evenSum);
}

CodePudding user response:

After taking input from the user, you can add an "if" statement to check whether the number is > 0 or not and in the else statement, you can give the message of an "invalid number".

CodePudding user response:

Here is some updated code:

private static Scanner sc; 
  public static void main(String[] args) { 
    int number, i, evenSum = 0; 
    sc = new Scanner(System.in);
    number = -1;
    while(number < 0){
      System.out.print("Please input a positive integer :  ");
      number = sc.nextInt();
    }
    System.out.print("Even numbers : ");
    for(i = 1; i <= number; i  ){
        if(i % 2 == 0){
            evenSum = evenSum   i; 
            System.out.print(i   " ");
        }      
    }
    System.out.println("\nSum of even numbers : "   evenSum);
         
  }

Here, I used a while loop to make the user keep inputting values until one matches the criteria of being nonnegative. Alternatively, you may just want to not run the rest of the program if the user inputs a negative number:

private

static Scanner sc; 
  public static void main(String[] args) { 
    int number, i, evenSum = 0; 
    sc = new Scanner(System.in);
    System.out.print("Please input a positive integer :  ");
    number = sc.nextInt();
    if(number >= 0){
      System.out.print("Even numbers : ");
      for(i = 1; i <= number; i  ){
          if(i % 2 == 0){
              evenSum = evenSum   i; 
              System.out.print(i   " ");
          }      
      }
      System.out.println("\nSum of even numbers : "   evenSum);
    }
    else{
      System.out.println("Invalid Number");
    }
  }

In this program, the rest of the code will only run if number is nonnegative. In general though, we should use a while loop so that we will eventually get a response from the user that will allow the program to run.

I hope this helped! Let me know if you have any other questions/clarifications.

  •  Tags:  
  • java
  • Related