Problems with saving strings to file

edited April 2016 in Questions about Code

Im having problems saving a string to a txt file. The file.txt is in the data folder. I'm not sure what I am doing wrong.

else if (hb0.value >= hb0.max) {
        frameNum++;

        image(images[frameNum/10 %images.length], 800, 600);     

        port.write(1); 


        println("Here is the achievment"); 
        image(achievment2, 700, 100);
        String words = "Achievement achieved";
        String[] list = split(words , " ");

        saveStrings("file.txt", list);
        println("They are saved");

Answers

  • Answer ✓

    Needs dataPath() in order to get "/data" folder path: dataPath("file.txt").

  • That worked thanks :D

  • edited April 2016

    GoToLoop I do have one more question when it comes to saving strings... I am trying to do the same thing with a users ID and password. Can u tell me how I can get this to work please? it says saveStrings expects parameters like "saveStrings(String,String[]") :/

        final StringDict accounts = new StringDict(
          new String[] {
          "john", "tony", "amy"
          }
          , new String[] {
            "ilikedogs", "husky", "pepsi"
          }
          );
    
    
    String askUser() {
      String id = showInputDialog("Please enter user:");
    
      if (id == null)
        showMessageDialog(null, "You've canceled login operation!"
          , "Alert", ERROR_MESSAGE);
    
      else if ("".equals(id))
        showMessageDialog(null, "Empty user input!"
          , "Alert", ERROR_MESSAGE);
    
      else if (!accounts.hasKey(id = id.toLowerCase()))
        showMessageDialog(null, "Unknown \"" + id + "\" user!" + (id = "")
          , "Alert", ERROR_MESSAGE);
    
      return id;
    }
    
    boolean askPass(String id) {
      boolean isLogged = false;
      pwd.setText("");
    
      int action = showConfirmDialog(null, pwd
        , "Now enter password:", OK_CANCEL_OPTION);
    
      if (action != OK_OPTION) {
        showMessageDialog(null, "Password input canceled!"
          , "Alert", ERROR_MESSAGE);
    
        return false;
      }
    
      String phrase = pwd.getText();
      //String phrase = new String(pwd.getPassword());
    
      if ("".equals(phrase))
        showMessageDialog(null, "Empty password input!"
          , "Alert", ERROR_MESSAGE);
    
      else if (accounts.get(id).equals(phrase)) {
        saveStrings(dataPath("file.txt"), accounts);
        showMessageDialog(null, "Welcome \"" + id + "\"!\nYou're logged in!"
          , "Info", INFORMATION_MESSAGE);
    
        isLogged = true;
        stage=2;
      } else
        showMessageDialog(null, "Password \"" + phrase + "\" mismatch!"
          , "Alert", ERROR_MESSAGE);
    
      return isLogged;
    }
    
  • It says saveStrings() expects parameters like "saveStrings(String,String[]").

    Have you read its reference already?: https://processing.org/reference/saveStrings_.html
    Some older threads w/ that: https://forum.Processing.org/two/discussions/tagged?Tag=savestrings()

  • Yes I have read all of those...So does that mean because my accounts is String[],String[] its not going to work?

  • If you got 2 String[], it's less of a hassle to just use 2 saveStrings() for 2 separate files.

  • edited April 2016

    The two Strings[] are like this tho, they are both apart of accounts, will this not work?

        final StringDict accounts = new StringDict(
              new String[] {
              "john", "tony", "amy"
              }
              , new String[] {
                "ilikedogs", "husky", "pepsi"
              }
              );
    
  • edited April 2016

    GoToLoop: dataPath() isn't even in the reference. What is that function doing again? I'm curious.

    Line 1:

    final StringDict accounts = new StringDict(...);

    Line 51:

    saveStrings(dataPath("file.txt"), accounts);


    StringDict is not a String[] array.
    saveStrings( String filePath, String[] fileToSave )

    Try: https://processing.org/reference/StringDict_valueArray_.html

    String[] colors = inventory.valueArray();

    or something like

    String[] names = accounts.keyArray();
    String[] entry= accounts.valueArray();
    String[] saveAccounts;
    for(int i=0; i<names.length; i++)
      saveAccounts[i] = names[i] + "," + entry[i];
    saveStrings(dataPath("file.txt", saveAccounts);
    
  • Im getting an EOF error with the for loop :/

  • edited April 2016

    ... dataPath() isn't even in the reference. What is that function doing again?

    It shows up again? Well, devs think we're too stupid to have such useful functions such as dataPath()!
    Although they're public, they're rather meant for library developers, not for us sketchers! [-(
    dataPath() and its cousin dataFile(), gets us the full path for the subfolder "data/".

  • edited April 2016

    GotToLoop: Man I sure need to pay more attention to the github.

    AmyM: Whoops, forgot the new String[];

      String[] names = accounts.keyArray();
      String[] entry= accounts.valueArray();
      String[] saveAccounts = new String[names.length]; // hehe
      for(int i=0; i<names.length; i++)
        saveAccounts[i] = names[i] + "," + entry[i];
      saveStrings(dataPath("file.txt"), saveAccounts);
    
Sign In or Register to comment.