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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:47:57+00:00 2026-05-30T02:47:57+00:00

Basically, I am trying to do a while loop, and to keep reading until

  • 0

Basically, I am trying to do a while loop, and to keep reading until the line input is $. When that is read, the the loop should exit.

However I am getting a runtime exception as java.util.InputMismatchException.

Here is the code:

while (scan.nextLine() != "$") {
    temp1 = scan.nextInt();
    temp2 = scan.nextInt();
    addEdge(temp1, temp2);
}
  • 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-05-30T02:47:58+00:00Added an answer on May 30, 2026 at 2:47 am

    You need to use the line you read in (inside the while condition).

    When you call nextLine() you not only are checking if it equals “$” but also you are throwing it away.

    I assume you are reading input that is like the following: (I bet you are)

    1 2
    3 4
    0 1
    4 5
    $
    

    try the following:

    String nextLineStr;
    while( !((nextLinestr = scan.nextLine()).equals("$")))
    {
    String tokens [] = nextLineStr.split(" ");
    temp1 = Integer.parseInt(tokens[0]);
    temp2 = Integer.parseInt(tokens[1]);
    addEdge(temp1,temp2);
    }
    

    As a side note: Notice that I switched out your ‘!=’ for a not equals method, this is because a String should not be compared using == or !=

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

Sidebar

Related Questions

im trying to fix this while loop but keep running into errors. Basically lets
I am trying to escape a while loop. Basically, if the if condition is
Basically I'm trying to accomplish the same thing that mailto:bgates@microsoft.com does in Internet Explorer
I am basically trying to return from a method which reads user input from
So I'm basically just trying to take in some file input, and then take
today I am basically trying to create some JAVA code that moves an actor
I am trying to get my code to work. I basically check that if
I'm basically trying to figure out the simplest way to perform your basic insert
I'm basically trying to teach myself how to code and I want to follow
I am basically trying to trigger a function if the footer is inside the

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.