Solved

Data values being read in are -.50 when odd in data source?? See simple example!

Posted on 2007-04-02
6
174 Views
Last Modified: 2010-03-31
Java output string changing dollar values by .50 cents (less) when I read in from DB and output to string? How weird! The odd numbers are dropped down by .5??? (code below) any help?

Orig data:(in exel)
59,198       44,398       50,318       63,583
After reading in and outputting to screen:
59197.5        44398.0        50318.0        63582.5

CODE:
/*
 * SDP.java
 *
 * Created on March 29, 2007, 4:16 PM
 */

package my.SDP;
import javax.swing.*;
import java.sql.*;

/**
 *
 * @author  Karl Smith
 */
public class SDP extends javax.swing.JFrame {
   
    /** Creates new form SDP */
    public SDP() {
        initComponents();
    }
   
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                         
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jComboBox2 = new javax.swing.JComboBox();
        jComboBox3 = new javax.swing.JComboBox();
        jComboBox4 = new javax.swing.JComboBox();
        jComboBox5 = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Archive Appliance"));
        jLabel1.setText("Slots");

        jLabel2.setText("Drives");

        jLabel3.setText("Cache");

        jLabel4.setText("Response");

        jLabel5.setText("Years");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select One...", "16", "32", "80", "174", "238", "438", "638" }));
        jComboBox1.setPreferredSize(new java.awt.Dimension(200, 20));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select One...", "2", "4", "6" }));
        jComboBox2.setPreferredSize(new java.awt.Dimension(200, 20));
        jComboBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox2ActionPerformed(evt);
            }
        });

        jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select One...", "Standard", "2TB Cache" }));
        jComboBox3.setPreferredSize(new java.awt.Dimension(200, 20));
        jComboBox3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox3ActionPerformed(evt);
            }
        });

        jComboBox4.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select One...", "9x5 NBD", "9x5x4", "24x7x4" }));
        jComboBox4.setPreferredSize(new java.awt.Dimension(200, 20));
        jComboBox4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox4ActionPerformed(evt);
            }
        });

        jComboBox5.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select One...", "2 Years Additional", "1 Year Uplift", "3 Years Uplift", "1 Year Post", "3 Years Post" }));
        jComboBox5.setPreferredSize(new java.awt.Dimension(200, 20));
        jComboBox5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox5ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jLabel4))
                .addGap(25, 25, 25)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jComboBox3, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jComboBox4, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jComboBox5, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(41, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(jLabel2)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(jLabel3)))
                .addGap(35, 35, 35)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jComboBox4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jComboBox5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(jLabel5)))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        jLabel6.setText("Part#");

        jButton1.setText("Submit");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Clear");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(31, 31, 31)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(17, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        search(bstring);
    }                                        

    private void jComboBox5ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // Code for size of years
        String select5;
        select5 = (String)jComboBox5.getSelectedItem();
        if (select5 == "2 Years Additional") bstring = bstring + "W36";
        else if (select5 == "1 Year Uplift") bstring = bstring + "W12";
        else if (select5 == "3 Years Uplift") bstring = bstring + "W36";
        else if (select5 == "1 Year Post") bstring = bstring + "P12";
        else if (select5 == "3 Years Post") bstring = bstring + "P36";
     
        jTextField1.setText(bstring);
           
    }                                          

    private void jComboBox4ActionPerformed(java.awt.event.ActionEvent evt) {                                          
         // Code for size of response
        String select4;
        select4 = (String)jComboBox4.getSelectedItem();
        if (select4 == "9x5 NBD") bstring = bstring + "1";
        else if (select4 == "9x5x4") bstring = bstring + "2";
        else if (select4 == "24x7x4") bstring = bstring + "3";
        jTextField1.setText(bstring);
    }                                          

    private void jComboBox3ActionPerformed(java.awt.event.ActionEvent evt) {                                          
         // Code for size of cache
        String select3;
        select3 = (String)jComboBox3.getSelectedItem();
        if (select3 == "Standard") bstring = bstring + "A";
        else if (select3 == "2TB Cache") bstring = bstring + "B";
        jTextField1.setText(bstring);
    }                                          

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
         // Code for clear
        jComboBox1.setSelectedIndex(0);
        jComboBox2.setSelectedIndex(0);
        jComboBox3.setSelectedIndex(0);
        jComboBox4.setSelectedIndex(0);
        jComboBox5.setSelectedIndex(0);
        bstring ="";
        jTextField1.setText(" ");
    }                                        

    private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {                                          
     // Code for # of drives
        String select2;
        select2 = (String)jComboBox2.getSelectedItem();
        bstring = bstring + select2 +"D";
        jTextField1.setText(bstring);
    }                                          

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // Code for # of slots      
        String select1;
        select1 = (String)jComboBox1.getSelectedItem();
        bstring = bstring + "AA" + select1 + "-";
        jTextField1.setText(bstring);
    }                                          
   
    /**
     * @My class scope cariables :-)
     */
    String bstring ="";
   
    public static void aa() {                             // a.k.a. main originally no aa static?
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SDP().setVisible(true);
            }
        });
    }
   
    // Variables declaration - do not modify                    
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JComboBox jComboBox3;
    private javax.swing.JComboBox jComboBox4;
    private javax.swing.JComboBox jComboBox5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration                  
   
//}

     public void search(String partnum) {
             Connection conn=null;
             Statement stmt=null;
             String sql="";
             ResultSet rs=null;
     
             try {
                 conn=DriverManager.getConnection("jdbc:odbc:excel","","");
                 stmt=conn.createStatement();
                 
                 sql="select * from [Sheet1$] " +
                     "where Code='"+ partnum +"'";

                 rs=stmt.executeQuery(sql);
       
 
                 String output = "";
                 
                    boolean resultFound = false;
                   
                    while(rs.next()){
                    resultFound = true;

                      output = output + ("Part Number: " + rs.getString("Code")
                      + "\n\nDescription: "+ rs.getString("Product") + "\n\nList:\t$"+ rs.getString("List")
                      + "\nLatin America\t$"+ rs.getString("ListL") + "\nAsia Pacific\t$"+ rs.getString("ListA") +"\nJapan\t$"
                      + rs.getString("ListJ") + "\n\nDistributor:\t$" + rs.getString("Dist") + "\nLatin America\t$" + rs.getString("DistL")
                      + "\nAsia Pacific\t$" + rs.getString("DistA") + "\nJapan\t$" + rs.getString("DistJ") + "\n\nSystems Int: \t$"
                      + rs.getString("Sys Int") + "\nLatin America: \t$" + rs.getString("Sys IntL")+ "\nAsia Pacific: \t$"
                      + rs.getString("Sys IntA") + "\nJapan: \t$" + rs.getString("Sys IntJ"));
                 
                 JTextArea outputA = new JTextArea();
             outputA.setText(output);
                     
                // Display results from excel query
                 JOptionPane.showMessageDialog(null, outputA);   <----------- HERE IS THE OUTPUT LINE
                 }
                 if(!resultFound) {
                    JOptionPane.showMessageDialog(null, "\nInvalid configuration or not found.\n\nPlease press Clear and retry.\n\n");
}

             }
             catch (Exception e){
                 System.err.println(e);
             }
             finally {
                 try{
                     rs.close();
                     stmt.close();
                     conn.close();
                     rs=null;
                     stmt=null;
                     conn=null;
                 }
                 catch(Exception e){}
             }
         }
}


0
Comment
Question by:Karl66
  • 3
  • 2
6 Comments
 
LVL 10

Accepted Solution

by:
ADSLMark earned 250 total points
ID: 18838909
You might want to try using

getDouble(..) or getInt(..)

instead of

getString(..)

Mark
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18839045
>> if (select5 == "2 Years Additional")

How did that work? You need to have if ( select5.equalsIgnoreCase ( "2 Years Additional" ) )
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18839062
BTW use StringBuilders for string  concatenation
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Assisted Solution

by:ADSLMark
ADSLMark earned 250 total points
ID: 18839089
Also make sure that the types of the fields in your Excel sheet are set to the appropriate types. I just experimented a little with excel but I cant reproduce the error..

Mark
0
 

Author Comment

by:Karl66
ID: 18839384
Ah the actual values in the spreadsheet were x.50 -- it was showing rounded -- ok so why does java round down when I do int and only show one digit (x.0) when I do Double. Round .5 down?
0
 
LVL 10

Expert Comment

by:ADSLMark
ID: 18839435
Hmm, not sure.
Integers are always rounded to floor, so:

int value = (int) 3.5; //value = 3
int value = (int) 3.7; //value = 3

But doubles should keep their fractional part, I still think it has todo with the formatting of your excel sheet. Did you put it on number or string?

Mark
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
topping1 challenge 7 73
JDeveloper 12c for 32 bit 4 71
Impossible to extract MSI from new JAVA releases 2 48
Arrays.asList  VS  ArrayList 4 45
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now