Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By jim829

Thread: netbeans hates conversion specifier s for reasons I don't understand?

  1. #1
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default netbeans hates conversion specifier s for reasons I don't understand?

    Hello everyone,
    I was just messing around with my main window in Java and I kept getting this problem where I couldn't append to a file.

    Currently I have this as my code
    Java Code:
         
             String stringthing = "category path = Root\\Kit Serial Numbers";
           String name1 = "name1";
           String name2="name2";
           String name3 = "name3";
           String Serial = "Serial number";
           String part = "part in series";
           String assembly = "Assembly Lot,";
           String anode ="anode lot";
           String cathode = "cathode lot";
           String header = "Header Glassing";
           String tantalum = "tantalum lot number";
           String Cap = "Capacitence";
           String ESR = "ESR";
           String Leakage= "leakage";
           String Assemblylotselected = "Assembly lot selected";
           String Anode1 = "anode 1";
           String Anode2 = "anode 2";
           String Anode3 = "anode 3";
           String Anode4 = "anode 4";
           String Anode5 = "anode 5";
           String Cathodelot = "Cathode Lot#";
           String TeflonSpacer1="Teflon Spacer 1";
           String TeflonSpacer2 = "Teflon Spacer 2";
           String TeflonSpacer3 = "Teflon Spacer 3";
           String HeaderGlass = " Header Glassing Lot";
           String Tantalumlot = "Tantalum lot";
           String Teflon = "Teflon Spacer Lot";    
    try {
    
            // FileWriter  fileWriter = null;
           String stuff = jComboBox2.getSelectedItem().toString();
                    File happyFile = new File("%s.csv",stuff);
            FileWriter fileWriter = new FileWriter(happyFile, true);
                  //  BufferedWriter buffer = new BufferedWriter(fileWriter);
                    PrintWriter printWriter = new PrintWriter(fileWriter);
                    printWriter.printf( "%s\n,%s,,%s,,,,%s\n,%s,%s,%s,%s,,%s,%s,%s,%s,%s,,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s",stringthing,name1,name2,name3,Serial,part,assembly,anode,cathode,header,tantalum,Cap,ESR,Leakage,Assemblylotselected,Anode1,Anode2,Anode3,Anode4,Anode5,Cathodelot,TeflonSpacer1,TeflonSpacer2,TeflonSpacer3,HeaderGlass,Tantalumlot,Teflon);
                    printWriter.close();%s,%s",stringthing,name1,name2,name3,Serial,part,assembly,anode,cathode,header,tantalum,Cap,ESR,Leakage,Assemblylotselected,Anode1,Anode2,Anode3,Anode4,Anode5,Cathodelot,TeflonSpacer1,TeflonSpacer2,TeflonSpacer3,HeaderGlass,Tantalumlot,Teflon);
                    printWriter.close();
        } catch (IOException ex) {
                   Logger.getLogger(mainWindow.class.getName()).log(Level.SEVERE, null, ex);
                   System.out.println("uh oh");
        }
    I already defined all of the strings, so that's all fine and dandy. Currently I can make a file but I can't write to it for some reason. I am appending the file when I'm trying to write to it but I can't see why that would be a problem. As far as I'm aware appending creates the file if it isn't there, therefore why can't I create the file and append to it? netbeans is getting so angry over this and I don't understand why, especially since I have a similar sequence in in the same code that works


    THIS SEGMENT WORKS -- despite being almost the same
    Java Code:
    try {
                    File happyFile = new File("Kit Serial Numbers TEMPLATE.csv");
                    FileWriter  fileWriter = new FileWriter(happyFile, true);
                    BufferedWriter buffer = new BufferedWriter(fileWriter);
                    PrintWriter printWriter = new PrintWriter(buffer);
                    printWriter.printf( "\n");
                    String total2 = String.valueOf(s);
                    printWriter.printf( ",%s",total2);
                    printWriter.close();
                    
                    
                } catch (IOException ex) {
                    Logger.getLogger(mainWindow.class.getName()).log(Level.SEVERE, null, ex);

    I'm thinking maybe this has to do with the combobox but idk. Does anyone have any Idea why I can't write to the file? with the first code I can't append to the new file, but with the second code I can. Why doesn't the first one work?
    Last edited by innovo; 08-12-2015 at 04:33 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: netbeans hates conversion specifier s for reasons I don't understand?

    Do you really want to create a file named %s.csv (line #32)? Better create a name as: stuff+".csv".

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default Re: netbeans hates conversion specifier s for reasons I don't understand?

    I was trying to assign whatever jcombobox2 had selected as the new file name. I thought I needed a conversion specifier is all.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: netbeans hates conversion specifier s for reasons I don't understand?

    That implies that you think you can use %s or "conversion specifiers" in just any String - that is not the case. Only very specific methods allow them to be used.

    https://docs.oracle.com/javase/tutor...berformat.html


    This specific case is quite nasty, as your code actually compiled! That is because you were unwillingly using a valid constructor of the File class.

    https://docs.oracle.com/javase/8/doc...a.lang.String-

    File actually has a constructor which accepts two String variables, it just doesn't do what you expected. And that just goes to prove: the javadocs are your friend.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: netbeans hates conversion specifier s for reasons I don't understand?

    That constructor for File does:
    "Creates a new File instance from a parent pathname string and a child pathname string."

    So in your usage of:
    Java Code:
    File happyFile = new File("%s.csv",stuff);
    means creating a file in the '%s.csv' directory with a name of stuff.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default Re: netbeans hates conversion specifier s for reasons I don't understand?

    Quote Originally Posted by gimbal2 View Post
    That implies that you think you can use %s or "conversion specifiers" in just any String - that is not the case. Only very specific methods allow them to be used.

    https://docs.oracle.com/javase/tutor...berformat.html



    This specific case is quite nasty, as your code actually compiled! That is because you were unwillingly using a valid constructor of the File class.

    https://docs.oracle.com/javase/8/doc...a.lang.String-

    File actually has a constructor which accepts two String variables, it just doesn't do what you expected. And that just goes to prove: the javadocs are your friend.
    Right now I actually have it working. I changed things around but eventually it randomly decided to work. I'm not sure why but alright @_@.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: netbeans hates conversion specifier s for reasons I don't understand?

    Quote Originally Posted by innovo View Post
    Right now I actually have it working. I changed things around but eventually it randomly decided to work. I'm not sure why but alright @_@.
    Then you still have a problem. I recommend you figure out why it didn't work before you proceed. Otherwise, future debugging may be even more difficult.

    Regards,
    Jim
    DarrylBurke likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. 12 reasons why I choose to be in IT industry
    By DK Bello in forum Entertainment
    Replies: 1
    Last Post: 01-06-2012, 08:53 AM
  2. Reasons Why You Should Program in Java (article)
    By FlipPoker@gmail.com in forum Reviews / Advertising
    Replies: 0
    Last Post: 08-02-2011, 06:10 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
  •