Write A Statement That Reads User Input Integer Into Defined Variable

Jan 20, 2015

so i'm following a java tutorial from the book and it has a few challenge questions. and i'm stucked on one. i think i just don't understand what is it that its asking me. heres the question, Write a statement that reads a user's input integer into the defined variable, and a second statement that prints the integer. assuming scanner is given, and i checked my heading code is ok.
 
Scanner scnr = new Scanner(System.in);
int userNum = 0;
System.out.println("What is the product of 8 time 2");
userNum = scnr.nextInt();

[code]....

View Replies


ADVERTISEMENT

Write A Java Program That Reads A Positive / Non-zero Integer As Input

Aug 1, 2011

Write a Java program that reads a positive, non-zero integer as input and checks if the integer is deficient, perfect, or abundant.

A positive, non-zero integer, N, is said to be perfect if the sum of its positive proper divisors (i.e., the positive integers, other than N itself, that divide N exactly) is equal to the number itself. If this sum is less than N, the number is said to be deficient. If the sum is greater than N, the number is said to be abundant.For example, the number 6 is perfect, since 6 = 1 + 2 + 3, the number 8 is deficient, since 8 > 1 + 2 + 4, while the number 12 is abundant, since 12 < 1 + 2 + 3 + 4 + 6.

SAMPLE OUTPUT:

1.)Input N: 5

5 is deficient.

2.)Input N: 6

6 is perfect.

3.)Input N: 18

18 is abundant.

View Replies View Related

Write A Program That Reads From User Several Lines Of Input

Mar 24, 2014

a. Assume that we have a list of employees' names of a company and their ages. Write a program that reads from the user several lines of input. Each line includes an employee's name and his/her age (as an integer). The program should calculate and print the following:

- The average age of all employees (rounded to 2 decimal places).
- The oldest employee and his/her age.

Hints:

You could assume that the user will insert valid data and at least one employee.
You could assume that the oldest employee is only one person.
User could stop the program via entering the word "end" as an employee's name.

Sample Input and Output: In each line, insert an employee's name and his/her age To halt the program, insert "end" as an employee's name

Adam 30
Tom 41
Ted 45
Karl 30
end

The average age of all employees is 36.50.The oldest employee is Hisham whose age is 45

View Replies View Related

Difficulties With User Defined Variable In Multiple Methods

Apr 9, 2015

I'm trying to build a program that will output what will ultimately look like a simple mario level turned on its side. As part of my output I need the user to define what mario looks like. I do this using Scanner and save the input to String mario. When I try to use that variable in another method it gives me troubles.

import java.util.Scanner;
public class Mario2

public static void mario() {
//user defines mario
String mario = ">->O";
Scanner keys = new Scanner(System.in);
System.out.println("What does mario look like?");
mario = keys.next();
System.out.println("Mario now looks like: " + mario);
 
[code]....

View Replies View Related

Write Application That Reads Five Integers

Jun 12, 2014

Write an application that reads five integers, determines and prints the largest and smallest integers in the group. Use only the programming techniques you learned in this chapter. (which means: use only if statements and the equality and relational operators).If I want to solve this I will write nested if statements for every probability like the following.The probability: the first integer is the largest and the fifth integer is the smallest:

1>2>3>4>5
1>2>4>3>5
1>4>3>2>5
1>4>2>3>5
1>3>4>2>5
1>3>2>4>5

And so on for every probability.

View Replies View Related

Check User Input Is Integer

Dec 18, 2014

System.out.print("To begin, please enter 1 to choose stand tickets or 2 to choose terrace tickets for your party. ");
int standOrTerraceTickets = aScanner.nextInt();
while (standOrTerraceTickets != 1 && standOrTerraceTickets != 2)
{
System.out.print("Invalid input. Please enter 1 for stand tickets or 2 for terrace tickets. ");
standOrTerraceTickets = aScanner.nextInt();
}

Okay I thought I had this working properly so that when the user entered anything other than 1 or 2 they would keep getting an error invalid input, however, that only works when the user enters an integer. If the user enters anything other than a number, the program crashes. How does one prevent this from happening?

View Replies View Related

Write A Java Program That Reads 5 Integers

Oct 9, 2014

Project is due before midnight eastern time (3 more hours). I have been working on this all day, we have not learned arrays or anything and just started 'for' loops. I don't really know how to implement that system yet. However, he wants us to check all the possible handwritten math ways of doing this. I have tried but I cannot return my numbers beside the card.my program only prints the type and I still have more cases to add as well..

QUESTION: Write a Java program that reads 5 integers, each of which is in the range of 1 and 13 representing a simplified poker card with no suit. Your program should then print the ranking of the hand with card number(s) beside the ranking. The rankings to determine are Four of a Kind(4 of the 5 cards of the same rank), Full House(3 of the 5 cards of the same rank, and 2 cards of another rank), Straight(5 cards of sequential rank), Three of a Kind(only 3 cards of the same rank), Two Pair(2 cards of the same rank, 2 cards of another rank, and no card of either rank), One Pair(only 2 of 5 cards are of the same rank), and High Card(no two cards of the same rank). Some sample runs are:

input: 2, 3, 2, 2, 2; output: Four of a Kind(2)
input: 2, 3, 2, 3, 2; output: Full House(2, 3)
input: 2, 3, 4, 5, 1; output: Straight(5)
input: 1, 13, 11, 10, 12; output: Straight(1)

[code]....

View Replies View Related

If Statement - Checking / Comparing Two Strings From User Input

Dec 7, 2014

I am trying to do is get a user to input their name into the system. Once the user has inputted the name into the system, the system should check to see if the name entered matches "alice". If this is true then the system should print "welcome to the system alice". If it is not alice it should not do anything.

Here is the code I have so far... The fault/ error seems to be with the sInput string in the IF statement.

import java.io.*;
public class Hellohuman
{
public static void main (String[] args) {
try {
InputStreamReader isr = new InputStreamReader(System.in);

[Code] ....

View Replies View Related

Converting User Input Binary To Integer

Sep 7, 2014

I am new to java and programming in general. I figured out how to convert an integer input to binary however I am having issues doing the opposite of converting a user input binary number to a decimal.

I need to do this with basic math (or string depending on how I represent the binary) and no functions.

I know how to convert binary to integer on paper but I am having a hard time working it out in java.

View Replies View Related

Write User Input To Text File

Dec 8, 2014

I need to write a program where the user inputs name, age, email, and cell and write the user inputs to a text file. I need to have it so the file can be added to if more than one set of information is added. Currently, every time I click the button, the file is overwritten instead of added to. How do I correct this? Current code is as follows:

try
{
FileWriter writer = new FileWriter("ContactInformation.txt");
BufferedWriter bw = new BufferedWriter(writer);
nameTextField.write(bw);
bw.newLine();
ageTextField.write(bw);
bw.newLine();
emailTextField.write(bw);
bw.newLine();
cellTextField.write(bw);
bw.newLine();
bw.close();
nameTextField.setText("");
ageTextField.setText("");
emailTextField.setText("");
cellTextField.setText("");
nameTextField.requestFocus();
}

View Replies View Related

How To Get User Input And Create Variable

Sep 16, 2014

Okay, so I know how to get user input and create a variable from the input, create a basic addition sum etc...

So I have this code:

package calc;
import java.util.Scanner;
class calc{
private static final double add = 0;
private static final double subtract = 0;
public static void main(String args[]){

[Code] .....

I'm basically trying to make it so that when the user enters tnum, tnum2, tnum3 and tnum4, their answer turns to the variable which has to be either true or false to make the boolean work. I don't know really how to do this. I want to make it so that if they enter yes, then that makes the boolean true and the numbers will multiply and create the answer variable. If they enter no then the boolean is false and it moves onto the next if statement. How can I do this?

View Replies View Related

Read From User Input (integer) And Reduce It By Multiplying Its Non-zero Digits

Nov 15, 2014

Write a program that reads from the user an integer and reduce it by multiplying its non-zero digits. The result of the multiplication is a number which is to be reduced as the initial one. This process continues until an integer of one single digit is obtained. For example:

64734502 (6 * 4 * 7 * 3 * 4 * 5 * 2) 20160 (2 * 1 * 6) 12 (1 * 2) 2

Your program should display the number obtained in every iteration.

Sample run1
Enter an integer: 64734502
After iteration 1: 20160
After iteration 2: 12
After iteration 3: 2

Sample run2
Enter an integer: 97737999
After iteration 1: 6751269
After iteration 2: 22680
After iteration 3: 192
After iteration 4: 18
After iteration 5: 8

View Replies View Related

Prompt User To Input Integer And Then Output Both Digits Of Number And Their Sum

Jun 7, 2015

Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. 
 
Now I have a code for spacing out the integers, and I have a separate code for adding the digits. But I am not sure how to merge them together. Both of them work independently
 
Spacing code:
import java.util.*;
public class SumoftheIntegers
{
static Scanner console=new Scanner(System.in);
public static void main(String []args) {
int num1, test, rem;
int counter = 0;

[Code]...

Now the sum of the integers code:
 
import java.util.Scanner;
public class sum
{
    public static void main(String[] args)
    {
        // Create a Scanner
        Scanner input = new Scanner(System.in);
// Enter amount
        System.out.print("Enter an integer: ");
        int integer = input.nextInt();

[Code]...

View Replies View Related

User Input Random Numbers Then Program Sort Them In Different Ways - Missing Return Statement

Apr 7, 2014

I have been assigned to write a program that has a user input random numbers then the program is to sort them in different ways. I have the coding for the different sorts but, I have an error saying that I am missing a return statement in the "Bubble" method. I am trying to return "arr[i]" in the "for loop" which gives me this error, and when I try to take the "return arr[i]" outside of the "for()" loop the error reads the it cannot locate variable "i".

import java.awt.* ;
import java.awt.event.*;
import javax.swing.*;
public class SwingSorts extends JFrame implements ActionListener
{
JRadioButton bubble;
JRadioButton selection;

[Code] .....

View Replies View Related

Write A Code That Allows A User To Input Number Of Stones In A Basket

Nov 20, 2014

I am trying to write a code that allows a user to input the number of stones in a basket, then put the stones in my pocket, after the stones are in my pocket, the stones must be taken out, one by one, and thrown in a pond. I get stuck at this part because my for statement has to subtract the number of stones in my pocket (one at a time) and add a stone to the pond (one by one) at the same time.I can get the pond stones to increase until the max amount of stones with the following:

for (int i = 0; i < stones; i++) {

System.out.println("Pocket:" ? " " + "Pond:" + i);

how to reduce one and add to other. Is it possible to do this in one for statement? I tried to put stones-- in place of the question mark, but that is not working.

View Replies View Related

Allowing User To Input A Number And Assign This To Int Variable

Jul 4, 2014

I need the user to be able to input a number, and for the program to assign this value to an 'int' variable. I know how to do this with a 'string' variable:

Java Code:

String options = JOptionPane.showInputDialog(null, "In your decision, how many options do you have?
" +" (NOTE: The maximum number of options = 5, and you must enter your answer as a numeral.)"); mh_sh_highlight_all('java');

But I need to know how to do this with an 'int' variable.

View Replies View Related

Storing Input From User As String Variable And Then Converting It To Double?

Jul 8, 2014

I am trying to make a program that calculates the change due in dollars and cents. The user inputs both the amount due and the amount tendered. My program only works with whole numbers?

View Replies View Related

Variable Fields Are Not Holding Information Through Constructor From User Input?

Oct 19, 2014

Variable Fields are not holding information through constructor from user input so here's what I did instead.

View Replies View Related

JSP :: Variable Defined In One Scriptlet Not Visible In Another

May 25, 2011

I'm having an issue when I try to define a variable in a JSP scriptlet, and then in a separate scriptlet on the same page attempt to use the variable. It looks like it goes out of scope.

I also cannot reference a variable from a servlet in a JSP expression tag. So I've had to write the entire page basically in one scriptlet.

I'm using Tomcat 6.0.

View Replies View Related

What Happens To A Static Variable That Is Defined Within A Method Of A Class

Feb 15, 2014

What happens to a static variable that is defined within a method of a class ?

View Replies View Related

Swing/AWT/SWT :: Non-final Variable Inside Inner Class Defined In Different Method

Aug 9, 2014

this code won't compile because selected row must be declared as final because of it being defined outside the window listener. Is their anyway around this? If I make it final the first time that the variable is called it keeps it starting value until the GUI is closed.

butEdit.addActionListener (new ActionListener () {
@Override
public void actionPerformed (java.awt.event.ActionEvent evt) {
int selectedRow = table.getSelectedRow ();
final String [] values = custTableModel.getRowValues (selectedRow);

[code]....

View Replies View Related

Cannot Refer To Non-final Variable Inside Inner Class Defined In Different Method

Apr 3, 2014

Created a java.sql.connection object. Refering those obj inside public void run() { } If i declare as final inside a method, i can't refer those outside method due to scope. Cannot refer to a non-final variable dbConnObj inside an inner class defined in a different method...

View Replies View Related

User To Input Integer And Then Outputs Both Individual Digits Of Number And Sum Of Digits

Feb 2, 2015

Goal is to: Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits.

First I don't know where I made mistakes here, and the only error it finds right now is that str2 was not initialized and I cannot figure out where/when to initialize it.

import javax.swing.JOptionPane;
public class DigitsAndSum {
public static void main (String[] args) {
String str1;
String str2;
int int1 = 0;
int int2 = 0;

[Code] ....

View Replies View Related

Parallel Arrays - User Input Integer 1-12 / Output Name Of Month And Number Of Days In That Month

May 11, 2015

I've been working on a question using parallel arrays where the user inputs an integer 1-12 and the output will be the name of the month and the number of days in that month. This is what I have so far

import java.util.*;
public class DaysMonth
{
public static void main (String args[]) {
Scanner keyIn = new Scanner(System.in);
int[] days = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

[Code] ....

After I enter the user int it just ends.

View Replies View Related

Calling A User Defined Exception?

Dec 3, 2014

I know that I am not 100% comprehending try/catch blocks, but after scouring message boards, forums, and Oracle, I still can't pick out where I am going wrong.

I have a ValidateInput class where I am trying to check that a String only has letters. If not, then throw an exception message via JOptionPane. I created my own NonLetterException class. When I call the method containing the try/catch Eclipse gives me an Unhandled Exception Type error.

in main()
ValidateInput validate = new ValidateInput();
String name = "error";
for(int x = 0; x < 1;){
name = JOptionPane.showInputDialog("Welcome. What is your name?");
boolean isItName = validate.stringInput(name); //error appears at validate.stringInput
if(isItName)

[code]....

Aren't I handling it in the try/catch? What did I miss?

Also, I have have tried the NonLetterException class as nested in ValidatedInput, but also not nested. To me nested makes more sense. I have never nested classes before, but it makes sense to me because I am not using this exception in other parts of my program.

View Replies View Related

Differentiate System And User Defined Classes

Mar 16, 2014

How Can I differentiate the System classes and user classes in my program written below by another program?

/**
* Employee Class containing all non-primitive data types.
*
*/

class Employee{
private String name;
private String phone;
private Integer age;
private Float salary;

[Code] ....

Any Technique that will decide which one of the fields (Integer, Address, DOB, String, Float) are user defined type. (An outsider class should used that will identify the Java classes and the user-defined classes.)

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved