Do / whille is not working how it suppose to.

edited November 2015 in Library Questions

Hello everyone who read this Question. Yesterday we were told at school to make a program that read a string from keyboard and than print the string. The library I have imported and "Inputs.readString("Enter your age");" help me to add the string from my keyboard creating a box where I can acutally type what string x will get. The box should pop up every time i close is or not leave it black. The problem is that even if I give to x a string, the box still pop up.. unsless i stop the program. Is this a bug into the library or is just me coding it wrong? There you can get the library: https://github.com/aerrity/Inputs

import iadt.creative.Inputs;
String x;
do
{
  x=Inputs.readString("Enter your age");
  println(x);
}while(x!=null);

Answers

  • It's going to be pretty hard to help you, since we don't have access to the Inputs.readString() function.

    However, what you described is exactly what I would expect this loop to do. Notice that you're looping while x is not null, which will always be true when the user enters some text.

  • edited October 2015

    The Inputs.readString() function is on the link i give. The problem is, when I do while(x==null) or whille(x.equals(null)) and close the box, it wont show the box again to add the string. Somehow it exits the do/while loop.

  • What exactly does the Inputs.readString()return? Does it return null, or does it return an empty String ""?

  • edited July 2018

    Class Inputs is a collection of static utility functions which wrap up Java's JOptionPane functionality:

    This is readString() btW if some1 is too lazy to check that out:

    public static String readString(String textLabel){
      JTextField field  = new JTextField(20);
      JLabel label = new JLabel(textLabel);
      JPanel p = new JPanel(new BorderLayout(5, 2));
      p.add(label, BorderLayout.WEST);
      p.add(field);
      JOptionPane.showMessageDialog(null, p, "Input required", JOptionPane.PLAIN_MESSAGE, null);
      String in = field.getText();
      return in; 
    }
    
  • That is a good question, when you try to print the string x, for example, in this case prints nothing. ! help The function itself is helping me to attribute the value/string to the variable.

  • @GoToLoop: I'm not lazy. I'm trying to help OP work through this.

    @portarul23: Try using print statements to check the value of the String. What happens if you do println(x.equals(""))?

  • edited October 2015

    true if i close the box, false if i type something into the box

  • Okay I figured out. It seems to work with this code

    import iadt.creative.Inputs;
    String x=null;
    do
     { x=Inputs.readString("Enter your age");
    
    
     }while(x.equals(""));
    

    Thanks a lot for you help. And sorry for keeping you too long on this thing.

  • edited October 2016 Answer ✓

    Glad you made it! :D
    Here's my own attempt after copy & paste some of "Inputs.java" and modify it a bit: :ar!

    // http://forum.Processing.org/two/discussion/13175/
    // do-whille-is-not-working-how-it-suppose-to#Item_9
    
    // https://GitHub.com/aerrity/Inputs/blob/master/src/Inputs.java
    // https://gist.GitHub.com/GoToLoop/bba0c288aaeeb5ef9bb1
    
    // GoToLoop (2015-Oct-22)
    
    void setup() {
      frame.setVisible(false);
    
      byte age;
      do age = readByte("Enter your age:");
      while (age < 0);
    
      println("Your age is:", age);
      exit();
    }
    
    import static javax.swing.JOptionPane.*;
    import javax.swing.JTextField;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import java.awt.BorderLayout;
    
    static final String readString(String textLabel) {
      JTextField field = new JTextField(20);
      JLabel label = new JLabel(textLabel);
      JPanel p = new JPanel(new BorderLayout(5, 2));
    
      p.add(label, BorderLayout.WEST);
      p.add(field);
    
      showMessageDialog(null, p, "Input required!", QUESTION_MESSAGE);
      return field.getText();
    }
    
    static final byte readByte(String label) {
      String in = readString(label);
    
      try {
        return Byte.parseByte(in);
      }
    
      catch (NumberFormatException ex) {
        return Byte.MIN_VALUE;
      }
    }
    
  • @GoToLoop: Want to do my homework next?

  • what happens if you escape or cancel the dialogue box? do you still get an empty string?

    println(x);

    put a delimiter around your output, catches a lot of errors (including trailing whitespace)

    println("[" + x + "]");

    doesn't help when you've got a string containing "null" though (not a null string, a string containing the word "null")

  • edited July 2017

    A more elaborate version for the "Inputs.java" library: :bz

    "Keyboard_Input_Library.pde"

    /**
     * Keyboard Input Library
     * Andrew Errity v0.2 (2015-Oct-01)
     * GoToLoop v1.0.2 (2015-Oct-22)
     *
     * https://Forum.Processing.org/two/discussion/13175/
     * do-whille-is-not-working-how-it-suppose-to#Item_12
     *
     * https://GitHub.com/aerrity/Inputs/blob/master/src/Inputs.java
     * https://Gist.GitHub.com/GoToLoop/bba0c288aaeeb5ef9bb1
     */
    
    import static iadt.creative.Inputs.*;
    
    void setup() {
      getSurface().setVisible(false);
    
      byte age;
      do {
        while ((age = readByte("Enter your age:")) < 0);
        println("Your age is:", age);
      } while (readBoolean("Again?"));
    
      exit();
    }
    


    "Inputs.java"

    /**
     * Keyboard Input Library
     * Andrew Errity v0.2 (2015-Oct-01)
     * GoToLoop v1.0.2 (2015-Oct-22)
     *
     * https://Forum.Processing.org/two/discussion/13175/
     * do-whille-is-not-working-how-it-suppose-to#Item_12
     *
     * https://GitHub.com/aerrity/Inputs/blob/master/src/Inputs.java
     * https://Gist.GitHub.com/GoToLoop/bba0c288aaeeb5ef9bb1
     */
    
    package iadt.creative;
    
    import javax.swing.JDialog;
    import javax.swing.JOptionPane;
    import static javax.swing.JOptionPane.*;
    
    import javax.swing.JTextField;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import java.awt.BorderLayout;
    
    public class Inputs {
      public static final String TITLE = "Input required!", CHOOSE = "Choose one:";
      public static final int CHARS = 25;
    
      public static final JTextField field = new JTextField(CHARS);
      public static final JLabel label = new JLabel();
      public static final JPanel panel = new JPanel(new BorderLayout(5, 2));
    
      static {
        panel.add(label, BorderLayout.WEST);
        panel.add(field);
      }
    
      public static final JDialog dialog = new JOptionPane(panel, QUESTION_MESSAGE) {
        @Override public void selectInitialValue() {
          field.requestFocusInWindow();
        }
      }
      .createDialog(null, TITLE);
    
      public static String readString(String txt) {
        label.setText(txt);
        field.setText("");
    
        dialog.setVisible(true);
        return field.getText();
      }
    
      public static boolean readBoolean(String label) {
        return showConfirmDialog(null, label, CHOOSE, YES_NO_OPTION) == YES_OPTION;
      }
    
      public static byte readByte(String label) {
        return readByte(label, Byte.MIN_VALUE);
      }
    
      public static byte readByte(String label, int failValue) {
        try {
          return Byte.parseByte(readString(label));
        }
        catch (NumberFormatException ex) {
          return (byte) failValue;
        }
      }
    
      public static short readShort(String label) {
        return readShort(label, Short.MIN_VALUE);
      }
    
      public static short readShort(String label, int failValue) {
        try {
          return Short.parseShort(readString(label));
        }
        catch (NumberFormatException ex) {
          return (short) failValue;
        }
      }
    
      public static int readInt(String label) {
        return readInt(label, Integer.MIN_VALUE);
      }
    
      public static int readInt(String label, int failValue) {
        try {
          return Integer.parseInt(readString(label));
        }
        catch (NumberFormatException ex) {
          return failValue;
        }
      }
    
      public static long readLong(String label) {
        return readLong(label, Long.MIN_VALUE);
      }
    
      public static long readLong(String label, long failValue) {
        try {
          return Long.parseLong(readString(label));
        }
        catch (NumberFormatException ex) {
          return failValue;
        }
      }
    
      public static float readFloat(String label) {
        return readFloat(label, Float.MIN_VALUE);
      }
    
      public static float readFloat(String label, float failValue) {
        try {
          return Float.parseFloat(readString(label));
        }
        catch (NumberFormatException ex) {
          return failValue;
        }
      }
    
      public static double readDouble(String label) {
        return readDouble(label, Double.MIN_VALUE);
      }
    
      public static double readDouble(String label, double failValue) {
        try {
          return Double.parseDouble(readString(label));
        }
        catch (NumberFormatException ex) {
          return failValue;
        }
      }
    }
    
  • @KevinWorkman The OP would have gained a lot from your input. Not only did he manage to reach a solution he will have gained knowledge of finding the solution. Nice one! =D>

  • Wow. So you decompiled what my teacher made so I can understand better how the Input work. Thanks a lot for that. I will take a look and try to figure out those things there. I have no words to thank you for this.

  • So you decompiled what my teacher made

    There is no "decompiling" necessary. The entire code is available at the link you gave us: https://github.com/aerrity/Inputs/blob/master/src/Inputs.java

    Here is the entirety of the readString() function you're using:

     public static String readString(String textLabel){
        JTextField field  = new JTextField(20);
        JLabel label = new JLabel(textLabel);
        JPanel p = new JPanel(new BorderLayout(5, 2));
        p.add(label, BorderLayout.WEST);
        p.add(field);
        JOptionPane.showMessageDialog(null, p, "Input required", JOptionPane.PLAIN_MESSAGE, null);
        String in = field.getText();
        return in; 
      }
    

    You can also use the Java API to figure out what this will return if the user cancels or doesn't enter any text. The component is a JTextField, so its getText() function will return an empty String instead ofnull`.

    I have no idea what GoToLoop is trying to help you learn, all I can see is that he's needlessly complicating your teacher's code. I wouldn't recommend using it as a learning tool, but it's really up to you.

  • Now I figured out what my lecturer made for us.. so now I am able to make an Input box without importing my teacher library. Thanks so much to all of you. Not only that you helped me solve my exercise, but you also helped me to figure out how he made the library and how it is working. I am new on this program and I have never used Java. I have only used C++ for like 4 years, so for me, everything looks like C++ combined to Jave...

  • Keep in mind that your teacher might want you to use his libraries.

Sign In or Register to comment.