Changing a hardcoded object [duplicate] - java

This question already has answers here:
How can I read input from the console using the Scanner class in Java?
(17 answers)
Closed 6 years ago.
I have tried googling this, and looked for a while, but have not found anywhere that explains how to do this well
SalariedEmployee salariedEmployee =
new SalariedEmployee("bob", "Smith", "222-22-2222", "800");
The variables are firstname, lastname, social security number, and weekly salary.
How would i change that to something where i can ask what each of the variables should be such as
SalariedEmployee salariedEmployee =
new SalariedEmployee(firstName, lastName, ssn, salary);
and then ask
what is the employee's first name?
what is the employee's last name?
What is the employee's ssn?
What is the employee's salary?
If anyone can explain this, or has seen this explained somewhere else your input would be greatly appreciated.

Read inputs from a user using a Scanner object or a Buffered Reader and save them locally. Initialize the object after you have all the inputs. Alternatively you can have set methods and assign them in any order you like.

Related

What is wrong here? I cannot run user input code [duplicate]

This question already has answers here:
Resource leak: 'in' is never closed
(14 answers)
Closed 8 months ago.
enter image description here
I just started learning java today. I am now stuck in this user input coding. I have tried many methods but I cannot run this user input code.
As the others already stated closing the scanner is best practice.
If you use Java 8 or higher you can do it the following way:
try(Scanner sc = new Scanner(System.in))
{
String name = sc.nextLine();
System.out.println(name);
}
Besides that your code already works! When you run the program it's waiting for input, so you need to write some text into the console-Window and press enter

How to put a certain string into *if* and check if it is a certain word [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
I am trying to scan a string and put it in an if like so:
(this is just an example part of the whole program)
Scanner scan=new Scanner();
System.out.print("Enter the word...");
String a=scan.nextLine();
if (a=="Hi")
System.out.println("Hello!")
Else System.exit(0);
So im checking if the user said "Hi" , but it doesnt work like this, I want a way to do it.
I have just started learning java so my questions might seem a little amateur but your answers will help a lot.
Use "Hi".equals(a), == compares identity, not equality.
Also, you should write specifically "Hi".equals(a) and not a.equals("Hi"), because it won't cause a NullPointerException, since a might be null.

How do I get input directly from keyboard? [duplicate]

This question already has answers here:
How to get the user input in Java?
(29 answers)
Closed 6 years ago.
I am an AP Computer Science student who is trying to write a console-based text editor. My teacher taught me how to read input using Scanner(System.in), but in a text-editor, you need to get input directly from the keyboard. How do I gain access to keystrokes without using the Scanner class, or a similar parallel? For example, BufferReader(new InputStreamReader(System.in))
EDIT: My point was how read without waiting for user to press enter (otherwise users couldn't add text in real time)
EDIT: Why are you saying my question is a duplicate of that question? I explicitly asked for a method of getting input without waiting fo ruser to press enter. The answers to that other question do not resolve that at all!
You can use this :
System.out.print("Please enter your String :\t");
String sentence = scanner.nextLine();

Java - Use Users input in code [duplicate]

This question already has answers here:
How to get the user input in Java?
(29 answers)
Closed 8 years ago.
I am trying to build a program that talks to the user and I want it to know as much as possible. For that I need an ability to use what the user types in my programs code and not just in variables in a way that it is fully implemanted in to the code
Or if there is now way, you know of just Tell me and I will accept it.
The Scanner class can be used for input. Example:
Scanner scanner = new Scanner(System.in);
String age = "";
System.out.println("Please enter your age:");
age = scanner.next();
This code will get user input via the console.

How to convert an amount of different currency into words in java? [duplicate]

This question already has answers here:
How to convert number to words in java
(31 answers)
Closed 9 years ago.
I have an amount with different currencies, I want to convert an amount into words.suppose if i have a number 1000000, i have to print it for "Rupees ten lakh only" for india, "Dollar one million only" for usa etc...
Any suggestions would be great.
Thanks in advance,
Msn
I'm going to take a stab in the dark and assume you're talking about String and int data types.
To append the value of the int type onto the String use the String's method valueOf, for example:
String formattednumber = "$" + String.valueOf(amount);
In the future please provide more information to exactly you want and what you have tried, also code would be helpful.
If you need to print numbers as words then this might be helpful.
same logic can be used to append currency name.
hope that helps.

Categories

Resources