Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9192939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:02:52+00:00 2026-06-17T21:02:52+00:00

Scanner sc = new Scanner(System.in); Boolean valid = true; do{ valid = true; //get

  • 0
Scanner sc = new Scanner(System.in);
Boolean valid =  true;
do{
    valid = true;
    //get first name input and valid
    System.out.print("Enter your First Name > ");
    String firstName = sc.nextLine();
    if(firstName.trim().length() > 25){
        valid = false;
    }
    //get last name input and valid
    System.out.print("Enter your Last Name > ");
    String lastName = sc.nextLine();
    if(lastName.trim().length() > 25){
        valid = false;
    }
    //get email input and valid
    System.out.print("Enter your Email > ");
    String email = sc.nextLine();
    if(email.trim().length() > 40){
        valid = false;
    }
      ctrl.addPerson(email, firstName, lastName);

}while(valid == false);

User keeps being prompted for invalid input, but invalid data still added/saved (addPerson).
I can understand why invalid is saved, but how should i use the if correctly? thank you for the help!


I have read everyone’s suggestions and recommendations, below is the editted codes. Hopefully it is better.

Scanner sc = new Scanner(System.in);
boolean valid =  true;
String firstName = null;
String lastName = null;
String email = null;
Date birthday = null;

        do{
            System.out.print("Enter your First Name > ");
            firstName = sc.nextLine();
        }while(firstName.trim().length() > 25);

        do{     
            System.out.print("Enter your Last Name > ");
            lastName = sc.nextLine();
        }while(lastName.trim().length() > 25);

        do{     
            System.out.print("Enter your Email > ");
            email = sc.nextLine();
        }while(email.trim().length() > 40);

        ctrl.addPerson(email, firstName, lastName);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:02:53+00:00Added an answer on June 17, 2026 at 9:02 pm

    You need to put method addPerson under condition like

    if(valid) {
        ctrl.addPerson(email, firstName, lastName);
    }
    

    I also suggest you to use primitive types (boolean instead of Boolean), there is no reason to use its object representation here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Scanner kb = new Scanner(System.in); System.out.print(>> ); String line = kb.nextLine(); line = line.trim();
My main method: public static void main(String[] args) { Scanner input = new Scanner(System.in);
public void getWebContent() throws Exception { Scanner in = new Scanner(System.in); System.out.println(Input URL:); fileURL
I have a method that returns true or false, public boolean confirmVotes() { System.out.println(Your
EDIT: The working code: Scanner input = new Scanner(System.in); String inputFile = Computer_Store_DSV.txt; String
private static int posNum() { Scanner scan = new Scanner(System.in); int input = 0;
import java.util.Scanner; class Practice { public static void main(String args[]) { System.out.println(Enter the number
If I have the following Java code: int[][] readAPuzzle() { Scanner input = new
this code is correct?? String note = text.txt; FileWriter file = new FileWriter(note); Scanner
package homework4; import java.util.Scanner; public class Prog4 { static Scanner scanner = new Scanner(System.in);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.