if statements


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 12 of 12

Thread: if statements

  1. #1
    Join Date
    Aug 2005
    Posts
    41

    if statements

    Hi everyone,
    I need to know why this doesn't work. I have a method that receives a string. The string is then taken into an IF statement. Here is the statement:

    IF (v=="m") {
    System.out.println("Inside the if statement");
    }

    v is the variable that contains the string m. If I do a system.out.println outside of the IF statement it shows me that v contains the letter m. So the variable has the right letter in it but it won't execute the IF statement. I know I'm missing something but I can't put a finger on it. Can anyone help??

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    In java you compare objects with the equals method. The '==' operator
    used on objects (such as String) compares reference value (memory address).
    so:
    Code:
    if (v.equals("m")) {
      System.out.println("Inside the if statement");
    }
    Last edited by sjalle; 10-25-2005 at 06:10 AM.
    eschew obfuscation

  3. #3
    Join Date
    Aug 2005
    Posts
    41

    thanks

    Thanks for the help. What you suggested worked great. I have another question I know there is a way to uppercase a variable but I don't remember exactly how. Do you? So that i can use the if statement even if the user types in a lower case letter.

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Code:
    if (v.equalsIgnoreCase("m")) {
      System.out.println("Inside the if statement");
    }
    You should aquire the javadoc from sun, it's all there.
    eschew obfuscation

  5. #5
    Join Date
    Aug 2005
    Posts
    41

    Talking thanks

    Thanks for the help. It works but isn't there something you can use that makes what ever is in a variable all uppercase or lowercase? This is just for knowledge sake.

  6. #6
    Join Date
    Aug 2005
    Posts
    41

    another question

    I have another question.
    I have a program running. It all works except for one thing. The program has a lot of textboxes on it and an ADD button which adds what ever is in the text boxes into a database. But when i press the button it gives me a truncation error on one of the fields. The error is: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data truncated for column 'cphone' at row 1 0 01004

    When i get the number from the cphone textbox field I use getText() then when I send the data I parseDouble() to convert it. The column in the database is set to BIGINT(20) so it should be able to handle the size. Or should it? I have no other errors on the other fields that are doing the same thing. Even if I enter the number 1 it sends me the truncation error.

    Here is the code for the method if you would like to go through it:

    //method receiving data
    public void UpdateData(String sex,String account,String id,String fname,
    String lname,String birth,String age,String street,
    String city,String province,String pc,
    String medical,String hphone,String cphone,
    String email,String momfname,String momlname,
    String dadfname,String dadlname,String level,
    String hours,String timefrom,String timeuntil,
    String fee) {

    //creating string variables to hold data
    String s=sex;
    String acc = account;
    String ident = id;
    String first = fname;
    String last = lname;
    String birthday = birth;
    String ageyear = age;
    String streetaddr = street;
    String cityname = city;
    String prov = province;
    String post = pc;
    String med = medical;
    String phone1 = hphone;
    String phone2 = cphone;
    String emailaddr = email;
    String mfirst = momfname;
    String mlast = momlname;
    String dfirst = dadfname;
    String dlast = dadlname;
    String levelname = level;
    String thour = hours;
    String tfrom = timefrom;
    String tuntil = timeuntil;
    String feecost = fee;

    if ((s.equalsIgnoreCase("m"))){
    String data = "jdbc:mysql://localhost:3306/athletes";
    try {
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection(
    data, "root", "naofumi");
    Statement st = conn.createStatement();

    //Sql Statement
    st.executeUpdate("INSERT INTO Male " +
    "VALUES ('"+Double.parseDouble((String)acc)+"'"+
    ",'"+ident+"'"+
    ",'"+s+"'"+
    ",'"+last+"'"+
    ",'"+first+"'"+
    ",'"+birthday+"'"+
    ",'"+Integer.parseInt((String)age)+"'"+
    ",'"+streetaddr+"'"+
    ",'"+cityname+"'"+
    ",'"+prov+"'"+
    ",'"+post+"'"+
    ",'"+Double.parseDouble((String)med)+"'"+
    ",'"+Double.parseDouble((String)phone1)+"'"+
    ",'"+Double.parseDouble((String)phone2)+"'"+ <-This is cphones data
    ",'"+emailaddr+"'"+
    ",'"+mlast+"'"+
    ",'"+mfirst+"'"+
    ",'"+dlast+"'"+
    ",'"+dfirst+"'"+
    ",'"+levelname+"'"+
    ",'"+Integer.parseInt((String)thour)+"'"+
    ",'"+tfrom+"'"+
    ",'"+tuntil+"'"+
    ",'"+Float.parseFloat((String)feecost)+"');"
    );
    st.close();
    } catch (SQLException sq) {
    System.out.println("SQL Error: " + sq.toString() + " "
    + sq.getErrorCode() + " " + sq.getSQLState());
    } catch (Exception e) {
    System.out.println(" Error: " + e.toString()
    + e.getMessage());
    }
    }

    Are you able to see something that I am missing?

  7. #7
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    1; use the toUpper/ToLower -Case methods of String.

    2; You dont't get an sql exception from that, it's because you are attemting a value
    insertion that exceeds the specified width for a column, namely 'cphone', somewhere in
    your code....
    eschew obfuscation

  8. #8
    Join Date
    Aug 2005
    Posts
    41

    thanks but..

    thanks for the information. Ita good to know about the toUpper/toLower methods.

    On the other info. I'm not quite sure what you mean about "a value
    insertion that exceeds the specified width for a column, namely 'cphone' " . Do you mean the column width of the database column of cphone. Or the width of the textbox in the program? Or something else? I think you mean the database but the column is set to a BIGINT(20). Isn't the column then set to 20. I get this truncation even when I only enter the number 1. It should fit shouldn't it?

    This is the only place that I have that inserts anything into the database. So I can't see any other place where this will occur.

  9. #9
    Join Date
    Aug 2005
    Posts
    41

    I figured it out

    I figured it out. The truncation was do to using the wrong data type. I should have looked closer. I changed the data type from Interger.parceInt() to Double.parceDouble() in an attempt to fixing a different problem. I have now changed it to Long.parceLong() which makes it work now. Int is to small for what I need so Long will have to do.

    Thanks for your help. I hope I can count on you again in the future. I probably will need some more help.

  10. #10
    Join Date
    Aug 2005
    Posts
    41

    any other?

    Hi sjalle,
    You told me that you can use toUpper/toLower but is there any way of only making the first letter of a word UpperCase?

    Also, is there a simple way of making the text that the user types in a JTextField UpperCase as the user types it in.

  11. #11
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Check it out.

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    /**
     * Uppercase, Keylistener, FocusListener & stuff
     */
    
    public class Charmer extends JFrame implements KeyListener, FocusListener {
      GridLayout gridLayout1 = new GridLayout();
      JLabel jLabel1 = new JLabel();
      JTextField firstUpperTF = new JTextField();
      JLabel jLabel2 = new JLabel();
      JTextField upperTF = new JTextField();
      /**
       * Constructor
       */
      public Charmer() {
        try {
          jbInit();
          upperTF.addKeyListener(this);
          firstUpperTF.addKeyListener(this);
          firstUpperTF.addFocusListener(this);
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      /**
       * MAIN
       */
      public static void main(String[] args) {
        Charmer cc = new Charmer();
        cc.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }
        });
        cc.setBounds(100,100,280,75);
        cc.setVisible(true);
      }
      /**
       * GUI
       */
      private void jbInit() throws Exception {
        jLabel1.setText("Uppercased");
        gridLayout1.setColumns(2);
        gridLayout1.setRows(2);
        this.getContentPane().setLayout(gridLayout1);
        jLabel2.setText("first Upper");
        upperTF.setText("");
        firstUpperTF.setText("");
        this.getContentPane().add(jLabel1, null);
        this.getContentPane().add(upperTF, null);
        this.getContentPane().add(jLabel2, null);
        this.getContentPane().add(firstUpperTF, null);
      }
      /**
       * KeyListener impl.
       * Note; at this point the typed character is
       * not set in the texfield.
       * @param e
       */
      public void keyTyped(KeyEvent e) {
        char c = e.getKeyChar();
        if (e.getSource()==upperTF) {
          e.setKeyChar(Character.toUpperCase(c));
        } else if (e.getSource()==firstUpperTF) {
          if (firstUpperTF.getText().trim().length()==0) {
            e.setKeyChar(Character.toUpperCase(c));
          }
        }
      }
      public void keyReleased(KeyEvent e) {}
      public void keyPressed(KeyEvent e) {}
    
      /**
       * FocusListener impl.
       * @param e
       */
      public void focusLost(FocusEvent e) {
        String s=firstUpperTF.getText().trim();
        if (s.length()==0) {
          return;
        } else if (s.length()==1) {
          firstUpperTF.setText(s.toUpperCase());
        } else {
          setFirstUppercase(s);
        }
      }
    
      private void setFirstUppercase(String s) {
        String s1=s.substring(0,1);
        String s2=s.substring(1);
        firstUpperTF.setText(s1.toUpperCase()+s2.toLowerCase());
      }
      public void focusGained(FocusEvent e) {}
    }
    eschew obfuscation

  12. #12
    Join Date
    Aug 2005
    Posts
    41

    thanks

    Thanks sjalle ,

    I will look at the code but it looks promising I really needed to get this to work. I have another question posted on the forum if you don't mind about taking a look. It about disabling the close,max and min buttons on a window.

Similar Threads

  1. Replies: 3
    Last Post: 06-17-2002, 10:15 AM
  2. Supress SQL Statements in Report
    By Dennis Katumalla in forum Database
    Replies: 0
    Last Post: 10-25-2001, 05:43 PM
  3. SQL Tutorial (Answer Q's & post your reply)
    By bigbastard4 in forum Database
    Replies: 2
    Last Post: 05-16-2001, 06:24 PM
  4. Maximum number of statements in command object
    By Francesco Lari in forum Database
    Replies: 1
    Last Post: 01-29-2001, 07:43 PM
  5. EXEC statements
    By Kristin in forum Database
    Replies: 1
    Last Post: 08-25-2000, 03:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center