Am I going to learn input during the Java course?

I need to complete a test task to be eligible for a seven-month Java course I want to take. It’s a calculator that can work with both Arab and Roman numbers. I hoped Codecademy would help me – don’t get me wrong, it’s great – but I wasn’t taught any input methods so far, the likes of system.in.read (or should I say input operators? I’m not adept at Java terminology). Without knowing any input, I won’t be able to complete the task!

Am I going to learn input during the Java course? I’m at Loops at the moment

I think the Intermediate Java course has some stuff on using Scanner for input.

What concerned me is the fact that there’s a project called “A basic calculator” in the pro-version of the Learn Java course. Does it mean that input methods are taught in the course if you have a pro subscription?

Generally, projects don’t teach anything new in them, and only use what’s been taught in previous lessons

But how can you make a calculator without input?

The calculator in that project is invoked by calling the class methods, for example:

 System.out.println(Calculator.add(5, 7));

Rather than directly taking input from the user :slight_smile:

As @janbazant1107978602 said above, I believe input is the first thing taught in the “Intermediate Java” course.

Ok, so I completed the first section of Learn Intermediate Java on Input/Output. It wasn’t much, and I still don’t know how to make the calculator I need. Now, I moved on to Serialization, and I’m no longer sure whether the course has what I need at all. Any advice?

You could make a calculator that deals with user input by using the Scanner stuff [and some other methods].

You might start by having something like this somewhere in the main method:

     Scanner scanner = new Scanner(System.in);
     double firstNum = scanner.nextDouble();
     char operation = scanner.next().charAt(0);
     double secondNum = scanner.nextDouble();

Could you give a subtle hint on how I could approach the issue of Arab numbers? .nextDouble would work fine if there is a double. If there’s something like IV, it won’t see it. Just don’t tell me the whole thing, just give me a nudge in the right direction, if possible

Also, does input only work from the command line? 'cause I can’t input anything in Codecademy’s workspace IDE, it just returns an error (even if I copypaste code with Scanner from one of the lessons). There are some exercises in Codecademy’s courses with a command line interface (example). Can I have a workspace with a command line?

Scanner scanner = new Scanner(System.in);
String string1 = scanner.next();

could be used to set up a scanner object and read the first line as a string. (You might then check whether that string starts with a roman numeral, or contains something useful.)

The Codecademy workspaces are not set up to get user input (except in a few lessons). So it won’t work there.

1 Like

Thank you! My calculator is working great (you can check it here if you want to)! But there’s an issue I would like to resolve. If you type in, for example, 5+3 as opposed to 5 + 3, it doesn’t work. Can my calculator be smart enough to delimit without explicit delimiters (like spaces)?

I’ve almost finished the Basic Java course and the Scanner method didn’t appear.

It’s briefly touched on in Learn Intermediate Java, but generally Codecademy is not going to teach much about it. The good news is it’s pretty straightforward

First, you create a Scanner object (I named it “scanner”, but it could be anything)

/* make sure you have import java.util.*; or import java.util.Scanner; 
 written at the top, before your class begins */
Scanner scanner = new Scanner(System.in);

Then you call a method on that Scanner object. next() means “scan the next element and return it as a String” (that is, “the next token”, as scanned elements are called), nextInt() means “scan the next element and return it as an int”, nextDouble means “scan the next element and return it as a double”. As soon as your scanner reaches a delimiter, it considers it the end of the token. By default, delimiters are one or more spaces. For example

// suppose a user types in "love 2 code" 
String token1 = scanner.next();
int token2 = scanner.nextInt();
System.out.println(token1);
System.out.println(token2);
// "love" and "2" get printed

or, alternatively,

String token1 = scanner.next();
String token2 = scanner.next();
System.out.println(token1);
System.out.println(token2)
// again, "love" and "2" get printed, but "2" is now a String

or, alternatively,

int token1 = scanner.nextInt();
System.out.println(token1);
/* the program returns the InputMismatchException because the first element 
in "love 2 code" is not an int */

You can also call the nextLine() method. It returns the whole line as a String

String token1 = scanner.nextLine();
System.out.println(token1);
// "love 2 code" gets printed

You can also change what your scanner considers a delimiter with the help of the useDelimiter() method. For example

// suppose the input is now "love 2,code"
scanner.useDelimiter("\\s+|,");
/* symbol | means "or", \\s means a space, + means "any number of" (spaces, 
 in this case), , means, well, a comma. So overall, the expression means "any 
 number of spaces or a comma" */
String token1 = scanner.next();
String token2 = scanner.next();
System.out.println(token1);
System.out.println(token2);
/* "love" and "2" get printed: the first token is separated by our scanner with 
 a space, the second token is separated with a comma */

Remember, when we created out Scanner object we wrote System.in as a parameter? It means we told our scanner to scan what a user types in during execution of our program. But scanner can scan more than that, for instance txt files. We need to create a List object and pass it as a parameter. For example

/* make sure you have import java.io.*; or import java.io.File written at the 
 top, before your class begins. now, suppose we have a file named input.txt on 
 our computer  which only contains this text, "1,2,3" */
File input = new File("input.txt");
Scanner scanner = new Scanner(input);
scanner.useDelimiter(",");
int token1 = scanner.nextInt();
int token2 = scanner.nextInt();
int token3 = scanner.nextInt();
System.out.println(token1);
System.out.println(token2);
System.out.println(token3);
// "1", "2", "3" get printed

You can even scan Strings, in case you may need it!

String love = "love,2,code";
Scanner scanner = new Scanner(love);
/* notice how we passed a String as a parameter instead of System.in or 
a File object */
scanner.useDelimiter(",");
String token1 = scanner.next();
String token2 = scanner.next();
String token3 = scanner.next();
System.out.println(token1);
System.out.println(token2);
System.out.println(token3);
// "love", "2", "code" get printed

I hope that cleared things out a little bit!

2 Likes

No its in the intermedite java course that calculator is hard coded