How to see the data of a .CSV file

edited June 2017 in Questions about Code

Hi people!

I´m glad to be here, I find the forum really interessting despite that i´m a begginer. Now my question! :)

As I told you guys, I´m begginer and I´m trying to develop an "App" and to try to understand how it works. I would like to build an app to conjugate verbs in german... So, my idea is to type the verb, press enter and get the conjugations of the verb. I have in a .csv file some verbs in order to try that it works. In this moment I just did a brake to type the verb, then I press enter, but I don´t know how to conect it with the info in the table once enter is pressed...

For example, I would like to type the verb "sein", press enter and receive the conjugation:

Ich - bin Du - Bist bla bla bla....

=================================================================

//Verb Conjugation

      String myText = "Type here your verb: ";
      String input = myText;
      PFont font;

      void setup() {
          size(500,500);
          font = createFont("Sans Serif",40);
          textFont(font,20);
          textAlign(LEFT,CENTER);
       }

      void draw() {
          background(0);
          //variable,
          text(myText, 30,0, width,height);

       }

      void keyPressed() {
           if (keyCode == BACKSPACE) {
           if (myText.length() > 0) {
           myText = myText.substring(0, myText.length()-1);
       }

        } 
      else if (keyCode == DELETE) {
          myText = ""; 

        } 

      else if (keyCode != SHIFT) {
      //guarda la configuracion de la letra tecleada anteriormente  
      myText = myText + key;

        }

      if (keyCode == ' ') {
          myText = myText.substring(0, myText.length()-1);
          myText = myText + '_';}


      if (keyCode == ENTER) {
         input = myText;
         myText = "";
         }


      String[] lines = loadStrings("100 Verben.csv");
      println("there are " + lines.length + " lines");
      for (int i = 0 ; i < lines.length; i++) {
      println(lines[i]);

      }


          }

=================================================================

With this code, I can only type the verb without receive the answer, and I can see the list of the verbs in the console.... To be honest I don´t know how to give the next step.... I know that maybe exists a easier way to do it, but im trying to make it myself in order to learn. :D

Sorry in advance for my possible mistakes!

Thanks for your attention!

Cheers!!!!!

Answers

  • look at loadTable

    a Table has a lo of commands you can use

    https://www.processing.org/reference/

  • edited June 2017

    if you look at your approach, these lines belong into setup:

      String[] lines = loadStrings("100 Verben.csv");
      println("there are " + lines.length + " lines");
      for (int i = 0; i < lines.length; i++) {
        println(lines[i]);
      }
    

    but without String[] .

    Have String[] lines; before setup()

    remeber to hit ctrl-t in processing for auto-format

  • edited June 2017

    this works without Table:

    Please note that myText is not "Type here your verb: " but what is entered by the user.

    Also note I had to move the else if (keyCode == ENTER) { part up because if (keyCode != SHIFT) { applies to Enter as well so, enter would be added to myText which we don't want. No ENter is checked before if (keyCode != SHIFT) { so it works. Same applies for Space ' ' but I didn't change this. Best use if else throughout and not a mixture of if else and if... (meaning this: if (keyCode == ' ') { starts a new block which I don't recommend, better use else if (keyCode == ' ') { and also move it before if (keyCode != SHIFT) {)

    Also it's er/sie/es in German, not only er (3rd Pers Sing).

    Best, Chrisir ;-)

    String promptText = "Type here your verb: ";
    String myText="";
    String input = myText;
    String result=""; 
    PFont font;
    
    String[] lines ={
      "sein,bin,ist,ist", 
      "haben,habe,hat", 
      "werden,werde,wird"
    };
    
    void setup() {
      size(500, 500);
      font = createFont("Sans Serif", 40);
      textFont(font, 20);
      textAlign(LEFT, CENTER);
    
      //  String[] lines = loadStrings("100 Verben.csv");
      println("there are " + lines.length + " lines");
      for (int i = 0; i < lines.length; i++) {
        println(lines[i]);
      }
    }
    
    void draw() {
      background(0);
      //variable,
      text(promptText+myText, 30, 0, width, height);
      text(result, 30, 90);
    }
    
    void keyPressed() {
      if (keyCode == BACKSPACE) {
        if (myText.length() > 0) {
          myText = myText.substring(0, myText.length()-1);
        }
      } else if (keyCode == DELETE) {
        myText = "";
      } else if (keyCode == ENTER) {
        input = myText;
        // myText = "";
        for (int i = 0; i < lines.length; i++) {
    
          String [] components=split(lines[i], ','); 
    
          if (input.equals(components[0])) {
            // found 
            result=lines[i]; 
            return;
          }//if
        }
      } else if (keyCode != SHIFT) {
        //guarda la configuracion de la letra tecleada anteriormente  
        myText = myText + key;
      }
    
      if (keyCode == ' ') {
        myText = myText.substring(0, myText.length()-1);
        myText = myText + '_';
      }
    }
    //
    
  • edited June 2017

    the core with the search is line 43 to 52 where we split each line and compare input to the first component of the line

    the entire line is then copied into result which is shown in draw()

  • of course you need to put line 19 back in as

    lines = loadStrings("100 Verben.csv");
    

    and replace line 7 with String[] lines;

  • Thank you so much for your help! And thanks for your patience with the begginers like me. It is now more clear for me and also the organization is much better now... Despite that I did modify my program as you wrote, and despite that is clear, I can not do it :(

    I type any verb from the list, then I press enter but nothing happens... I´m sure that i´m making a small mistake but I can not see it (I have been trying for the past two hours :D ). Have a look:

            String promptText = "Type here your verb: ";
      String myText="";
      String input = myText;
      String result=""; 
      PFont font;
    
      String[] lines;
    
    
    
      void setup() {
          size(500, 500);
          font = createFont("Sans Serif", 40);
          textFont(font, 20);
          textAlign(LEFT, CENTER);
    
    
    
      lines = loadStrings("100 Verben.csv");
      println("there are " + lines.length + " lines");
      for (int i = 0; i < lines.length; i++) {
      println(lines[i]);
      }
        }
    
      void draw() {
          background(0);
          //variable,
          text(promptText+myText, 30, 0, width, height);
          text(result, 30, 90);
      }
    
      void keyPressed() {
          if (keyCode == BACKSPACE) {
          if (myText.length() > 0) {
          myText = myText.substring(0, myText.length()-1);
      }
      }   
      else if (keyCode == DELETE) {
      myText = "";
      }   
      else if (keyCode == ENTER) {
      input = myText;
      // myText = "";
      for (int i = 0; i < lines.length; i++) {
    
      String [] components=split(lines[i], ','); 
    
      if (input.equals(components[0])) {
        // found 
        result=lines[i]; 
        return;
      }//if
      }
      }   
      else if (keyCode != SHIFT) {
      //guarda la configuracion de la letra tecleada anteriormente  
      myText = myText + key;
      }
    
      if (keyCode == ' ') {
        myText = myText.substring(0, myText.length()-1);
        myText = myText + '_';
      }
      }
    

    //

    Regarding to er/sie/es in german, you´re totally right! I just want to make that my code works first and then I will modify the table properly :D It is a mixture between learn a new language and learn processing.

    ;)

  • It's case sensitive, you need to write Sein not sein

  • Use ctrl-t please

  • :D Sorry! forgot it! :D

  • edited June 2017

    In line 48 use println (components [0] );

    Does it give you sein and haben and so on ?

  • No, nothing :/ I can see the data in the console, but not in the window when I execute my code... :(

  • edited June 2017

    In line 48 use println (components [0] );

    Does it give you sein and haben and so on ?

    It should.

    is the separator really a , comma ?

  • else if (keyCode == ENTER||keyCode==RETURN) {
    

    is the code really as you have posted it?

    This works for me:

    String promptText = "Type here your verb: ";
    String myText="";
    String input = myText;
    String result=""; 
    PFont font;
    
    
    
    
    String[] lines ={
      "sein,bin,bist,ist", 
      "haben,habe,hast", 
      "werden,werde,wirst"
    };
    
    void setup() {
      size(500, 500);
      font = createFont("Sans Serif", 40);
      textFont(font, 20);
      textAlign(LEFT, CENTER);
    
    
    
      // lines = loadStrings("100 Verben.csv");
      println("there are " + lines.length + " lines");
      for (int i = 0; i < lines.length; i++) {
        println(lines[i]);
      }
    }
    
    void draw() {
      background(0);
      //variable,
      text(promptText+myText, 30, 0, width, height);
      text(result, 30, 90);
    }
    
    void keyPressed() {
      if (keyCode == BACKSPACE) {
        if (myText.length() > 0) {
          myText = myText.substring(0, myText.length()-1);
        }
      } else if (keyCode == DELETE) {
        myText = "";
      } else if (keyCode == ENTER) {
        input = myText;
        // myText = "";
        for (int i = 0; i < lines.length; i++) {
    
          String [] components=split(lines[i], ','); 
    
          if (input.equals(components[0])) {
            // found 
            result=lines[i]; 
            return;
          }//if
        }
      } else if (keyCode != SHIFT) {
        //guarda la configuracion de la letra tecleada anteriormente  
        myText = myText + key;
      }
    
      if (keyCode == ' ') {
        myText = myText.substring(0, myText.length()-1);
        myText = myText + '_';
      }
    }
    
  • edited June 2017
    • Made my own version for it. It stores the loaded ".csv" rows into a HashMap:
      https://Processing.org/reference/HashMap.html
    • The key is the infinitive form of a German verb as a String.
    • While the value is that same verb plus all of its 6 conjugation present tense forms as a String[].
    • BtW, the CSV file is called: "verben.csv".
    • In order to request a user's input, JS' Window::prompt() is emulated in "Java Mode" via Java's JOptionPane::showInputDialog():
    1. https://developer.Mozilla.org/en-US/docs/Web/API/Window/prompt
    2. http://docs.Oracle.com/javase/8/docs/api/javax/swing/JOptionPane.html#showInputDialog-java.lang.Object-
    3. https://forum.Processing.org/two/discussion/12532/windowjs-cross-mode-alert-confirm-prompt-other-js-api-for-java
    • Method prompt() is in a class called window in the tab called "window.java".
    • And then imported into main "Verb_Conjugation.pde" sketch via import static js.window.prompt;
    • File "window.java" is necessary only for "Java Mode" though.
    • When run in "JavaScript Mode" (Pjs) that's not used, b/c JS already got that function natively!
    • There's also tab "TableUtils.pde", w/ functions to create the requested verb as a table String, then showed at the canvas.
  • edited June 2017

    Here comes the 3 sketch files and the ".csv" file used. :-bd

    You can see it online going to the link below: :bz
    https://OpenProcessing.org/sketch/436121

    Any doubts about my sketch version, just ask here in your forum thread: :-B

    verben.csv:

    Verb,Ich,Du,Er/Sie/Es,Wir,Ihr,Sie
    sein,bin,bist,ist,sind,seid,sind
    haben,habe,hast,hat,haben,habt,haben
    werden,werde,wirst,wird,werden,werdet,werden
    

    Verb_Conjugation.pde:

    /** 
     * Verb Conjugation (v1.0.3)
     * by GoToLoop (2017/Jun/26)
     *
     * forum.Processing.org/two/discussion/23200/
     * how-to-see-the-data-of-a-csv-file#Item_15
     *
     * OpenProcessing.org/sketch/436121
     */
    
    import static js.window.prompt;
    import java.util.Map;
    
    static final boolean JAVA = 1/2 != 1/2.;
    static final int FONT_SIZE = 20;
    
    static final String FONT = JAVA? "DejaVu Sans Mono Bold" : "monospace";
    static final String LF = "\n", TB = "\t", SPC = " ";
    static final String FILENAME = "verben.csv";
    
    static final String INPUT = "Please enter some verb to see its conjugation:";
    static final String EMPTY = "Your input was empty!";
    static final String MISS  = "\" isn't available in our verb database, sorry!";
    
    final Map<String, String[]> verbs = new HashMap<String, String[]>();
    String[] titles;
    byte[] titlesLens, verbenLens;
    
    void setup() {
      size(1000, 200);
      smooth(3);
      noLoop();
      frameRate(60);
    
      if (JAVA)  blendMode(REPLACE);
      colorMode(RGB);
      fill(#FFFF00);
      background(#008000);
    
      textMode(MODEL);
      textAlign(CENTER, CENTER);
      textFont(createFont(FONT, FONT_SIZE, true));
    
      final String[] csv = loadStrings(FILENAME);
      titles = split(csv[0], ',');
      println(titles);
    
      final int qty = titles.length;
      titlesLens = grabLengths(titles, new byte[qty]);
      verbenLens = new byte[qty];
    
      for (int len = csv.length, i = 0; ++i < len; ) {
        final String[] row = split(csv[i], ',');
        verbs.put(row[0], row);
      }
    
      for (final String verb : verbs.keySet())  print(verb + SPC + TB);
      println(LF);
    }
    
    void draw() {
      background(#0000FF);
      final int cx = width >> 1, cy = height >> 1;
    
      final String input = prompt(INPUT);
      if (input == null)  exit();
      else if (input.length() == 0)  text(EMPTY, cx, cy);
      else {
        final String verb = split(input.trim(), ' ')[0].toLowerCase();
        final String[] verben = verbs.get(verb);
        if (verben == null)  text("\"" + verb + MISS, cx, cy);
        else {
          final String s = tableDisplay(titles, titlesLens, verben, verbenLens);
          text(s, cx, cy);
          println(s);
        }
      }
    }
    
    void mousePressed() {
      redraw();
    }
    
    void keyPressed() {
      mousePressed();
    }
    

    TableUtils.pde:

    static final String tableDisplay(
      final String[] titles, final byte[] tLens, 
      final String[] verben, final byte[] vLens) {
    
      grabLengths(verben, vLens);
      neededLengths(tLens, vLens);
    
      final String titlesRow = rowDisplay(titles, vLens);
      final String verbenRow = rowDisplay(verben, vLens);
    
      final String ruler = repeatString("-", titlesRow.length() - 1) + LF;
      return ruler + titlesRow + ruler + verbenRow + ruler;
    }
    
    static final byte[] grabLengths(final String[] words, final byte[] lens) {
      for (int len = lens.length, i = 0; i < len; )
        lens[i] = (byte) words[i++].length();
      return lens;
    }
    
    static final byte[] neededLengths(final byte[] src, final byte[] dst) {
      for (int len = dst.length, i = 0; i < len; ++i)
        if (dst[i] < src[i])  dst[i] = src[i];
      return dst;
    }
    
    static final String rowDisplay(final String[] words, final byte[] lens) {
      String txt = "| ", sep = SPC + txt;
      for (int len = lens.length, i = 0; i < len; ++i)
        txt += addSpaces(words[i], lens[i]) + sep;
      return txt.trim() + LF;
    }
    
    static final String addSpaces(final String word, final byte len) {
      return word + repeatString(SPC, len - word.length());
    }
    
    static final String repeatString(final String str, int qty) {
      String spaces = "";
      qty = abs(qty);
      for (int i = 0; i++ < qty; spaces += str);
      return spaces;
    }
    

    window.java:

    /**
     * Name:      WindowJS
     * Version:   1.2.1
     * Language:  Java 5
     *
     * Author:    GoToLoop
     * Date:      2015/Sep/15
     * License:   LGPL 2.1
     */
    
    // gist.GitHub.com/GoToLoop/8017bc38b8c8e0a5b70b
    
    package js;
    
    import static javax.swing.JOptionPane.*;
    
    //static // Uncomment out static for when ".pde" and comment for ".java".
    public abstract class window {
      public static final window
        self = new window() {
      }
      , window = self;
      public static final Object undefined = null;
      public static final float NaN = Float.NaN;
      public static final float Infinity = Float.POSITIVE_INFINITY;
      public static String name = "";
    
      public static Object alert() {
        return alert(null);
      }
    
      public static Object alert(final Object msg) {
        showMessageDialog(null, msg);
        return null;
      }
    
      public static boolean confirm() {
        return confirm(null);
      }
    
      public static boolean confirm(final Object msg) {
        return showConfirmDialog(null, msg, "Confirm?", OK_CANCEL_OPTION)
          == OK_OPTION;
      }
    
      public static String prompt() {
        return prompt(null);
      }
    
      public static String prompt(final Object msg) {
        return showInputDialog(msg);
      }
    
      public static String prompt(final Object msg, final Object val) {
        return showInputDialog(msg, val);
      }
    
      public static String Date() {
        return new java.util.Date().toString();
      }
    
      public static class Date extends java.util.Date {
        public static long now() {
          return new java.util.Date().getTime();
        }
      }
    
      public static Object[] Array(final int len) {
        return new Object[len];
      }
    
      @SafeVarargs public static <T> T[] Array(final T... arr) {
        return arr == null? (T[]) new Object[0] : arr;
      }
    }
    
  • Wow!!! guys! Thanks a lot for the help. There is a lot of new things for me that I have to digest :) GoToLoop, Your on-line example is awesome!!! That is what I would like to reach... but I´m still trying to make that my code works :D, as soon as I have done my code, I will post it here to share my experience... It will take a while for me! Thanks again!

  • Your on-line example is awesome!!!

    Don't forget it also works offline in PDE's "Java Mode" too. It is cross-mode! $-)

Sign In or Register to comment.