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

  • Home
  • SEARCH
  • 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 8792649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:00:34+00:00 2026-06-13T23:00:34+00:00

Trying to read double data from the file that has different contents. For example

  • 0

Trying to read double data from the file that has different contents. For example if it is a double then the message should be “Double number is 23.5”. and if it is not double number the message should be “Sixty three is not a double number”. The file contents are

97.9

100.1

Three

Sixty three

77

12.4

3002.4

76

Cool

34.6

This is it

…………

The code i wrote opens the file and scans next line But does not seem to properly work.

class ReadDouble 
 {

 Scanner scan = new Scanner(System.in);


try

{

    File textFile = new File ("doubleData.txt");
    Scanner scanFile = new Scanner (textFile);
    String str = scan.nextLine();

    while(scanFile.hasNextLine())
    {

        double num = Double.parseDouble(str);
        if(str == num)
        {
            System.out.println("Double number is" + str);
        }

    }//end while


}//end try

catch (NumberFormatException nfe)
{
   System.out.println(str + "Is not a Double number");
}

}

}//end class

  • 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-13T23:00:35+00:00Added an answer on June 13, 2026 at 11:00 pm

    First, you should call String str = scan.nextLine(); within the loop otherwise you only ever read the first line. Also, your try / catch block should be wrapped around double num = Double.parseDouble(str); within the while loop otherwise you will not make another call to scan.nextLine() after you encounter your first non-double.

    Finally, you shouldn’t do if(str == num) as this will always be false. If Double.parseDouble(str) does not throw an exception, it contains the double found on that line.

    Here is a solution that reads from standard in:

    import java.util.Scanner;
    
    public class ReadDouble {
    
        public static void main(String[] args){
            Scanner scan = new Scanner (System.in);
    
            while(scan.hasNextLine()){
            String str = scan.nextLine();
    
            try {
                num = Double.parseDouble(str);
                System.out.println("Double number is " + num);
            } catch (NumberFormatException nfe) {
                System.out.println(str + " is not a Double number");
            }       
        }
    }
    }
    

    Another option is to use Scanner to see if the next element is a double if it is read it using nextDouble() otherwise read using nextLine().

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

Sidebar

Related Questions

I am trying to read from a file that has comma seperated values like
I'm trying to read data in from a binary file and then store in
I am trying to read from a file and encrypt the data using AES
Here is my problem, I'm trying to read in data from a file beginningbalance.dat
am trying to read a file (double values) with scanner and save the data
I'm trying to read from XML and store the data in a text file.
I'm trying to write/read a double matrix to file as binary data but I'm
I am trying to read some data from a xml file, numbers are saved
I'm basically trying to use an array, which is data that is read from
I am trying to read in an XML file that I have saved to

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.