Inputting two separate integer numbers with no space between them? - java

I am making a lottery game and I am wondering if there is a way to type in two integer numbers, like 3 and 4 but type them in as 34 and have them read individually? I need to have them read individually as the game will reward if one of the numbers entered matches one of the randomly generated numbers. My code so far is this:
import java.util.Scanner;
import java.util.Random;
public class Lottery {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
Random random = new Random();
int guess, guess2, counter;
int win1000, win10, win1;
int number = random.nextInt(10); int number2 = random.nextInt(10);
System.out.println("Welcome to the Super Lottery! \nLet's Play!");
System.out.println("Round 1 - you have 5 chances to win money");
System.out.print("Enter your guess: ");
guess = keyboard.nextInt();
guess2 = keyboard.nextInt();
System.out.println("The lottery number is " + number + number2);
win1 = 1;
win10 = 10;
win1000 = 1000;
counter = 1;
while(counter < 5) {
number = random.nextInt(10); number2 = random.nextInt(10);
System.out.print("Enter your guess: ");
guess = keyboard.nextInt(); guess2 = keyboard.nextInt();
System.out.println("The lottery number is " + number + number2);
counter++;
if(guess == number2 && guess2 == number) {
System.out.println("You guessed both numbers, but in a different order! You win $" + win10);
}
else if(guess == number && guess2 == number2) {
System.out.println("CONGRATULATIONS! You win the Super Lottery!" + win1000);
}
else if(guess == number || guess2 == number2) {
System.out.println("You guessed one number! You win $" + win1);
}
}
}
}

You can use the substring method to separate the numbers by their index so for a visual example:
String numbers = "12345";
System.out.println(numbers.substring(0,1));
System.out.println(numbers.substring(1,2));
System.out.println(numbers.substring(2,3));
System.out.println(numbers.substring(3,4));
System.out.println(numbers.substring(4,5));
}
Which will print out:
1
2
3
4
5
If you want to separate with a comma you could do something like this:
String numbers = "12345";
char[] temp = numbers.toCharArray();
for(int i = 0; i < temp.length; i++) {
System.out.print(temp[i] + ",");
}
Output:
1,2,3,4,5,

Since random.nextInt has 10 as an argument, that means the random generated number will be from 0-9.
With that being said I am going to assume the number that the player will enter will have be from 00 to 99.
You can use a String for that and then split it into two pieces.
Check this code example:
Scanner keyboard = new Scanner(System.in);
String number = keyboard.nextLine();
int guess, guess2;
guess = Integer.valueOf((number.substring(0, 1)));
guess2 = Integer.valueOf((number.substring(1, 2)));
System.out.println(guess);
System.out.println(guess2);

Try something like:
Scanner in = new Scanner(System.in);
int number = in.nextInt();
int firstNumber = number / 10;
int secondNumber = number % 10;
System.out.println("Result: "+ firstNumber + " & " + secondNumber);

Related

How can I print numbers between two numbers that I enter?

I am creating a program which asks the user to enter two numbers. It will then print the numbers the user entered and the numbers between the two numbers in numerical order. I declared and initialized two variables, which are 'number1' and 'number2'.
int number1;
int number2;
do{
Scanner input = new Scanner(System.in);
System.out.print("Enter the first number: " );
number1 = input.nextInt();
System.out.print("Enter the second number: " );
number2 = input.nextInt();
if(number1 == number2)
{
System.out.println("The numbers you entered equal with each other. Try again.\n");
}
}while(number1 == number2);
if (number1 > number2)
{
for(int a = number2; a <= number1; a++)
{
System.out.print(a + " ");
}
}
else
{
for(int a = number1; a <= number2; a++)
{
System.out.print(a + " ");
}
}
How do I make it so it also prints only the numbers between 'number1' and 'number2'?
The first part of your code was fine, if you want to do it this way.
Scanner scanner = new Scanner(System.in);
int first, second;
do {
System.out.print("Enter the first number: ");
first = scanner.nextInt();
System.out.print("Enter the second number: ");
second = scanner.nextInt();
if (first == second) {
System.out.println("The numbers you entered equal with each other. Try again.\n");
}
} while (first == second);
For the second part, I'd recommend determining which number is which (smaller/bigger) instead of using duplicate code. Also you need to only cycle through numbers in between the numbers you chose, so you have to change your for cycle
//determine which number is bigger/smaller
int smaller = Math.min(first, second);
int bigger = Math.max(first, second);
for(int i = smaller+1 ; i < bigger ; i++){
System.out.print(i + " ");
}
The only odd scenario here is where you input two numbers which are adjacent, which will output no numbers. For example 3 and 4.
int max = Math.max(number1, number2);
for(int i = (max == number1 ? number2 : number1) + 1 ; i < max ; i++) {
System.out.println(i);
}

How to make while loop check if there are 16 digits in a string

How do I make the loop check if there is 16 digits in a string and reset the string if there is not enough. I am trying to make a credit card program that will calculate the check digit. I have everything else working I just cant get the program to check the number of digits in the user inputted string.Thanks for any and all help!
import java.util.Scanner;
public class LuhnAlgorithm {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number credit card number (Enter a blank line to quit: ");
String nums = input.nextLine();
int i = 0;
char chk = nums.charAt(15);
while(!nums .equals("") ) {
if (nums.length()<16 || nums.length() > 15){ //How do I get this line to reset the while loop?
System.out.println("ERROR! Number MUST have exactly 16 digits.");
}
int sum = 0;
for( i = 0; i < 15; i++) {
char numc = nums.charAt(i);
int num = Character.getNumericValue(numc);
if ( i % 2 == 0 ) {
num = num * 2;
if ( num >= 10) {
num = num - 9;
}
}
sum = num + sum;
}
int sum2 = sum % 10;
if (sum2 > 0) {
sum2 = 10 - sum2;
}
int chk2 = Character.getNumericValue(chk);
System.out.println("The check digit should be: " + sum2);
System.out.println("The check digit is: " + chk);
if ( sum2 == chk2) {
System.out.println("Number is valid.");
}
else {
System.out.println("Number is not valid. ");
}
System.out.print("Enter a number credit card number (Enter a blank line to quit:) ");
nums = input.nextLine();
}
System.out.println("Goodbye!");
input.close();
}
}
You can include your code that you only want done if the length ==16 in an if statement.
Meaning, instead of:
if (nums.length != 16) {
//code if there is an error
}
//code if there is no error
you can do:
if (nums.length == 16) {
//code if there is no error
} else {
//code if there is an error
}
(I also want to point out that you set chk = nums.charAt(15) before your while loop, but you don't reset it in the while loop for the next time the user inputs a new credit card number.)
You can bring the prompts and all your initialization except the scanner itself into the while loop. Then if they say "", break to exit the loop. If they say a number that is too short or too long, say continue to go back to the prompting.
Thus:
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (true) {
System.out.print("Enter a number credit card number (Enter a blank line to quit: ");
String nums = input.nextLine().trim();
if (nums.length() == 0) {
break; //exits while loop
}
if (nums.length() != 16) { //How do I get this line to reset the while loop?
System.out.println("ERROR! Number MUST have exactly 16 digits.");
continue; //goes back to the beginning right away
}
//still here, process the number
char chk = nums.charAt(15);
int sum = 0;
for (int i = 0; i < 15; i++) {
char numc = nums.charAt(i);
int num = Character.getNumericValue(numc);
if (i % 2 == 0) {
num = num * 2;
if (num >= 10) {
num = num - 9;
}
}
sum = num + sum;
}
int sum2 = sum % 10;
if (sum2 > 0) {
sum2 = 10 - sum2;
}
int chk2 = Character.getNumericValue(chk);
System.out.println("The check digit should be: " + sum2);
System.out.println("The check digit is: " + chk);
if (sum2 == chk2) {
System.out.println("Number is valid.");
} else {
System.out.println("Number is not valid. ");
}
}
System.out.println("Goodbye!");
input.close();
}
}

While Loop - Sum of even numbers between two inclusive integers

EDIT: Lol I would've never thought you guys would be such savages. I thought this would be the right place for a college student to come and seek for answers. If I didn't care, why would I even be here.. Basically my question is different from the other ones because I do need the first loop to state my even numbers ie: 10 12 14 16 18 20. I sincerely hoped I would get some feedback on how to reinstate the original value of firstNum for the second loop, not criticism expecting to get selected as an answer for points in a matter of seconds.
I've implemented a code that I saw as an answer in a very similar question on the forum. Yet when I compile it, the output for my sum code doesn't return anything. It's just blank. Can I get some feedback on what I am missing or doing wrong? Thank you. BTW I can only use while loop.
System.out.println("Enter an integer:");
int firstNum = keyboard.nextInt();
System.out.println("Enter another integer larger than the first one:");
int secondNum = keyboard.nextInt();
System.out.println();
int mod = firstNum % 2;
int sum = 0;
if (mod != 0)
{
firstNum++;
}
System.out.print("Even numbers: ");
while (firstNum <= secondNum)
{
System.out.print(firstNum + " ");
firstNum += 2;
}
System.out.println();
System.out.print("Sum of even numbers: ");
while (firstNum <= secondNum)
{
System.out.print(sum);
sum += firstNum;
firstNum += 2;
}
The first loop only prints the even numbers, it can not add because the firstNum reaches the secondNum.
If you want to keep the same code you have, you only need to add a new variable that prints even numbers. So the first loop will print the even numbers and the second loop will do the sum.
For example:
int evenNumber;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter an integer:");
int firstNum = keyboard.nextInt();
System.out.println("Enter another integer larger than the first one:");
int secondNum = keyboard.nextInt();
System.out.println();
int mod = firstNum % 2;
int sum = 0;
if (mod != 0) {
firstNum++;
}
evenNumber = firstNum;
System.out.print("Even numbers: ");
while (evenNumber <= secondNum) {
System.out.print(evenNumber + " ");
evenNumber += 2;
}
System.out.println();
System.out.print("Sum of even numbers: ");
while (firstNum <= secondNum) {
sum += firstNum;
firstNum += 2;
}
System.out.println(sum);
This code is not optimal, but it complies with what you need. But if you want to do better, you can do it like this:
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter an integer:");
int firstNum = keyboard.nextInt();
System.out.println("Enter another integer larger than the first one:");
int secondNum = keyboard.nextInt();
int total = 0;
System.out.print("Even Numbers: ");
while (firstNum <= secondNum) {
if (firstNum % 2 == 0) {
System.out.print(firstNum + " ");
total += firstNum;
}
firstNum++;
}
System.out.println("\nSum: " + total);
During your first while, you already changed the value of firstNum. So basically, when you arrive to the second while, you don't even enter it.
I would simply change the first loop and delete the second one :
while (firstNum <= secondNum) {
System.out.print(firstNum + " ");
sum += firstNum;
firstNum += 2;
}
System.out.println();
System.out.print("Sum of even numbers: ");
System.out.println(sum);
In your first while loop -
while (firstNum <= secondNum){
System.out.print(firstNum + " ");
firstNum += 2;
}
you keep incrementing variable firstNum until you reach secondNum, so the condition of the second while loop (firstNum <= secondNum) will never evaluates to true.
A very simple way to achieve your goal can be as follows.
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter an integer:");
int firstNum = keyboard.nextInt();
System.out.println("Enter another integer larger than the first one:");
int secondNum = keyboard.nextInt();
int total = 0;
while(firstNum <= secondNum){
if(firstNum%2 == 0)
total += firstNum;
firstNum++;
}
System.out.println("Sum: " + total);

How do I display even and odd numbers based on user input? - java

Please help... I need to write a program that displays even and odd numbers based on user input, but it loops forever during my last print statement.
Here is what I have so far, what's wrong?
import java.util.Scanner;
public class Integer {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = 0;
int odd = 0;
int even = 0;
System.out.println("How many numbers will be entered?");
n = input.nextInt();
while (n < 0 || n > 100) {
System.out.println("ERROR! Valid range 0-100. RE-Enter:");
n = input.nextInt();
}
while(n >= 0) {
System.out.println("Now enter " + n + " integers: ");
int num = input.nextInt();
while(num > 0) {
for(int i = 0; i <= n; i++){
if (i % 2 == 0) {
even++;
}
else {
odd++;
}
System.out.println("You entered " + odd + " odd numbers and " + even + " even numbers.");
}
}
}
}
}
Scanner input = new Scanner(System.in);
int n = 0;
int odd = 0;
int even = 0;
System.out.println("How many numbers will be entered?");
n = input.nextInt();
while (n < 0 || n > 100) {
System.out.println("ERROR! Valid range 0-100. RE-Enter:");
n = input.nextInt();
}
while (n > 0) {
System.out.println("Now enter integer " + n + ": ");
int num = input.nextInt();
if (num % 2 == 0) {
even++;
} else {
odd++;
}
n--;
}
System.out.println("You entered " + odd + " odd numbers and " + even + " even numbers.");
Explanation:
You handle n inputs from user and just check if the input is odd or even with an if-statement. You were over-complicating it.
You have a couple of issues with too many while loops. You don't decrement n anywhere but you also loop on num while it's greater than 0. So it will loop forever if they stick something other than 0 in there.
System.out.println("Now enter " + n + " integers: ");
while(n >= 0) {
int num = input.nextInt();
if(num > 0) {
if (num % 2 == 0)
even++;
else
odd++;
}
n--;
}
System.out.println("You entered " + odd + " odd numbers and " + even + " even numbers.");
The infinite/forever loops is happening because of input.nextInt() and this is due to the behavior of next() method of Scanner class.
next(), nextInt() and its other variations do not move the cursor to the next line (end of current line - instead reads the first complete token with default delimiter as whitespace). nextLine() method on the other hand reads the whole input and moves the cursor to the next line.
Your code can be fixed by adding input.nextLine() to move the cursor to the beginning of the line before taking the next input. See below modified code.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = 0;
int odd = 0;
int even = 0;
System.out.println("How many numbers will be entered?");
n = input.nextInt();
while (n < 0 || n > 100) {
System.out.println("ERROR! Valid range 0-100. RE-Enter:");
n = input.nextInt();
}
for(int i = 0 ; i < n; i++) {
System.out.println("Now enter integer " + (i+1) + " of " + n + " integers: ");
int num = input.nextInt();
while(num < 0) {
System.out.println("ERROR! Valid range 0-100. RE-Enter:");
num = input.nextInt();
}
//this line is needed to move the input cursor to next line (due to use of nextInt() above)
input.nextLine();
if(i%2==0) {
even++;
} else {
odd++;
}
}
System.out.println("You entered " + odd + " odd numbers and " + even + " even numbers.");
}

Why does my code not run?

I have this code:
import java.util.Scanner;
public class PositiveNegative { public static void main(String[] args) {
int numbers, plus = 0, minus = 0;
int count = 0;
double total = 0;
Scanner scan = new Scanner(System.in);
System.out.print("Enter an integer (0 to quit): ");
numbers = scan.nextInt();
while(numbers != 0)
{
total += numbers;
if(numbers > 0)
plus++;
if(numbers < 0)
minus++;
}
System.out.println("The number of positives is: " +plus);
System.out.println("The number of negatives is: " +minus);
System.out.println("The number of total is: " +total);
}
}
The problem with is that I try to run it and type the numbers but it does nothing. I want it so that when you type 0 it stops taking numbers and starts processing the code. What should I do?
Try this:
import java.util.Scanner;
public class PositiveNegative {
public static void main(String[] args) {
int numbers = 0, plus = 0, minus = 0;
double total = 0;
do{
Scanner scan = new Scanner(System.in);
System.out.print("Enter an integer (0 to quit): ");
numbers = Integer.valueOf(scan.nextLine());
total += numbers;
if (numbers > 0)
plus++;
if (numbers < 0)
minus++;
}
while (numbers != 0);
System.out.println("The number of positives is: " + plus);
System.out.println("The number of negatives is: " + minus);
System.out.println("The number of total is: " + total);
}
}
Put your Scanner in the while loop so that everytime loop start it will ask for User input.
You need to update numbers or your loop will run for ever. And I recommend using braces (and an else). Something like,
System.out.print("Enter an integer (0 to quit): ");
numbers = scan.nextInt();
while (numbers != 0) {
total += numbers;
if (numbers > 0) {
plus++;
} else if (numbers < 0) {
minus++;
}
System.out.print("Enter an integer (0 to quit): ");
numbers = scan.nextInt();
}
Alternatively, you could use a do-while loop. Then you only need one copy of the prompt. Like,
do {
System.out.print("Enter an integer (0 to quit): ");
numbers = scan.nextInt();
total += numbers;
if (numbers > 0) {
plus++;
} else if (numbers < 0) {
minus++;
}
} while (numbers != 0);
You have to modify numbers each time to make it work in your while.
So, in your existing code, just comment out numbers = scan.nextInt(); and use below--
// numbers = scan.nextInt(); //comment out this call
while ((numbers = scan.nextInt()) != 0) {
....
this will give you desired output--
Enter an integer (0 to quit): 9
4
-9
1
0
The number of positives is: 3
The number of negatives is: 1
The number of total is: 5.0

Categories