Edit a given String over the Console in Java - java

I'm aware of various methods for reading a String from the user's keyboard in Java (Scanner, Console, BufferedReader), however, none of them seems to be capable to accept a given String that the user can edit.
To rephrase it in code, I'm looking for something like this:
Scanner sc = new Scanner(System.in);
System.out.print("Please edit as you like: " + s);
s = sc.nextLine(s);
As this seems so simple, am I overlooking something? Or is this really not possible in Java without a GUI?

Doesn't look like it will be possible. There are similar posts discussing this. Java: populating Scanner with default value on Scanner.nextLine();
A few options have been discussed in there but even the people providing those options are clear that it doesn't meet the stated requirment

If you are trying to edit an existing String, then it's not possible. String is immutable, you can use StringBuilder or create a new String with the new value.

Related

Java: On the command line, present the user with an editable option [duplicate]

I'm aware of various methods for reading a String from the user's keyboard in Java (Scanner, Console, BufferedReader), however, none of them seems to be capable to accept a given String that the user can edit.
To rephrase it in code, I'm looking for something like this:
Scanner sc = new Scanner(System.in);
System.out.print("Please edit as you like: " + s);
s = sc.nextLine(s);
As this seems so simple, am I overlooking something? Or is this really not possible in Java without a GUI?
Doesn't look like it will be possible. There are similar posts discussing this. Java: populating Scanner with default value on Scanner.nextLine();
A few options have been discussed in there but even the people providing those options are clear that it doesn't meet the stated requirment
If you are trying to edit an existing String, then it's not possible. String is immutable, you can use StringBuilder or create a new String with the new value.

'Inheriting' function parameters

I think this is kinda language-agnostic, but I'd like to know how this feature works in Java.
Let's suppose I have a function that reads a file using a Scanner:
void printFile(Scanner s) {
while (s.hasNextLine()) {
String line = s.nextLine();
System.out.println(line);
}
}
So, if I have several ways to simulate a file, using this function would be
String content = "foo\nbar\n";
printFile(new Scanner(content));
File file = new File("my/file.txt");
printFile(new Scanner(file));
InputStream input = new URL("http://www.somewebsite.com/a.txt").openStream();
printFile(new Scanner(input));
My question is, if I have several ways to simulate this file, how do I make it easier to use this function without having to enter new Scanner() every time I call it?
Some observations:
This would be useful in two ways: ease of writing unit tests and simplifying the API for the user.
Overloading the function solves the problem, however if the original function has several overloads, this could make the code ugly and/or complicate writing code, where the developer would have to read the documentation, write a corresponding overload and repeat.

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

Is there a way to make reference names an input in java? [duplicate]

This question already has an answer here:
How to use java.util.Scanner to correctly read user input from System.in and act on it?
(1 answer)
Closed 8 years ago.
I've been trying to build an Elo style ranking system for myself and a few friends so that we could keep track of our performance in just our games together. But I've been having some trouble with what to call the references to both the players and the game objects to keep it as readable as possible. Intuitively, it seems that the reference to the game should be the game number, and the reference to the player should be the playername. Yet as the specifics of each change every game, I can't hardwire these things into the code. So, is there a way to make the object references a user input?
Here are a few things I've tried that have made the compiler yell at me:
Scanner input = new Scanner(System.in);
Game input.next() = new Game(); //doesn't work
String playername;
for(int i = 0; i < numberOfPlayers; ++i){
System.out.print("Player name: ");
playerName = input.next();
Player playerName = new Player(); //compiler still isn't happy
}
//then there's also this-
String playerName = input.next();
Player /* ????? */ = new Player(Playername);
//it seems like a good idea at first, but it doesn't solve the problem at all.
If there is a way to do what I'm trying to get at here, I have no clue how to do it. And if there isn't a way to do this, what am I supposed to call the variables that isn't a completely ambiguous/unreadable reference name?
EDIT: This question was apparently marked as a duplicate of another question asking how to use System.in with the scanner correctly for a variety of things, not including reference names. As this question is asking a completely different thing(this question is asking primarily about reference names, not System.in or Scanner), I assume that the duplicate mark is either an amusing troll or a misunderstanding of my question.
Maybe the Map data structure will serve you better. Take a look at this code and see if it helps you. You may store identifiers as the key and reference to the game or players as the value. Note: If you enter the same key twice you rewrite its value.
Scanner input = new Scanner(System.in);
HashMap<String, Game> games = new HashMap<String, Game>();
HashMap<String, Player> players = new HashMap<String, Player>();
games.put(input.next(), new Game());
players.put(input.next(), new Player());
//do something with game and player properties
System.out.println(games.get("game 1"));
System.out.println(players.get("john"));

Java: The right way to make a scanner

First I'm a noob to Java so don't be mad at me if I'm acting stupid - Thanks.
As I said I'm trying to learn Java. Right now I'm trying to learn the right scanner for this mini-game, but I'm getting confused because people tell me to do it in two different ways. I just wan't to know which one to use and where I can use the other one.
First Example:
Scanner input = new Scanner(System.in);
Second Example:
Scanner input = new Scanner(System.in);
String userInput = input.nextLine();
Please tell me how to make the " right " scanner for my mini-game and explain when I should use the other one.
If you know which one to use, another way to create a scanner for this or just wanna share the scanners and how to use them - then please add it as an answer.
Scanner input = new Scanner(System.in);
This is calling a scanner and telling that it should be used in the console "System.in".
String userInput = input.nextLine();
This line is taking the value u inserted in the console and saving in a variable named "userInput"
You can add this System.out.println("the inserted value is : " + userInput);
And it will print in the console the value you inserted
If I'm reading your question correctly, both of your examples are same as far as creating a Scanner object is concerned. Only difference is that second example is also storing the nextLine of input into a String variable called userInput.
Look here to understand Scanner class better:
http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

Categories