JUniting simple user input? [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to make a JUnit testclass which tests a very simple method.
public static String whatsYourName() {
System.out.println("Input your name:");
yourName = in.next();
return yourName;
}.
My Question was regarded to how you could test a simple methode as above stated.
I think the below solution is perfect!

I would first change the method to take the Scanner as an argument (dependency injection principles):
public static String whatsYourName(Scanner in) {
System.out.println("Input your name:");
yourName = in.next();
return yourName;
}
Then it becomes easy to test:
#Test public void testName() {
Scanner in = new Scanner("my name");
assertEquals("my name", whatsYourName(in));
}
Now that only tests that your method extract the right information from the Scanner, it does not test that a user can actually input something - but that would be part of your integration tests, not unit tests.
Also note that your method is essentially equivalent to return in.next(); so you are actually testing the next method of Scanner, which is unnecessary. But if the method beocmes more complex the principle will apply.
A very interesting piece to read: Google guide on Writing Testable Code.

Related

what is the difference between Number ob(10,20) and Number ob=new Number(10,20) in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
A class 10th standard student asked me what is the difference between Number ob(10,20) and Number ob=new Number(10,20) in java?
in the first case Number ob(10,20) - Is it syntax error?
Both are incorrect:
Number ob(10,20), syntax error
Number ob = new Number(10,20) syntax is correct but as java.lang.Number is abstract class so cannot be instantiated.
Source:
https://docs.oracle.com/javase/8/docs/api/java/lang/Number.html
https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
Actually both are incorrect.
See below code snippet:
package Rough;
public class TestNumbers {
public static void main(String[] args) {
Number ob(10,20); // compilation error
Number ob = new ob(10,20); // compilation error
Number ob_2 = 5; // no error
Number ob_3=new Number(10,20); // compilation error
}
}
This shows that to initialize a Number class, you need to instantiate with actual value.
I advise to online compiler handy before asking https://www.jdoodle.com/online-java-compiler, all I am saying is it will enhance learning.
Welcome to SO.

Would it make any difference giving arguments using scanner class instead of command line arguments? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
package Part1;
import java.util.Scanner;
public class Class1 {
public static void main(String[] args) {
String a = args[1]; // While executing, arguments given as - java Class1 1 2 3 4
System.out.println(a);
Scanner Scan = new Scanner(System.in);// User input is: 1
int inputnumber= Scan.nextInt();
System.out.println(inputnumber);
// Is Scanner class an alternative to command line arguments?
}
}
Command line arguments are configuration parameters of the process. They may change the processing flow and how data is processed.
Scanner reads data from the input stream. This is an actual data to be processed, not configuration.
In the context of your question (i.e., comparing to main method's arguments), the Scanner class works better for interactive sessions where the user is available for supplying the input interactively. But it also works with streams, files, and readers, etc.
This is more suitable when a user interacts with the program by starting it manually.
The main method's arguments, however, can be sent to the program even when the program is started programmatically or by schedulers, etc.
Although software can be programmed to send interactive input to other programs, the main program's arguments work better in non-interactive sessions.

What is the easyest way to transform the In.ReadInts function from Java to C#? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have the following code:
public class Search
{
public static void main(String[] args)
{
int[] whiteList = In.readInts(args[0]);
while(!StdIn.Empty())
{
int key = StdIn.readIn();
...
}
}
%java Search largeW.txt < largeT.txt
How to transform it to C# ?
Here you are:
Console.WriteLine("Input your number: "); // input 4, press enter
var theVar = Convert.ToInt32(Console.ReadLine()); // theVar is 4
Hope this help.
Java and C# aren't too different. You just need to know how to read STDIN, right? For a console application, use Console.ReadLine() or one of the other methods provided by the Console class.
Also keep in mind when converting:
1. With method names, you capitalize the first letter of EVERY word, i.e. MyMethod(). (important because Main() needs to be capitalzed)
2. All classes are inside a namespace block.
3. All of your type conversion tools are under Convert
4. File is a static class. You don't create instances of it like in Java. I recommend looking at File.ReadAllLines(string name).
Other than that the syntax is very similar and it should be fairly easy to convert between the two languages.

How to compare a String to two values in a single if [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
That is the method I've tried. If they type Yes with caps and yes without it will still count and it does not work.
Scanner in = new Scanner(System.in);
String In = in.next();
if (In.equals("Yes") & (In.equals("yes")) {
// Do this
}
else {
// Do this
}
Yes, you can do what you're talking about. This is the syntax:
if (In.equalsIgnoreCase("yes")) {
//Do something
}
It is also possible to do this:
if (In.equals("YES") || In.equals("yes")) {
//Do something
}
However, there's no reason to with the String method's case-insensitive comparison, see above. You really don't want to use your current syntax for 3 reasons: (1) AND operator is &&. Your operator is bitwise AND but this is not a bitwise operation. (2) It's not possible to have a string be equal to more than one thing, so use OR not AND if you're going that way. (3) Watch your parentheses, they're off.

Never executes the code. Java [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
My issue is when I run the code in eclipse it just never seems to do anything at the bottom. The other day I made some almost identical programs and they worked fine. The only thing I can assume is going on is it never gets the input from scanner so it never prints the line so the program just keeps on running.
import java.util.Scanner;
public class testrun {
public static void main(String args[]){
String name;
Scanner get = new Scanner(System.in);
name = get.nextLine();
System.out.println(name);
}
}
You may need to type something and hit that ENTER key. The Scanner's nextLine() is waiting for input.
:)
It happens to us all.
The program works good and asks for input.
Keep some sop to print some helping statements and make it user friendly.
import java.util.Scanner;
public class Testrun {
public static void main(String args[]){
String name;
System.out.println("Please Enter : ");
Scanner get = new Scanner(System.in);
name = get.nextLine();
System.out.println(name);
}
}

Categories