User Input 2 Ints - Output True If One Is In Range Otherwise False

May 14, 2014

How do I make it so that if both numbers are entered between 10 and 20 it will print false?

The assignment: Ask the user to enter 2 ints. Output "true" if one of them is in the range 10 - 20 inclusive, but not both. Otherwise output "false"

12 99 → true
21 20 → true
8 99 → false
15 17 → false

import java.util.Scanner;
 public class Practice {
 public static void main(String[] args) {
 //get user input
Scanner user_input = new Scanner(System.in);
System.out.print("Enter a number: " );

[Code] ....

View Replies


ADVERTISEMENT

User Input True False Questions

Jan 11, 2015

Create an application that generates a quiz. Prompt for the user's first and last name, college major, and confidence in test taking (high, medium, or low). The quiz should contain at least five true/false questions about horticulture. When the user selects the correct answer, a message of positive reinforcement should be displayed. If the user selects the incorrect answer, the correct answer should be displayed with a message of constructive criticism. At the end of the quiz, display the number of correct and incorrect answers as well as the percentage of correct responses for each user.

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

[Code].....

View Replies View Related

True And False / Scanner

Jul 7, 2014

My goal is to ask them for their name and if they got it right then it says yes while if they wrote it wrong, it would say no.

Java Code: public static void main(String[] args) {
Scanner name = new Scanner(System.in);
System.out.println("What is your name?");
String sname = name.nextLine();
name.close();
System.out.println("Are you sure it's " + sname + "?");
Scanner tfname = new Scanner(System.in);
String stfname = tfname.next();

[code]....

View Replies View Related

Multiple Of 3 Between Range Of User Input?

Sep 22, 2014

The program should output all numbers between the starting and ending number that are a multiple of the number N. Your solution must use a for-loop. Here is example output from running the program:

import java.util.Scanner;
public class MultiplyNThree {
@SuppressWarnings("resource")
public static void main(String[] args) {
Scanner userInputScanner = new Scanner(System.in);
System.out.println("Enter 1st number: ");
int start = userInputScanner.nextInt();

[Code] ....

View Replies View Related

Boolean Value (true Or False) Randomly Set?

Jun 15, 2014

I have the code and it works very well, but my professor wants us to use Junit testing to test our code. I've never used JUnit before, how it works. Is it possible to have a boolean value (true or false) randomly set?

Here is the code I need to test:

package musicalinstruments;
class MusicalInstrument {
public String name;
public boolean isPlaying;
public boolean isTuned;
public MusicalInstrument(){
isPlaying = false;
isTuned = false;

[Code]...

View Replies View Related

Truth Table - Display 1s And 0s Instead Of True False

Feb 26, 2005

I'm supposed to take this truth table and alter it so it displays 1's and 0's instead of true false. I'm assumed to do this I would just need to change the variable type and replace true and false with 1 and 0 but every way I try this does not work.

//a truth table for the logical operators.

class LogicalOpTable {
public static void main(String args[]) {
boolean p, q;
System.out.println("P Q AND OR XOR NOT");
p = true; q = true;
System.out.print(p + " " + q +" ");

[Code] ....

View Replies View Related

How To Make Panels Visibility Go False / True With Actionlistener

Jan 28, 2014

I'm trying to create a program that has two labels... one in the top left and one in the top right... so far when i run it only the one in the top right (label2) shows... also In the program there will be multiple button and when I click a button it will show a different panel and then i can go back to the first panel to select other panels... so far i haven't figured out how to make panels visibility go false/true with actionlistener. last thing... when i have more then one panel added to the frame none of them show up.

Java Code:

//Matthew
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test{
public static void main(String[] args){

[code]...

View Replies View Related

True / False Result Cannot Be Printed Due To Variable Answer

Jun 30, 2014

Code in the book is written exactly like this:

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

//Generate random two integers using utility System.currentTimeMillis
int n1 = (int)(System.currentTimeMillis() % 10);
int n2 = (int)(System.currentTimeMillis() / 7 % 10);

[Code] ....

however the true/false result can not be printed due to the "answer" variable...

View Replies View Related

Java Method - Return True If String Can Be Found As Element Of Array And False Otherwise

Mar 17, 2014

I am new to Java and would like to ask how to do this question specifically:

Code a Java method that accepts a String array and a String. The method should return true if the string can be found as an element of the array and false otherwise. Test your method by calling it from the main method which supplies its two parameters (no user input required). Use an array initialiser list to initialise the array you pass. Test thoroughly.

View Replies View Related

Take Input From User And Append Some Text To It - Output Null

Sep 20, 2014

I'm using one method to take input from the user and append some text to it, then I am trying to return the value of the variable and use another method to print it out. But for some reason whenever I try doing this I can't print out anything and I only get a "null" output. Why this is happening?

package Homework3;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Homework3 {

[Code] .....

View Replies View Related

Display User Input In Lower Case - Why No Output In Second TextField

Jun 2, 2014

I've done a lot of hardwork for this assignment but I don't know what's why the second textfield which is for output is blank.

All I want is to get the input from user in textfield a and display it in textfield b in lower case.

public void KeyPressed (KeyEvent ke) {
String letter2="";
if(ke.getKeyCode()==KeyEvent.VK_A)
{
letter2=letter2+"a";
btextfield.setText(letter2);

[Code] .....

View Replies View Related

JOption Pane - Output Number With Greatest Value From User Input

Nov 7, 2014

Using JOptionPane,ask for 10 numbers from the user.Use an array to store the values of these 10 numbers.Output on the screen the number with the greatest value.

View Replies View Related

User Input 3 Sides - Output Invalid If Does Not Make A Triangle

Nov 4, 2014

I had to make a program that allows the user to enter 3 sides and it should output "invalid" if it does not make a triangle and if it does make a triangle it calculates the area of the triangle and it doesnt seem to do anything but say "invalid" as the out put.

import java.util.Scanner;
public class MyTriangle {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean valid = true;
double side1, side2, side3, area, A;
 
[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

Sort User Input 3 Words Alphabetically And Output Middle Word

Feb 8, 2014

I have a project requiring me to build a program having a user input 3 words, sort them alphabetically and output the middle word. I have done some searching and seem to only come back with results for sorting 2 words. I so far have code to get the user input but I am completely lost as to how to sort them alphabetically.

import java.util.Scanner; //The Scanner is in the java.util package.
public class MiddleString {
public static void main(String [] args){
Scanner input = new Scanner(System.in); //Create a Scanner object.
String str1, str2, str3;
System.out.println("Please enter three word words : "); //Prompt user to enter the three words

[Code]...

we havnt done arrays yet and I THINK i have to do compareTo.....how to use it?

View Replies View Related

Creates Array Of 3 Consecutive Ints (0 - 7) User Has To Guess What Those Numbers Are

Aug 16, 2014

I have the following program. In a nutshell, I creates an array of 3 consecutive ints and the user has to guess what those numbers are, knowing that they are between 0 and 7, this is from Head First Java. In the book, the code has a bug that is made on purpose and they challenge you to fix it. As you can see bellow, every time a user's guess matches a int in the array, the NumOfHits is increased by one. When the NumOfHits is 3 (the length of the array) the game finishes.

The bug is that if you guess 1 and 1 is in the array, if you type in 1, 3 times, I will increase the NumOfHits 3 times and end the game as if you had won. To solve the bug, you need to find a way to tell the program that if the user already guessed that number, it should no longer be taken into account and we shouldn't increase the NumOfHits if the same number is provided.

I "fixed" this by searching for the index of the number that matches an int of the array, and changing that index's value to 100, since the user knows that the numbers are between 0 and 7, they will not submit 100.

The result? If the user submits 2 and 2 is in the array, the value of its indexed is replaced by 100, so that if the user submits 2 again it doesn't count as a match.

Please see the comments in the code

import java.io.*;
import java.util.Arrays;

class DotCom{
int NumOfHits = 0;
int[] LocationCells;
public void setLocationCells(int[] locs){
LocationCells = locs;

[Code] .....

The code works, but for some reason, some times I get a ArrayIndexOutOfBoundsException run time error and it appears to be somehow random. This is what I saw on the command line earlier

C:Userspablo.alvarez>java launchDotCom

Enter a number: 3
missed
Enter a number: 4
hit
Enter a number: 5
hit
Enter a number: 7
missed
Enter a number: 5
missed
Enter a number: 4
missed
Enter a number: 3
missed
Enter a number: 4
missed
Enter a number: 5
missed
Enter a number: 6

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at DotCom.checkYourSelf(launchDotCom.java:16)
at DotComGame.startGame(launchDotCom.java:59)
at launchDotCom.main(launchDotCom.java:72)

As you can see here, 3 returned 'missed' but 4 and 5 returned hit. Since the numbers are in sequence and are only 3, it makes sense that the last number is 6 (4,5,6). You will then notice that when I submitted 3 and 4 again it returned 'missed' as expected, because the numbers were already matched earlier, but then when I submitted 6, it returned the error seen above.

Sometimes this doesn't happen at all, sometimes it happens at the 2nd guess or third, it appears to be somehow random. I'm familiar with the error message, but I don't see where I'm using an index that doesn't exist in the array. The only time I'm referencing an array index is on

LocationCells[index] = 100;

but the index variable will only hold a value if the int submitted by the user matches on of the values in one of the indexes, so how is it that I'm going over the number of available indexes?

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

Unable To Match Input String With Range Of Values And Getting PatternSyntaxException

Jun 12, 2014

I am trying to match the input string with range of values and getting PatternSyntaxException. below is my code

public static void main(String[] args) {
String regex="01[4-6]";
String code="015";
boolean bool=code.matches(regex);
System.out.println(bool);
}

output: true

But when i try to give range in double digits, it throws exception. What if i have to match a number(as string) between 10 to 25 or like that.

public static void main(String[] args) {
String regex="0[14-16]";
String code="015";
boolean bool=code.matches(regex);
System.out.println(bool);
}

Output:

Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal character range near index 5
0[14-16]
^

View Replies View Related

Long Type Output File - Print Prime Numbers Between Given Range Of Numbers

Aug 22, 2014

I tried to create file and write the output of my program in it in java when i use WriteLong then the file does not contain long value, how I create this file my program is to print prime numbers between 500000 to 10000000

public class primenumber {
public static void main(String[] args) {
long start = 5000000;
long end = 10000000;
System.out.println("List of prime numbers between " + start + " and " + end);
for (long i = start; i <= end; i++) {
if (isPrime(i)) {
System.out.println(i);

[Code] ....

View Replies View Related

Switch Statement That Decides What Range To Print Based On Letter Grade Input

Nov 1, 2014

Alright so I wrote a switch statement that decides what range to print based on the letter grade input.

import java.util.Scanner;
public class SwitchPractice
{
public static void main(String [] args)
{
Scanner scan = new Scanner(System.in);

[code]...

It works fine, but once it the user enters a letter grade and then displays the value, it does not prompt the user for another letter grade so that it can perform the output again. Also if I wanted to display an error message to the user if they enter an invalid letter grade how would I do that. I tried using a while loop and if statement in the switch statement but that didn't work.

View Replies View Related

Generate QR Code From Input Text And Display Information About Input / Output Bits

Nov 12, 2014

I am trying to write a program that will generate a QR Code from an input text and also display some information about the input/output bits. So far I have created the frame and what to do next. And I'm not sure if I am on the right track since my level of programming is not that great. By the way, I am using zxing libraries from GitHub. I know, there are plenty of generators online for the QR Code, but that is not what I am looking for. As you can see on the attached image, I am more interested in the efficiency of encoding 2D data. Also, I noticed that almost all the online projects regarding 2D codes are for Android. Which is not very useful.

// QR Code Generator
package qrcode;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

[Code]....

View Replies View Related

Number Guessing Program - Computer Guesses A Number That User Is Thinking Of Within Certain Range

Mar 28, 2015

I have a beginning Java Program I have been working on that creates a number guessing program where the computer guesses a number that the user is thinking of within a certain range. I so far have the program below, but need getting rid of a few kinks/ adding features.

-First, I need to set it up so that the user will be prompted and give a range. It should run and produce something like this:

Welcome to this first-ever mind-guessing program!

Please pick a range (higher than 1 and no larger than 50): 32

You will choose a number between 1 and 32... and I will try to guess it.

With each of my guess, you will tell me whether I am too high (h or H), too low (l or L), match (m or M), or you want to quit (q or Q). My objective is to find the number using as few guesses as possible.

-Second, the game is supposed to give up and restart after failing the five, guesses, but for some reason, after it fails the fifth time, it prompts a fifth guess once again instead, then restarts after- I need to prevent this, so that it should look something like this:

My fourth guess is 17: h
My guess is too high?

My fifth guess is 16: h
*** I am unlucky this round. I give up.

Let's play!

My first guess is 10:
etc..

import java.util.*;
import java.lang.Math;
public class numguessprac1 {
// Declaring variables
public static String input;
public static int quit;
public static int guess;
public static int wins;

[Code] ....

View Replies View Related

Java Program That Will Ask A User To Input Grades Until User Inputs Sentinel Value

Apr 9, 2014

Write a java program that will ask a user to input grades until the user inputs the sentinel value -1. The program should test each input to determine whether 0<=grade<=100. If the grade entered is within this range, the program should print "Grade accepted;" if not, the program should print "Invalid input".

View Replies View Related

Program That Takes User Odd Number And Print Prime Numbers Lower Than User Input

Nov 4, 2014

I have been struggling with this program for weeks. This program is supposed to take a user's inputted odd number and then print out all prime numbers lower than that number.

public class PrimeNumber
{
Scanner scan = new Scanner(System.in);
int userNum;
String neg;

public void getUserNum()

[code]...

View Replies View Related

Where To Input Segment To Get User Input Dialog

Apr 9, 2015

Where do I input this segment?

Scanner user_input = new Scanner( System.in );// This line goes at the top with the other global variables

public void getUserInput(){
System.out.print("Enter litres: ");
litres = user_input.nextDouble( );
System.out.print("Enter mileage: ");
mileage = user_input.nextDouble( );
System.out.print("Enter speed: ");
speed = user_input.nextDouble( );

[Code] ....

This is my client file.

class Client{
public static void main(String []args){
Bike R1=new Bike(5.0, 60.0,30.0);//create bike object with params
Bike R2=new Bike();//without params
System.out.println(R1.increaseSpeed());//calling methods
System.out.println(R1.maxDistance());
System.out.println(R2.increaseSpeed());
System.out.println(R2.maxDistance());
}
}

View Replies View Related

Input And Output Via JTextField

Jun 29, 2014

How would I display certain output text if the text entered equaled "text".

So lets say you enter a question into a JTextField. You press a button that submits the text. The output displayed depends on what the question you asked was.

Think about it as "If question = What color is the sky? On submit, display text "Blue". How to implement this.

View Replies View Related







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