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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:30:41+00:00 2026-05-27T01:30:41+00:00

Edit: figured it out, misplaced a piece of code I have a program that

  • 0

Edit: figured it out, misplaced a piece of code

I have a program that calculates slopes from a file. The format the file is in is like this:

Y2 ‘space’ Y1 ‘space’ X2 ‘space’ X1 ‘space’

I am using a Scanner to read Strings from the file then converting them to a double or integer. The reason I am doing this is because it seems like it doesn’t read doubles or integers from a text file only strings. Here is some of my code:

 modelSlopes.clear();
    modelValues.clear();
    int returnVal = openFileChooser.showOpenDialog(this);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = openFileChooser.getSelectedFile();
        try {
            Scanner fileScanner = new Scanner(new FileReader(file));
            int count = 1;
            boolean suc = true;
            while (fileScanner.hasNext()) {
                suc = true;
                double tmp1 = 0;
                double tmp2 = 0;
                double tmp3 = 0;
                double tmp4 = 0;
                try {
                    if(count == 1) {
                        tmp1 = Double.valueOf(fileScanner.next());

                    }
                    if(count == 2) {
                        tmp2 = Double.valueOf(fileScanner.next());

                    }
                    if(count == 3) {
                        tmp3 = Double.valueOf(fileScanner.next());

                    }
                    if(count == 4) {
                        tmp4 = Double.valueOf(fileScanner.next());

                    }

                } catch (NumberFormatException e) {

                    try {
                        if(count == 1) {
                            tmp1 = Integer.valueOf(fileScanner.next());
                        }
                        if(count == 2) {
                            tmp2 = Integer.valueOf(fileScanner.next());
                        }
                        if(count == 3) {
                            tmp3 = Integer.valueOf(fileScanner.next());
                        }
                        if(count == 4) {
                            tmp4 = Integer.valueOf(fileScanner.next());
                        }
                    } catch(NumberFormatException e1) {
                        suc = false;
                    }
                }

                if(suc)  {
                    if(count != 4) {
                        count++;
                    }
                    if(count == 4) {
                        count = 1;
                        SlopeSolver tmpS = new SlopeSolver(Double.valueOf(tmp1), Double.valueOf(tmp2), Double.valueOf(tmp3), Double.valueOf(tmp4));
                        modelSlopes.addElement(tmpS.getSlope());
                        modelValues.addElement("Y2 - " + String.valueOf(tmp1) + "; Y1 - " + String.valueOf(tmp2) + "; X2 - " + String.valueOf(tmp3) + "; X1 - " + String.valueOf(tmp4));

                    }
                }
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Error opening or with file");
        }
    } else {
    }

When I do this it displays 0’s for the slope and the values. I can’t seem to get it to work. I am new to java, and I am pretty clueless right now. Any help would be appreciated.

Here is the text file I am reading data from:

0.0 2.0 0.0 1.0

~Andrew

  • 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-27T01:30:41+00:00Added an answer on May 27, 2026 at 1:30 am

    I would simplify the code

    Scanner sc = new Scanner(new FileReader(file));
    SlopeSolver ss = new SlopeSolver(sc.nextDouble(),
                                  sc.nextDouble(), sc.nextDouble(), sc.nextDouble());
    sc.close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: I figured out how to solve the specific code broken part of my
I have a XML file that has the following data, <extcode Type=abc Code=12345 />
EDIT AGAIN ... I'm just a dummy and figured it out! EDIT: So, it
Edit: From another question I provided an answer that has links to a lot
Edit: I figured this out. jQuery Cycle plugin adds a z-index to every item
EDIT: I figured out the answer to the original YUI3 question I posted here,
EDIT: Figured out the problem. Also if you found this through google or another
I've got a powershell script that monitors a log file, filters out the interesting
EDIT: Figured it out. I should be doing (span.age, :content => ...) I am
Possible Duplicate: Change flash src with jquery? EDIT I figured out the first issue

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.