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 9080339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:03:30+00:00 2026-06-16T20:03:30+00:00

As part of my homework, I need to make a program that requires user

  • 0

As part of my homework, I need to make a program that requires user input. For now I’m sticking with the console, however I’d like to avoid a crash due to newline characters.

This is a test, it acts similarly to what I’m trying to do and crashes due to even one newline character

public void testRead() {

    Scanner input = new Scanner(System.in);

    String s1 = "", s2 = "", s3 = "", s4 = "", s5 = "";

    while (s1 == "" || s1 =="\n") 
        if (input.hasNext()) {
            s1 = input.nextLine();
        }
    while (s2 == "" || s2 =="\n") 
        if (input.hasNext()) {
            s2 = input.nextLine();
        }
    while (s3 == "" || s3 == "\n") 
        if (input.hasNext()) {
            s3 = input.nextLine();
        }
    while (s4 == "" || s4 == "\n") 
        if (input.hasNext()) {
            s4 = input.nextLine();
        }
    while (s5 == "" || s5 == "\n") 
        if (input.hasNext()) {
            s5 = input.nextLine();
        }
        // Here is why it might crash
    if (input.hasNextInt()) // even though it should not pass this if
            // However the if is not the issue. 
            // This input may even be in another function
           int crash = input.nextLine();

    System.out.println("s1: " + s1);
    System.out.println("s2: " + s2);
    System.out.println("s3: " + s3);
    System.out.println("s4: " + s4);
    System.out.println("s5: " + s5);
}

}

I hoped the while statement would solve it, but it does not.

I could solve the crash, but that would not fix the problem since I would still have empty strings and what not.

Example

This is the first string
This is the second string
                                          <- pressed enter again, by mistake or not
This is the third string
This is the fourth string

Output

s1: This is the first string
s2: This is the second string
s3:
s4: This is the third string
s5: This is the fourth string

// now crash. Again, the crash can be avoided, but I still have a problem where string 3 has not been read or contains …. something I don’t want.

Is there an easy way of solving this issue ? if there is no easy way, I would rather ignore it and finish my homework fast, but I would still like to know the long answer for future reference.

  • 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-16T20:03:31+00:00Added an answer on June 16, 2026 at 8:03 pm

    you cannot use == for string contents equality, try this for ex:

     while (s1.equals("") || s1.equals("\n")) 
            if (input.hasNext()) {
                s1 = input.nextLine();
            }
        while (s2.equals("") || s2.equals("\n")) 
            if (input.hasNext()) {
                s2 = input.nextLine();
            }
        while (s3.equals("") || s3.equals("\n")) 
            if (input.hasNext()) {
                s3 = input.nextLine();
            }
        while (s4.equals("") || s4.equals("\n")) 
            if (input.hasNext()) {
                s4 = input.nextLine();
            }
        while (s5.equals("") || s5.equals("\n")) 
            if (input.hasNext()) {
                s5 = input.nextLine();
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to make engine for my console application that will count the
It is a part of a homework. I need to write a function that
As part of a homework assignment I need to concatenate certain values in an
as a part of my homework i need to implement this pattern matching. the
hello as part of my Homework. i need to calculate and display the edge
so this is part of my homework assignment. need some help with it. my
As part of a homework assignment, we are supposed to create an array that
As part of my homework project I had to implement a library that implements
As part of a homework assignment, I have to write a C program in
Some classmates and I are working on a homework assignment for Java that requires

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.