Getting User input

Java Proccessing
OK, I’ve watched several youtube shows of getting the user’s input. Even though I didn’t like the way they did it I tried it since I have no other idea of how to do it. So in the game I’m making my high Score board & want the user’s name. The tutorials I’ve seen shows use Scanner but when I try it & type in my name nothing happens. Here the code.

import java.util.Scanner;

void setup(){
  size(300,300);
}

void draw(){
  Scanner in = new Scanner(System.in);
  System.out.print("Please enter your name ");
  String name = in.nextLine();
  System.out.println("Hello " +name + "Welcome to Java.");
}

Can anyone tell me why I’m not seeing my name after I type it?

Where are you asking for them to type in all that?

By invoking the methods from the class library. :sunglasses:

I’ve tried Scanner.
I’ve tried import iadt.creative.Inputs;
both not working.
There must be a simple way to do this.
Can you explain it instead of dropping files .I’ve looked at them tried copying to get what I want and still only errors.

@GoToLoop Please include explanations when you post links.

1 Like
  • The gists are presented as 2 files: 1 is a “.pde” & the other is a “.java” file.
  • The “.pde” goes to the 1st main tab.
  • The 2nd “.java” 1 needs to be created.
  • For example, “Inputs.java” contents should go to a tab named exactly “Inputs.java”.

I got a bunch of errors. I just wasted a day looking for a way to get input from a user I just run into error after error.

Here you can see an example suing the scanner: http://happycoding.io/examples/java/hello-world/higher-lower-random-computer-player

However, if you are using processing, key events would be handled by your sketch. So, with this in mind, you can do something like this (It will only work after the sketch gains focus aka. you need to click on the sketch before you start taping)

Kf



final String INIT_MSG="Start typing";
String msg=INIT_MSG;

void setup() {
  size(600, 400);
  
}

void draw() {
  background(0);
  fill(255);
  text(msg, 100, 100);
  
  fill(0,250,0);
  text("Mouse click to reset message", 100, height*0.8);
}

void mousePressed() {
  msg=INIT_MSG;  //Reset message, prepare for a new message
}

void keyPressed() {

  //Prepare when writing a new message. Next resets message container
  if (msg.equals(INIT_MSG)) {
    msg="";
  }

//Detects only alphanumeric chars
  if ( (key>='a' && key<='z') ||
    (key>='a' && key<='z') ||
    (key>='0' && key<='9') 
    ) {
    msg+=key;
  }
}

Simplest way is to use G4P GUI builder Tool and it’s library.

ty ty ty you are a life saver. I can add this

I looked into GUI JTEXTFolder or something like that & tried it & ran into errors when trying to import the library they said import

You need to make sure you add the jars to your sketch. The best thing to do is for you to provide your link for this library and tell us how you are managing your jars. If you use a Processing library like G4P or ContolP5, you can install any of them through the contribution manager. They come with examples to get you started. JOptionPane is another option, which comes with Java. You can check for previous examples in the older forum.

Kf

TY, I’m happy with the current way but I’ll look back into it. btw what are jars?

What is your current way?

Jars are library’s byte code ready to be used in your code. For example, Processing libs are found in the core.jar which is part of the Processing distribution, right when you download Processing from the website. For non-Processing libraries, you need to make sure those libraries are accessible by your code. If you build your own code, you will use the classpath flag. If you are using Processing to run your code, then you need to include the jar in your sketch. The easiest way is to drag and drop the jar right into the Processing editor. This will create a folder called code at the same level as your data folder. This code folder will contain the jar file(s) and they will be including during the building process for you. To access those function, you need to explicitly make those resources available via import statements, for instance import com.some.external.library.

You might find this next useful: http://happycoding.io/tutorials/processing/libraries

Kf

TY, I’ll try later. This was very useful. Why ain’t all the libraries already able to access?

java.util.Scanner doesn’t work in the Processing IDE. You have to use eclipse or a terminal.