DiceSum - Prompt User For A Desired Sum

Nov 16, 2014

Write a method named diceSum that prompts the user for a desired sum, then repeatedly rolls two six-sided dice until their sum is the desired sum. Here is the expected dialogue with the user:

Desired dice sum: 9
4 and 3 = 7
3 and 5 = 8
5 and 6 = 11
5 and 6 = 11
1 and 5 = 6
6 and 3 = 9

This is my code:

import java.util.Scanner;
import java.util.Random;
public class mod {
public static void main(String[] args) {
diceSum();

[Code] .....

it works for numbers 2 - 4, but any number from 5 - 12 just prints one output, which is not the correct desired sum.

View Replies


ADVERTISEMENT

Prompt User For Integer To Make Sure Length Entered By User Is A Power Of 2

Oct 8, 2014

Basically I need to make a program prompts the user for an integer, check to make sure the length entered by the user is a power of 2 and at least 2. Then I need to convert from base e to base 2 then I need to print the tick marks of a ruler based of the value of the length.

I got the whole converting thing working and the check for a power of 2, that wasn't an issue because it didn't require any recursion. I just don't know how to print out the proper tick mark values.

The way it is supposed to work is that it needs to look like this. Say the user enters 8;

012131210
012345678

Or if the user enters 16;

01213121412131210
01234567890123456

The bottom row is pretty much just the index value, that I print fine.

The top row though is supposed to be the length of the ticks on a ruler, with the middle most value being the value of the conversion from base e to base 2 from above. I can get that printed and what I get just looks like this.

For 8;

000030000
012345678

For 16;

00000000400000000
01234567890123456

As you can see I can get the middle value and the index values below but I don't know how to use recursion to get the right numbers.

Here's my current code.

import java.util.*;
public class TickMarks {
public static void main (String args[]){
Scanner input = new Scanner(System.in);
boolean looping = true;
while(looping == true){
System.out.print("Please enter a length: ");

[Code]...

Now the methods isPowerOfTwo() and printLength() both work fine. However, I'm not sure how to get the left and right side working.

My thoughts were to split the length in half and get a left and right side. I gave both of them arrays so I can keep track of the values. Now as you've seen above I get all zeros and I know it's because that's the default value in an array, I just don't know how to get the proper values.

View Replies View Related

Random Number With User Prompt?

Sep 8, 2014

I am wanting this program to prompt the user to enter a maximum value and a minimum value and the program should in theory generate a random number for the output. Here is my logic:

int maxRange1;
int minRange1;
static int range;
static Random gen;

Then in my mail argument:

gen = new Random();
Scanner input = new Scanner(System.in);
System.out.print("Enter the maximum number: ");
int maxRange1 = input.nextInt();

[Code] ....

I am able to return both the user prompts and get the inputted answer to appear but I am not getting a response for the prompt "The random number is: ".

View Replies View Related

Prompt User To Enter A String

Feb 15, 2015

I am suppose to design a problem the prompts the user to enter a String. Based on the input from the user, have the program prompt with a full description of the playing card entered. Below is a table of the notation and meaning:

Notation Meaning
A Ace
2...10 Card Values
J Jack
Q Queen
K King
D Diamonds
H Hearts
S Spades
C Clubs

Use the .charAt(...) to extract each character from the user's input and build a switch statement to determine the full description.

import java.util.Scanner;
public class PlayingCardIdentifier {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a card notation: ");
String s = input.nextLine();

[code]...

View Replies View Related

Insert While-loop To Prompt User For Yes Or No

Apr 15, 2015

public static void getInput(int [] student, int []test1,int []test2,int []test3,
int [] test4, int [] test5) {
int stuId = 0;
int t1 = 0;
int t2 = 0;
int t3 = 0;
int t4 = 0;
int t5 = 0;
int h = 0;
char answer = '@';

[Code] ....

View Replies View Related

Prompt For User Input At The End Of First Loop

Mar 3, 2014

I am having a problem with code that I've written, the problem occurs when I try to prompt for user input at the end of the first loop. What happens is the println fires but the prompt never does and it exits the loop.

Java Code:

import java.util.Scanner;
public class Lab3test
{
public static void main( String[] arguments )
{
//Variable declaration

[Code] ....

View Replies View Related

Code Up With A Loop That Prompt User To Say If Program Should Run Again

Sep 10, 2014

I am still relatively new to java and am working on a lab program. I have already met the requirements and am trying to spice my code up with a loop that prompts the user to say if the program should run again. This is what I have so far:

package lab2;
import java.util.Scanner;
public class OrderedPairTest{
public static void main(String[] args){
boolean retry = true;
while(retry == true){

[code]....

However, it is giving me an error after the user inputs "y" in for the answer to run again. What did I do wrong?

Error code is: Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Unknown Source) at lab2.OrderedPairTest.main(OrderedPairTest.java:11)

View Replies View Related

Prompt User To Enter Integer From 1 To 15 And Displays A Pyramid

Jun 1, 2014

The exercise sounds like this : Write a program that prompts the user to enter an integer from 1 to 15 and displays a pyramid , as shown in the following sample run (my code displays correctly the first 9 lines):

1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7

The problem is when i input a number greater then 9 as it requires 2 spaces . I m pritty sure i solved it incorrectly or at lost not optimal as i m using a string that decreases on each line to create the pyramid effect.

import java.util.*;
public class C5_17 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of lines: ");

[Code] ....

View Replies View Related

Prompt A User To Enter Three Tasks And Their Priorities In A Driver

Oct 31, 2014

My full instructions are to Design a Java interface called Priority that includes two methods and two constants. The methods are setPriority and getPriority. The constants are MIN_PRIORITY with value 1 and MAX_PRIORITY with value 6. The interface should define a way to establish numeric priority among a set of objects. Design and implement a class called Task that represents a task (such as on a to-do list) that implements the Priority interface. Assume that Task has two attributes (instance data): name and priority. Define the Task constructor to accept and initialize the name of the task. Each newly created Task should have priority equal to MIN_PRIORITY (the constructor should initialize priority to MIN_PRIORITY). Priorities can be changed using setPriority method. Create a driver class TaskDriver that should do the following: prompt the user to enter three tasks and their priorities, and then print the tasks in the order of decreasing priorities. If the user enters an invalid priority that is less than MIN_PRIORITY or greater than MAX_PRIORITY, then the user should be asked to enter priority again until a valid priority value is entered.

So far I have:

public interface Priority
{
public void setPriority(int level);
public int getPriority();

[Code].....

View Replies View Related

Prompt User To Enter Value Of Their Savings For N Number Of Weeks

Oct 19, 2014

So I have this program where i have to prompt user to enter a the value of their savings for n number of weeks (quarters dimes nickels pennies) and the program outputs the total of savings an average of saving per week and an estimation of saving per year.

I managed to do the first step but the problem is that when they enter the value of their savings i don't know how to save the value of each(quarters dimes nickels and pennies) to make the calculations? Here is what i have so far

import java.util.Scanner;
 public class yearlySavings
{
 public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int numberOfWeeks = 0; // initialize value of numberOfWeeks
 
[Code] .....

View Replies View Related

Decoder Ring - Prompt User For A Message To Decode

Nov 7, 2014

So it is the question: This program implements a simple "decoder ring". It will prompt the user for a message to decode and decode the message based on the following substitution rules:

ABCDEFGHIJKLMNOPQRSTUVWXYZ will map to
ZEBRASCDFGHIJKLMNOPQTUVWXY

abcdefghijklmnopqrstuvwxyz will also map to
ZEBRASCDFGHIJKLMNOPQTUVWXY

any other characters (like space, punctuation, will map to themselves)

so if it sees an 'A' (or an 'a'), it will map to 'Z', etc... All i could think of is this but can't figure out the rest.

import java.util.Scanner;
public class DecoderRing
{
public static void main(String[] args)
{
String1[] = 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z';
String2[] = 'Z','E','B','R','A','S','C','D','F','G','H','I','J','K','L','M','N','O','P','Q','T','U','V','W','X','Y';
}
}

Not sure, what things are necessary such as switch statements or if and else or for loops and such...

View Replies View Related

Search Matrix For The Element With Highest Value - Prompt User With Location

Feb 11, 2015

I have to create a program that asks a user to enter an amount for rows and an amount for columns. After they input both amounts, they must fill in the matrix with values (values of double data type). After they enter the values, the program is suppose to search the matrix for the element with the highest value. Once it is found, the user will be prompted with the location of the highest value within the array. I got it to work as to where the user inputs the values and he/she will receive a location of an element. However what they receive is incorrect.

For example

"Enter number of rows: 3
Enter number of columns: 4
Enter values: 23.5 6 13 2
1 5 6 3
7 5 9 2"

Once they enter the last value, they will be prompted with

"The highest element is located in 34".

As you can tell, the highest element is located in 01. I think when the user is prompted with the values that they enter for the rows and columns. How can I modify my code as to where it shows the actual location of the highest value?

View Replies View Related

Test Program That Prompt User To Enter N And Displays N-by-b Matrix

Nov 25, 2014

I did a problem from my book which is "Write a method that displays an n-by-n matrix. Each element is 0 or 1, which is generate randomly. Write a test program that prompts the user to enter n and displays the n-by-b matrix".

So if a user would enter 3, and output could be 0 1 1
0 1 0
1 0 1

So here's my question... I was able to get the program to work in the way my book describes (by just writing the code in the class with the main), but for practice, I want to learn how to do this OOP-style...

I'm not sure how to do this, though, since the method is a void method, so I can't seem to call it within a toString method.

import java.util.Scanner;
public class MatrixTest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter an int");
int number = input.nextInt();
MatrixClass mc = new MatrixClass(number);

[Code] .....

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

Prompt User For Input Number And Check If It Is Greater Than Zero - Java Multiplication

Apr 13, 2014

Write a program that prompts the user for an input number and checks to see if that input number is greater than zero. If the input number is greater than 0, the program does a multiplication of all the numbers up to the input number starting with the input number. For example if the user inputs the number 9, then the program displays the following sum:

9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 = 362880

That's the question I'm getting and so far all I've got is

import java.util.Scanner;
public class Lab4Q3
{
public static void main (String[] args)
{
int keyboard;

[Code] .....

View Replies View Related

Add Integers To ArrayList And Prompt User To Delete Specific Numbers Not Required

Jan 21, 2015

This program simply adds integers to an ArrayList and then prompt the user if they would like to delete specific numbers that they don't want.

The output that I get when I want to delete numbers like 2 and 4 from 1 2 3 4 5 is 1 2 3 4 5 instead of 1 3 5.

Java Code:

import java.util.ArrayList;
import java.util.Scanner;
public class AL
{
// A regular array like int arr[] has to have its size declare before run-time and it's not dynamic which mean it can not grow or expand on its own.
static Scanner input = new Scanner(System.in);
// You have to use reference types(Integer, Double, Float); not primitive types(int).

[Code] .....

View Replies View Related

Get Desired Output With BigDecimal

Jan 9, 2014

Jdeveloper version 11.1.1.7.0
 
I have a variable which if of BigDecimal. that variable can have values like 0.0, 2.03,3.04,7.00 and 9.0 and 10.01, 14.567
 
So now I want that if that BigDecimal value is 0.0 or 7.00 or 9.0 then

i)  I should show 0 and 7 and 9 instead of 0.0 and 7.00 or 9.0
ii) I should show 2.03,3.04 10.01 and 14.56(2 decimal places for all).

View Replies View Related

Round Up Double Numbers To Desired Value?

Mar 28, 2015

I have a double/big decimal value with 2 decimal places....What I want is that if decimal value is .01 - .49, it should round up to 0.50.

Then if decimal value is between 0.51 - 0.98, it will round up to 0.99.

Is there a function in double that can do this? How can I do this?

View Replies View Related

JSF :: Can Place Content In Xhtml To Get Desired View

Apr 3, 2014

I have 2 JSF applications - Let me say A and B.

Application A uses JSP and Application B uses XHTML.

Now, I want Application B also get the same look and feel as Application A.

For this purpose, I have to recode the complete JSP page (of Application A) into XHTML (in Application B).

My question: Is there any better way, like reusing all the content of JSP in XHTML or something like that.

Could I replace the existing XHTML content with the JSP content with some minor modifications?

View Replies View Related

How To Get Desired Output Layout Database Query

Jan 15, 2015

My output of my app its outputting the correct data just no the format ...

code :

DatabaseQuery.java :
import java.io.*;
import java.sql.*;
import java.util.*;
public class DatabaseQuery

[Code] ....

Current output:

Stock holdings by User

User ID User Name
Stock - Description
-------------------------------------------
admin01
Default Admin
DELL Dell Computer Corp
admin01
Default Admin
MSFT Microsoft Computer Corp

[Code] ....

Desired Output :

Stock holdings by User

User ID User Name
Stock - Description
-------------------------------------------
admin01 Default Admin
DELL Dell Computer Corp
MSFT Microsoft Computer Corp
ORCL Oracle Corp

[Code] .....

View Replies View Related

Binary Search Algorithm Won't Return Desired Value

May 10, 2014

This was my initial question: I am confused as to why this won't return the desired value. I keep running into an infinite loop or data is returned that I searched.

I still need to work on whether a user enters a search that doesn't exist......that is where I am stuck....

code has been updated......

int low = 0;
int high = subArray.length;
while (low < high) {
System.out.println("You have entered the while loop");
int mid = (low + high) / 2;

[Code] ....

View Replies View Related

Converting Long Values To String - BufferedWriter Not Printing Desired Results

Apr 20, 2014

Currently, my program converts Long values to String. And when I test it out, it do print out the correct output. However, when the converted String value is passed over to be written in a text file, it seems that BufferedWriter isn't printing out the outcome that it's supposed to be.

saltVs = Long.toString(saltV);
System.out.print(saltVs); //will print out 79723172

Now the problem is here...It only prints out the last digit of the String value (instead of 79723172).

Here is my FileWriter/BufferedWriter part.

Why is that when I run my program using command prompt, it prints out the output that I wanted, but however when it comes to writing to the file, it doesn't come out right.

View Replies View Related

Thermostat - Showcase Time Passing And Temperature Reaching Desired Setting

Feb 25, 2014

Here is the question:

-In your main, you should create a thermostat and thoroughly test it. Be sure to showcase time passing and the temperature reaching the desired setting. Also, showcase switching modes from heating to cooling.

My question is, once it reaches the desired temp, would the thermostat turn off, and then how do you show switching from heating and cooling.

This is what prints.

Thermostat is: true Thermostat is: false The desired temp: 70.0 The current temp: 69.0
Tue Feb 25 19:06:21 CST 2014
Tue Feb 25 20:06:21 CST 2014
Thermostat is: true Thermostat is: false The desired temp: 70.0 The current temp: 70.0

Here is my Class:

import java.util.Random;
import java.util.Date;
public class Thermostat {
private double currentTemp;
private double desiredTemp;
private double heatingThreshold;
private double coolingThreshold;

[Code] .....

View Replies View Related

Read Chars In Text Area And Produce Desired Output For Text Editor Project?

Nov 27, 2014

I am working on a personal project and want to create a text editor to write my code. I am wondering how could I read the last input from the user and if say it was an open curly brace {, then like netbean's my editor will supply the closing curly brace. My java experience is limited but I have tried to read key board input and a few other options that did not work.

View Replies View Related

At The End Of Each Game Prompt Replay

Oct 9, 2014

//*******************************************
//GuessingGame.java
//
//plays the Hi-Lo guessing game with numbers
//
//*******************************************

import java.util.Scanner;
import java.util.Random;
public class GuessingGame
{
public static void main (String[] args)

[Code] ....

View Replies View Related

How To Compile Servlets In Command Prompt

Apr 14, 2014

I have set the CLASSPATH

(C:Program FilesApache-Tomcat-7apache-tomcat-7.0.53libservlet-api.jar,
PATH(C:Program FilesJavajdk1.7.0_51in)

and JAVA_HOME

(C:Program FilesJavajdk1.7.0_51) environmental variable on my windows XP.

I installed tomcat server and its running fine. I made my first servlet(DemoServlet.java) in

C:Program FilesApache-Tomcat-7apache-tomcat-7.0.53webappsDEMOWEB-INFclasses folder.

I made the XML file (web.xml) in C:Program FilesApache-Tomcat-7apache-tomcat-7.0.53webappsDEMOWEB-INF folder. In command prompt I went to the classes folder

(C:Program FilesApache-Tomcat-7apache-tomcat-7.0.53webappsDEMOWEB-INFclasses).

And tried to compile my servlet(javac DemoServlet.java) but it is showing file not found error. I am new to servlets.

View Replies View Related







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