Entering Data into jGrasp without using a GUI - java

My class got a Java programming project today in class and the program we are using is jGrasp, I know that I can handle the pieces of this project except for one aspect.
The project requires the user to enter data into the program (i.e. answer a question), but for this Semester we are not using GUIs so I can't create a GUI for the user to input the data or answer.
I'd like to know how could the user enter data into jGrasp without using a GUI?
Thanks for taking the time to read this post, and I'd greatly appreciate any help you could give me.

I hope I'm helpful :) it's funny that I want to know about the same thing in GUI & you want to know without it :D ....well i can solve your matter I believe :)
From the starting,
Example:
import java.util.*;
public class Assignment //class name
{
static Scanner stdIn = new Scanner(System.in);
public static void main(String[]args)
{
int A, B; //Intialize variables as normal
System.out.print("Enter a question you want to ask "); //Between the double quotes " " you type anything you want to type :)
A = stdIn.nextInt();
: //
: // complete your program
System.out.println("Add the statement for giving the result "+B); // "+B" for inserting the answer along with your result statement
}
}
Note: In the statements,
System.out.print();
System.out.println();
These statement work similarly.... just the difference is statement with "print" is displayed on the same line & Statement with "println" is displayed in next line....when you will try you will understand ...just for avoiding confusion why I have written so I'm mentioning it :)
I hope this helps...tczz :)

Related

ArrayList<Integer> is not storing user-inputted integers in Java 8 (1.8)

*EDIT - SOLVED: After instantiating the Scanner Object, I used a delimiter as follows:
scanner.useDelimiter("");
Prior to this, I did try a delimiter that looked something like this (the exact code is available on Stack Overflow):
scanner.useDelimiter("\\p{javaWhitespace}");
...but it didn't work very well.
Thank you, everyone. If you're having this very same issue, try the first delimiter. If it doesn't work, upgrade your JDK to 13 then try it again.
Ok, my goal is to have a user input a credit card number which I would then like to store in an ArrayList of Integers and subsequently pass this list to my functions which will perform the Luhn algorithm in order to validate the provided number. Once the user presses Enter, the processing begins. This is a console application, nothing fancy.
Everything works beautifully...except the user-input part. None of the user-input is being stored into the declared ArrayList. I've inserted a print message to give me the size of the list just after the pertinent while-loop and....yep, 0. I also pass this list into a custom lengthChecker(ArrayList<Integer> list){} function subsequent to the relevant while-loop and it's printing my custom error-message.
I have declared local int variables within the scope of the while-loop and that wasn't helping much. I have tried getting the user's input as Strings and storing them in an ArrayList<String> list; then parsing the input but that didn't work very well (especially as I need the Enter key to behave as a delimiter such that the next steps can take place)
Anyways, here is the code to the function in question. Am I missing something obvious or should I just quit programming?
public void userInput() {
Scanner scanner = new Scanner(System.in);
List<Integer> list = new ArrayList<Integer>();
System.out.println("Please input the card-number to be checked then press Enter: ");
while(scanner.hasNextInt()) {
list.add(scanner.nextInt());
}
System.out.println("Length of list: " + list.size());
listLengthChecker(list);
scanner.close();
}
Thank you in advance.
I don't have the full context on all the code you've written to be able to solve your problem, but I can guess at what's going on. If you want to run any user I/O (such as the scanner), it must occur within the main method. I can only assume that you run your userInput() function within the main method in your class. However, because your userInput() function doesn't have the static keyword in its definition, it can't be accessed without initialising an object of the class - but as far as I can tell from your code, there is no object that the method could refer to. Add the static keyword (i.e. initialise the method as public static void userInput()) to be able to run the function as you intend.
As for the while loop - there's a small chance that this is a difference in Java versions (I use Java 11), but while(scanner.hasNextInt()) won't stop being true at the end of your line or when you press enter - only when you insert something (such as a character) that cannot be interpreted as an integer.
This while loop untill you enter any non integer value.
You finished entering all the integer values and then your program will print your list elements.

How do I mask passwords in a command-line Java program with asterisks or the like?

The standard JVM method for reading a password from the command line without showing it is java.io.Console.readPassword(). This, however, shows nothing while the user is typing; users accustomed to graphical programs will expect symbols such as "•" or "*" to appear in place of the characters they type. Naturally, they will also want backspacing, inserting, and so on to work as normal, just with all the characters being operated on replaced with the same symbol.
In 2019, is there a generally accepted JVM procedure for showing "*******" when the user types "hunter2" in a console application? Can this even be done properly without a GUI? A 2011 SO question on the topic got an answer linking to this article on the topic; can we do better nowadays than the rather elaborate solution shown therein?
(I happen to be using Kotlin as my language of choice, so a Kotlin-specific solution will satisfy if there is one.)
hunter2? Wow. Reference acknowledged.
There is no easy way. The primary problem is that the standard System.in doesn't give you any characters at all until the user has pressed enter, so there's no way to emulate it (if you try to read char-for-char from System.in and emit a * every time a key is pressed, that won't work).
The lanterna library at https://github.com/mabe02/lanterna can do it, though. If you want to emulate it, it's.. very complicated. It has branching code paths for unix and windows. For example, on unix, it uses some hackery to figure out what tty you're on, and then opens the right /dev/tty device. With lanterna, writing this yourself would be trivial.
It's that or accept Console.readPassword()'s blank nothingness, really. Or, write a web interface or a swing/awt/javafx GUI.
I think answer to your question can be found here in stackoverflow itself.
please see this:
masking-password-input-from-the-console-java
sample code from there:
import java.io.Console;
public class Main {
public void passwordExample() {
Console console = System.console();
if (console == null) {
System.out.println("Couldn't get Console instance");
System.exit(0);
}
console.printf("Testing password%n");
char passwordArray[] = console.readPassword("Enter your secret password: ");
console.printf("Password entered was: %s%n", new String(passwordArray));
}
public static void main(String[] args) {
new Main().passwordExample();
}
}
hope this is helpful. :)

Scanner is running before any part and needs two input in order to run

package happy;
import java.util.Scanner;
public class PiVal {
public static void main(String[] args)
{
Scanner s = new Scanner(System.in); //initialise scanner
String val= Double.toString(Math.PI); //Storing PI to string
System.out.println(val);//Printing the stored value
System.out.println("Enter the value");//Screen output to print
int till =s.nextInt(); //taking user input storing in till
till+=2; //increasing value to +2 as only want to change after decima
if(till>15) //Checking if variable value more than 15
{
System.out.println("Out of bounds");
}
System.out.println(val.substring(0,till));
s.close();
}
}
My code looks like this very simple code i know just revising some stuffs but the thing i am not able to get my head around is why i need to input 2 times just to run the code and the "Print " command is not running very first it is running after the scanner input.
OutPut looks like this
8
8
3.141592653589793
Enter the value
3.14159265
Edit:
Eclipse LUNA
Seeing that the code produces the correct output for everyone else, there is something wrong with your IDE. Try running your program in another IDE and see if that helps. You should update Eclipse Luna(if any updates are available) and restart it(just quit and reopen). That should solve your issue, however if it does not, You may need to use a different Environment.
Refer to this link dude. It's always recommended by the developer of eclipse to use the most updated version of eclipse. But of course, it's not necessary all the time. However, luna is using version 4.4, while the second latest eclipse is already using 4.6 which is ahead by at least 2-3 years.
What you're facing could be some issues that no one really has officially brought up before. It's just how certain IDE works behind which programmers like us may not understand. It's similar to Quincy who doesn't follow the correct order during program runtime.

java jar accept multiple arguments on runtime

I have a java jar that need to accept about 3 arguments but I want to pass them as a Q&A type like the following:
1st step run java jar
java -jar myTest.java
2nd step ask questions and wait for answers:
Hi, how old are you?
I type my answer that accepts it and then ask the 2nd question:
nice! what is your name?
type my second answer and the get a third question and so on. how do I achieve this? I know that I can pass arguments to main but what I found is that I have to pass them all when I first run the jar not like what I'm looking for.
Any ideas?
Thanks!
the way to interact with an application depends of the logic in the application itself...
if you need to give parameters that the application needs from the start point then you give those as soon as you run the application
java -jar myTest.java
all those parameters are getting passed to the string[] parameter in the public static void main method...
in your case, (and if I got the question right) you will need more information from the user, and this is given at runtime... so you need another way to do that like Scanner class allowing you to read input from the terminal too...
Use the Scanner class and read the user input from the console..
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("What is your name?");
String input = sc.nextLine();
System.out.println("Hi, " + input + ", where are you from");
input = sc.nextLine();
System.out.println("Ohh, " + input + " is nice place I guess!");
System.out.println("and how old are you??");
...
}
It seems that you are mixing up concepts.
Command line arguments are those strings that you see as String[] args parameter to your main method!
But you want the user to provide "more" input to your application, the typical way is to read them from "stdin" (see here to learn how to do that).
Those two are fundamentally different things; and you should first clarify which one you really intend to use (given your example ... probably the "stdin" option).
Try reading the Java IO tutorial here:
https://docs.oracle.com/javase/tutorial/essential/io/cl.html

Website that allows me to properly run C++ code that uses cin?

Is there a website where I can run Java or C++ code online that lets me use cin in c++? Codepad and many others just let you see your program but don't let the user provide input, and I wanted to know if I could find a website that allows that.
Ideone doesn't seem to do the input right : This is supposed to be a game where you enter a word then the letter changes places, and then you need to figure out the word. The code is correct but the website doesnt read the cin right ! http://ideone.com/0PmDX
Ideone does just that this is also in the Info section of the c++ tag.
In your provided code: http://ideone.com/0PmDX you call return main(); which is undefined behaviour and is causing your infinite loops in your code. Try return 0 instead.
Code: http://ideone.com/ymXeH

Categories