Question

Java. I am having a hard time figuring out how to take user input and then...

Java. I am having a hard time figuring out how to take user input and then write a method that returns the answer. All the examples I find have numbers hard coded. Can you show me how this is done. For example, maybe add two numbers. Thank you

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

I am using Scanner class to get the input. We use following methods to get user input:
1) public String nextLine(): For getting input String
2) public int nextInt(): For integer input
3) public float nextFloat(): For float input

Now as per your demand, I am writing the code for the addition of two numbers.

First we need to import the package java.util to access the Scanner class's functionality.

Complete code is as follows:

import java.util.*;

class Main

{

//function that calculate the sum of two integers that are given by the user

public static int sum()

{

//create object of Scanner class where in is a identifier

Scanner in = new Scanner(System.in);

//take first user input

System.out.print("Input first integer: ");

int first = in.nextInt();

//take second user input

System.out.print("Input second integer: ");

int second = in.nextInt();

//perform addition

int result = first + second;

//returns the answer

return result;

}

//main function

public static void main(String[] args)

{

//call the function sum

System.out.println("The sum is: "+sum());

}

}

Output:

Please give thumbsup, if you like it. Thanks.

Add a comment
Know the answer?
Add Answer to:
Java. I am having a hard time figuring out how to take user input and then...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Im having a hard time figuring what Ksp is. I don't know If I am inputing...

    Im having a hard time figuring what Ksp is. I don't know If I am inputing the numbers wrong or calculating it incorrectly. Please help and thank you!! A Student mixed .59 of Solid AB with 25.ML of dis Hiled water and aueused the mixtare to achieve etuiu: brium qecordag the foutouing eguariua The Student ther birkerad the me. The Student took a lo.o ML Samle af the Soiuten and Perhoimed a itration. The concentrathon of the anien was hund...

  • I am trying to write a java method that takes two string parameters from a user...

    I am trying to write a java method that takes two string parameters from a user and then returns the first input after removing the letters that appear in the second input. For example: if input1 = fortune and input2 = tune then the method returns "for" seems simple but I am not understanding exactly how to tackle this thanks!!

  • I am having trouble figuring out what should go in the place of "number" to make...

    I am having trouble figuring out what should go in the place of "number" to make the loop stop as soon as they enter the value they put in for the "count" input. I am also having trouble nesting a do while loop into the original while loop (if that is even what I am supposed to do to get the program to keep going if the user wants to enter more numbers???) I have inserted the question below, as...

  • Can someone please help me with this problem? I am having trouble with figuring it out....

    Can someone please help me with this problem? I am having trouble with figuring it out. Thank you in advanced! The 10x objective has a numerical aperture of 0.25. What would be the smallest distance (in μm) between two objects that can be resolved when using that objective with light at 400 nm?

  • So I am having probelms with figuring out how to do this program the part in...

    So I am having probelms with figuring out how to do this program the part in bold is the part I dont know how to do. This is in JAVA. What I have so far is shown below the question, this is right upto what I have to do next. Choose Your Operation Write a program that uses a WidgetViewer object to do the following: Generate two random integers between 1 and 9 (inclusive). Name one of them x, the...

  • java: i am having a hard time understanding this concept and if so please follow the...

    java: i am having a hard time understanding this concept and if so please follow the below guidelines and explain what you do for me thanks draw a binary search tree (BST) using these values : 4, 6, 11, 22, 53, 94, 21, 53 then select one of the leaves of the bst you chose and show how the below contains algorithm goes through its execution on the bst to find the node you have selected. the contains algorithm: private...

  • Hello, I am having trouble figuring out how to go about each of these questions for...

    Hello, I am having trouble figuring out how to go about each of these questions for a worksheet as I have not learned any of this in orgo lecture yet but, I was given this assignment in orgo lab. Could someone please be kind enough to answer and give me an explanation for each one as I have no clue how to go about this. thank you for your time. at are the starting materials needed to run the size...

  • I am having a hard time with these types of questions. Could someone show me how...

    I am having a hard time with these types of questions. Could someone show me how to break it down so I know how to do them. The distribution of sample means for monthly employee income at a particular company for samples of 70 employees is​ normal, with a mean of $5522, and a standard deviation of $224. You take a random sample of 70 employees from the company and find that their mean monthly income is $5,800. How many...

  • Hello I am having trouble figuring this out because of the the 2 SD? I am confused? Thank you! On a standardized test, t...

    Hello I am having trouble figuring this out because of the the 2 SD? I am confused? Thank you! On a standardized test, the mean history score for boys was 40(SD =15), whereas the mean score for girls was 70(SD=35), Calculate Cohen’s d to determine the effect size of this difference.

  • My class is learning solving exponential functions and I’m having a hard time figuring out how...

    My class is learning solving exponential functions and I’m having a hard time figuring out how to find x with this kind of function, can you help to solve and explain while you do it -6 42 4) 2162 36 x-2 3x -3 2x 3x +2

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT