What if I want to take user input from the args[0] array, but just in case I (the user) forgot to define it, I wanted a prompt to come up - is it better to use an if block to determine whether the array item(s) is empty or not, or to catch the exception? So, is this
public class Stuff {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
String foo;
if(args.length > 0) {
foo = args[0];
}
else {
foo = getString("Input? ");
}
}
public static String getString(String prompt) {
System.out.print(prompt + " ");
String answer = input.nextLine();
return answer;
}
}
better or worse than
public class Stuff {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
String foo;
try {
foo = args[0];
}
catch(ArrayIndexOutOfBoundsException e) {
foo = getString("Input? ");
}
}
public static String getString(String prompt) {
System.out.print(prompt + " ");
String answer = input.nextLine();
return answer;
}
}
Your first example will still throw an exception since in the if statement you are still accessing an index which does not exist.
If you want to use an if statement then you should be checking that the length of the array is greater than the index you are trying to access for example:
if(args.length > 0)
foo = args[0];
You need to test args.length rather than reading args[0]
But apart from that error, it is better to use if/else for the following reasons:
It makes the code clearer
It has better performance (although not relevant in this case)
There is no exceptional condition (it is expected that the args array may be empty in some circumstances e.g. "user error", so the code should be able to handle it). Exception throwing should be reserved for exceptional situations that shouldn't happen.
IMO, if-else is better/faster in this case.
Throwing an exception is used when you are inside a method and you want to say to the caller that something went wrong, and you can't do it with the return value.
But as said Jon Taylor, your if statement won't work that way.
All you really need is a single line of code.
final String foo = args.length > 0? args[0] : getString("Input? ");
Use if block to check whether array is empty that is easy and faster.
Given the correction evidenced by Jon Taylor, I will prefer the version with if.
Not only for the speed gain (that in your example I guess will be not noticeable), but because the code with the if better explains its intents, simplifying future maintenance on the code.
Related
I came up with a problem while trying to write a JUnit test for one specific method.
I searched for possible solutions but many of them were not helpful as the output was not dependent on the input. Any help would be much appreciated.
My class method looks like this:
public static void method1() {
Scanner input = new Scanner(System.in);
String state = input.nextLine();
if( /* condition dependent on state value */ ) {
System.out.println("...");
} else {
System.out.println("..."+state+"...");
}
}
How to write a JUnit test for it, can Robot class somehow solve the problem?
If you extract the logic to a separate function like
public static void method1() {
Scanner input = new Scanner(System.in);
String state = input.nextLine();
System.out.println(processingLogic(state));
}
static String processingLogic(String state) {
if ( /* condition dependent on state value */) {
return "some value";
} else {
return "some other value";
}
}
you can write a unit test for that function to see that it works correctly.
You can manipulate System.in and System.out using System.setIn() etc. For instance
System.setIn(new ByteArrayInputStream("test data".getBytes()));
However, I have the feeling that for your case, changing your method in a way that #p-j-meisch suggested in https://stackoverflow.com/a/59513668/2621917 is much better.
I want to take input from user and then want to find is it Integer, float or something else. Till now I am using Scanner class for that. Like Scanner scan1=new Scanner(System.in);
String st= scan1.next(); and then I am trying to parse that input (st) in int and float respectively. But with this approach I am not able to satisfy the "else" condition. i.e. when input is neither string, int nor float.
Below is the code I tried :-
public String checkInput() {
String statement = "This input is of type ";
String inputType;
Scanner scan1 = new Scanner(System.in);
String st = scan1.next();
scan1.close();
inputType = st.getClass().getSimpleName();
try {
Float numFloat = Float.parseFloat(st);
inputType = numFloat.getClass().getSimpleName();
} catch (NumberFormatException e) {
}
try {
Integer numInt = Integer.parseInt(st);
inputType = numInt.getClass().getSimpleName();
} catch (NumberFormatException e) {
}
return statement + inputType;
}
here I am not able to decide how should I place else condition to check the input is neither string,float nor integer.
Here's how I would restructure your code. I'll explain the changes below:
private static final String STATEMENT = "This input is of type ";
public static String checkInput(Scanner scanner) {
if (scanner.hasNextFloat()) {
return STATEMENT + Float.class.getSimpleName();
else if (scanner.hasNextInt()) {
return STATEMENT + Integer.class.getSimpleName();
}
return STATEMENT + "UNKNOWN";
}
First, we pulled statement out into a constant, since it's not being changed.
Second, we pass the Scanner in as a parameter, rather than constructing a new one. There are a number of reasons to prefer this, but the principle one is that you should avoid creating multiple Scanner instances reading from System.in - generally you'll create such a Scanner in your main() method and pass it off to the methods and classes that need to use it.
Next, rather than reading from the scanner directly, we use the has*() methods to inspect the scanner's state without advancing it. This changes the semantics of checkInput(), because when it returns the input being inspected is still in the scanner, but that's more consistent with a method named check...() - it should inspect, not change state. Because your implementation calls .next() you lose the actual provided input, which is presumably undesirable.
Finally, we return from inside each block, rather than setting a temporary inputType variable and returning it at the end.
Your main() method might now look like this:
public static void main(String[] args) {
// using try-with-resources so we don't have to call .close()
try (Scanner scanner = new Scanner(System.in)) {
System.out.println(checkInput(scanner));
String input = scanner.next(); // this actually advances the scanner
System.out.println("You input: " + input);
}
}
Taking it further, you might prefer to have checkInput() return a Class<?> rather than a String, and then construct your statement separately. This would allow you to handle the inputs differently. For example:
public static Class<?> inputType(Scanner scanner) {
if (scanner.hasNextFloat()) {
return Float.class;
else if (scanner.hasNextInt()) {
return Integer.class;
}
// add other types as needed
return String.class;
}
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
Class<?> inputType = inputType(scanner);
String input = scanner.next();
System.out.println("You input: " + input);
if (inputType.equals(Integer.class)) {
System.out.prinln("That's a valid integer!");
}
}
}
All that said, we're very much reinventing the wheel here. The "right" way to use Scanner is to use the typed methods directly - e.g.:
if (scanner.hasNextInt()) {
int value = scanner.nextInt();
}
This avoids needing to do any manual type checking or similar busywork - just let Scanner do the validation for you.
So i'm using IntelliJ and the replace buzzword is highlighted. Most of the tips are over my head so i ignore them, but what i got for this one was that the result of string.replace is ignored. Why?
would i need something like ( string = string.replace(string.charAt(i));)?
import java.util.Scanner;
public class PhoneNumberDecipher {
public static String phoneNumber;
public static String Decipher(String string) {
string = phoneNumber;
for(int i =0; i<=phoneNumber.length(); i++) {
if(string.equalsIgnoreCase("A")
||string.equalsIgnoreCase("B")
||string.equalsIgnoreCase("C")) {
string.replace(string.charAt(i),'2')
}
else if(string.equalsIgnoreCase("D")
||string.equalsIgnoreCase("E")
||string.equalsIgnoreCase("F")) {
string.replace(string.charAt(i),'3');
}
else if(string.equalsIgnoreCase("G")
||string.equalsIgnoreCase("H")
||string.equalsIgnoreCase("I")) {
string.replace(string.charAt(i),'4');
}
else if(string.equalsIgnoreCase("J")
||string.equalsIgnoreCase("K")
||string.equalsIgnoreCase("L")) {
string.replace(string.charAt(i),'5');
}
else if(string.equalsIgnoreCase("M")
||string.equalsIgnoreCase("N")
||string.equalsIgnoreCase("O")) {
string.replace(string.charAt(i),'6');
}
else if(string.equalsIgnoreCase("P")
||string.equalsIgnoreCase("Q")
||string.equalsIgnoreCase("R")
|| string.equalsIgnoreCase("S")) {
string.replace(string.charAt(i),'7');
}
else if(string.equalsIgnoreCase("T")
||string.equalsIgnoreCase("U")
||string.equalsIgnoreCase("V")) {
string.replace(string.charAt(i),'8');
}
else if(string.equalsIgnoreCase("W")
||string.equalsIgnoreCase("X")
||string.equalsIgnoreCase("Y")
||string.equalsIgnoreCase("Z")) {
string.replace(string.charAt(i),'9');
}
}
return string;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please Enter a Phone Number you Wish to Decipher...");
phoneNumber = input.nextLine();
System.out.print(Decipher(phoneNumber));
}
}
String objects are immutable.
From the docs:
public String replace(char oldChar,char newChar)
Returns a string resulting from replacing all occurrences of oldChar in this string with newChar.
Hope this helps.
IntelliJ is complaining that you're calling a method whose only effect is to return a value (String.replace) but you're ignoring that value. The program isn't doing anything at the moment because you're throwing away all the work it does.
You need to use the return value.
There are other bugs in there too. You might be able to progress a little further if you use some of this code:
StringBuilder convertedPhoneNumber = new StringBuilder();
// Your loop begins here
char curCharacter = phoneNumber.charAt(i);
if (curCharacter == 'a') {
convertedPhoneNumber.append("2");
}
// More conditional logic and rest of loop goes here.
return convertedPhoneNumber.toString();
I had the same problem, but i did it like this:
String newWord = oldWord.replace(oldChar,newChar);
use that statement
string = string.replace(string.charAt(i));
Why? String is an immutable object. Look at this thread to get a complete explanation. This a fundemental part of Java, so make sure you learn it well.
string = string.replace(string.charAt(i), '<The char to replace>') will work.
Refer to this article you might understand better:
https://alvinalexander.com/blog/post/java/faq-why-isnt-replace-replaceall-replacefirst-not-working/
Since string is immutable, you have to reassign the a string with the string.replace() string.
I created a JAVA code, and I don't have any errors, but when I run the code, the output does this:
Enter a word: Thank you for entering a word! And it does not let me enter anything, when I intend for the code to let me enter a word, then it checks if it is a word, and gives the answer if it is a word, or none if it isn't. (It is my first time asking on this site) Here's the code:
package files;
import java.util.Scanner;
public class Testprinter {
static boolean myBoolean = false;
static Scanner userInput = new Scanner(System.in);
public static void main(String[] args){
String usersInput;
while(myBoolean != true)
{
System.out.print("Enter a word: ");
usersInput = userInput.toString();
myBoolean = checkInput(usersInput);
}
checkifComplete();
}
public static boolean checkInput(String usersInput){
if(usersInput == (String)usersInput)
{
return true;
} else { return false; }
}
public static void checkifComplete(){
if(myBoolean = true){
System.out.print("Thank you for entering a word!");
}
}
}
This line is wrong:
if (usersInput == (String)usersInput)
It should be:
if (usersInput.equals(usersInput))
In Java, strings (and in general: all objects, that is all types that are non-primitive) must me compared using the equals() method, which tests for equality. The == operator is fine for testing equality between primitive types, but for objects it tests for identity - a different concept, and 99% of the time, not what you want.
And besides, you're comparing a string with itself! it'll always return true, I'm quite sure that's not what you want to do… notice that the parameter must have a different name, currently it's called just like the attribute. Perhaps this is what you meant?
public static boolean checkInput(String input) {
return usersInput.equals(input);
}
You forgot scanner.nextLine(); thats reason its not asking you enter anything.
Instead of usersInput = userInput.toString();
Use:
String usersInputStr = scanner.nextLine();
Follow this link - for how to use scanner: How can I read input from the console using the Scanner class in Java?
Your issue is using userinput.toString(), when you should be using usersInput = userInput.next();. You are currently retrieving the string representation of the scanner, not getting a word.
Corrected main:
public static void main(String[] args){
String usersInput;
while(myBoolean != true)
{
System.out.print("Enter a word: ");
usersInput = userInput.next();
myBoolean = checkInput(usersInput);
}
checkifComplete();
}
I am very new to programming and am quite young.
I have no friends/family who can help me so I am seeking help on the internet. There is problem with my code as it isn't working as I intend it.
Instead of printing out what the variable "TheChoice", it just ends.
This isn't all the code and I have consised it so that it will be easier to read and maybe more people will be able to quickly answer.
However, this is definately the part of my code which I have messed up).
public String Choices(String value1, String value2)
{
Scanner x = new Scanner(System.in);
if(x.next() == value1){return value1;}
if(x.next() == value2){return value2;}
}
// Separate class...
ChoiceClass Object1 = new ChoiceClass();
String TheChoice = Object1.Choices("Ham", "Cheese");
System.out.println(TheChoice);
There's a number of issues with your code.
Firstly, you compare Strings with == instead of equals (there's
a ton of literature about String comparison and Object equality
in Java, I suggest you take a look here and here.
Secondly, you don't always return a value in your choices method. Your method must return a String (even in its default value, as null), but you're not considering user inputs other than given arguments.
Also your Scanner next wouldn't work as you're calling it twice,
when you only want to call it once.
Finally, you should take a look at Java naming conventions: method
names are camelBack. See here for code conventions.
Here's a snippet that'll probably help you out:
public static String choices(String value1, String value2) {
Scanner x = new Scanner(System.in);
System.out.println("Type your choice and ENTER...");
String input = x.nextLine();
if (input.equals(value1)) {
return value1;
}
else if (input.equals(value2)) {
return value2;
}
// handling other user inputs
else {
return "nothing";
}
}
public static void main(String[] args) {
// usage of method. Note that you could also refactor with varargs, as in:
// String... values in method signature.
// This way you could iterate over values and check an arbitrary number of values,
// instead of only 2.
System.out.println(choices("foo", "bar"));
}
Output:
Type your choice and ENTER...
... then either "foo" or "bar" or "nothing" according to input.
You can use something similar to this:-
public String Choices(String value1, String value2)
{
Scanner x = new Scanner(System.in);
String option=x.next();
if(option!=null)
{
if(option.equals(value1)) return value1;
else if (option.equals(value2)) return value2;
}
else return option;
}
If you want to compare Strings just use equals no need to use Scanner here.