hi guys I'm trying to make a lottery program And I'm trying to get the users input to after asking if they want to retry playing. But my program ends before reaching the while loop.
public class Main {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String yn = "";
Lottery.getTicket();
Lottery.generateWinningNumbers();
System.out.print("\nWould you like to try again? ");
while(input.hasNextLine())
{
yn = input.nextLine();
if(yn.equalsIgnoreCase("y"))
{
Lottery.getTicket();
Lottery.generateWinningNumbers();
}
else
{
System.out.println("Done");
}
}
input.close();
}
}
In my Lottery class:
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
import java.util.Scanner;
public class Lottery {
public static Set<Integer> generateWinningNumbers()
{
Random rndNumbers = new Random();
TreeSet<Integer> winningNumbers = new TreeSet<Integer>();
int max = 40;
int min = 1;
int range;
int sixNum;
for(int i = 0; i < 6; i++)
{
range = max - min + 1;
sixNum = rndNumbers.nextInt(range) + min;
while(winningNumbers.contains(sixNum))
{
sixNum = rndNumbers.nextInt(range) + min;
}
winningNumbers.add(sixNum);
}
System.out.print("Winning Numbers: " + winningNumbers);
return winningNumbers;
}
public static Set<Integer> getTicket()
{
int userInput;
TreeSet<Integer> getNumbers = new TreeSet<Integer>();
Scanner input = new Scanner(System.in);
System.out.println("Enter your 6 numbers between 1-40: ");
for (int i = 0; i<6 ; i++)
{
System.out.print(i+1 + ": ");
userInput = input.nextInt();
while( userInput <1 || userInput > 40 || getNumbers.contains(userInput))
{
if (getNumbers.contains(userInput))
{
System.out.println("Number already picked");
userInput = input.nextInt();
}
if(userInput < 1 || userInput > 40)
{
System.out.println("Invalid. Pick a number between 1-40");
userInput = input.nextInt();
}
}
getNumbers.add(userInput);
}
input.close();
System.out.println("Your ticket was: " + getNumbers);
return getNumbers;
}
}//end of Lottery class
You are using the Scanner Object
Scanner input = new Scanner(System.in);
in Lottery.getTicket and you do
input.close();
This means that System.in will be closed for the rest of the program
Try passing the Scanner object from main to other classes and method that need it.
Related
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter first number");
int a = sc.nextInt();
boolean aValid = sc.hasNextInt();
System.out.println(aValid);
System.out.println("Enter Second number");
int b = sc.nextInt();
boolean bValid = sc.hasNextInt();
System.out.println(bValid);
if(aValid && bValid){
System.out.println("Sum of number is "+(a+b));
}
else{
System.out.println("Enter integer number");
}
}
}
I try to get input of a and b and validate that a and b are integers. But it takes three input. It gives the sum of the first two numbers and gives the validity of the last two.
you need to place check integer statements before taking input
boolean aValid = sc.hasNextInt();
int a = sc.nextInt();
boolean bValid = sc.hasNextInt();
int b = sc.nextInt();
Running this code makes the error fairly obvious. Some additional debug statements, running the code in debug or testing would also make it clear.
Scanner.nextInt() will throw an exception if it gets a non integer.
Scanner.hasNextInt() on the other hand returns a boolean value to warn you in advance whether it has an integer or not - it does not take the value from the Scanner.
This code has a couple of tiny corrections that will achieve the outcome you stated.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int a = 0;
int b = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter first number");
boolean aValid = sc.hasNextInt();
if (aValid) {
a = sc.nextInt();
} else {
sc.next(); //Discard the non-integer input.fr
}
System.out.println(aValid);
System.out.println("Enter Second number");
boolean bValid = sc.hasNextInt();
if (bValid) {
b = sc.nextInt();
}
System.out.println(bValid);
if (aValid && bValid) {
System.out.println("Sum of number is " + (a + b));
} else {
System.out.println("Enter integer numbers only");
}
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
int a = 0;
int b = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter first number");
boolean aValid = sc.hasNextInt();
if (aValid) {
a = sc.nextInt();
}
System.out.println(aValid);
System.out.println("Enter Second number");
boolean bValid = sc.hasNextInt();
if (bValid) {
b = sc.nextInt();
}
System.out.println(bValid);
if (aValid && bValid) {
System.out.println("Sum of number is " + (a + b));
} else {
System.out.println("Enter integer numbers only");
}
}
}
I need help making it so that the entire script will loop from the beginning if the user types y at the end and the script ends if the user types n at the end. Thanks for any help.
package test123;
import java.util.Scanner;
public class test321 {
public static void main(String[] args) {
int n = 1;
int c;
String playAgain;
do {
System.out.println("Enter a number");
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
if ( n < 0 )
System.out.println("Number cant be a negative.");
else
{
int x=n*(n-1)*(n-2);
System.out.println("Factorial= "+x);
System.out.println("Do you want to continue? (y/n):");
playAgain = scanner.nextLine();
}while (!playAgain.equals("y"));
}
}
}
package test123;
import java.util.Scanner;
public class test321 {
public static void main(String[] args) {
int n = 1;
int c;
String playAgain;
do {
System.out.println("Enter a number");
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
if ( n < 0 )
System.out.println("Number cant be a negative.");
else
{
int x=n*(n-1)*(n-2);
System.out.println("Factorial= "+x);
System.out.println("Do you want to continue? (y/n):");
playAgain = scanner.nextLine();
} // end else
} // end do
while (playAgain.equals("y"));
}// end main
} // end class
Change
while (!playAgain.equals("y"));
to
while (!playAgain.equals("n"));
How can i get the output on how many times the program has looped?
Example output :
Enter Your Name: Harith
Harith, Please enter 4 numbers
Number 1:6
Number 2:7
Number 3:5
Number 4:10
Smallest = 5
Largest = 10
This program has repeated for 2 times.
Heres is my code:
import java.io.*;
class loop
{
public static void main(String args[])throws IOException
{
BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
String temp,name;
int number,largest,smallest;
System.out.print("Enter Your Name: ");
name = cin.readLine();
int num[] = new int[4];
System.out.println(name+", Please enter 4 numbers");
for(int i=0;i<4;i++)
{
System.out.print("Number "+(i+1)+":");
temp = cin.readLine();
num[i] = Integer.parseInt(temp);
}
largest=num[0];
smallest=num[0];
for(int i=0;i<4;i++)
{
if(num[i]>largest)
{
largest=num[i];
}
else if(num[i]<smallest)
{
smallest=num[i];
}
}
System.out.println("Smallest = "+smallest);
System.out.println("Largest = "+largest);
}
}
If you just want to know how many times the loops looped, introduce a variable for example int loopCounter = 0;
Then before the end of each loop in your program increment that variable by one and at the end print that variable.
For example.
import java.io.*;
class loop {
public static void main(String args[]) throws IOException {
int loopCounter = 0;
BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
String temp, name;
int number, largest, smallest;
System.out.print("Enter Your Name: ");
name = cin.readLine();
int num[] = new int[4];
System.out.println(name + ", Please enter 4 numbers");
for (int i = 0; i < 4; i++) {
System.out.print("Number " + (i + 1) + ":");
temp = cin.readLine();
num[i] = Integer.parseInt(temp);
loopCounter++;
}
largest = num[0];
smallest = num[0];
for (int i = 0; i < 4; i++) {
if (num[i] > largest) {
largest = num[i];
} else if (num[i] < smallest) {
smallest = num[i];
}
loopCounter++;
}
System.out.println("Smallest = " + smallest);
System.out.println("Largest = " + largest);
System.out.println("Looped " + loopCounter + " times.");
}
}
Please put one variable to count the loop iteration
package com.test.servlet;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class A1 {
public static void main(String args[])throws IOException
{
BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
String temp,name;
int largest,smallest;
System.out.print("Enter Your Name: ");
name = cin.readLine();
int num[] = new int[4];
System.out.println(name+", Please enter 4 numbers");
int loopCount = 0;
for(int i=0;i<4;i++)
{
System.out.print("Number "+(i+1)+":");
temp = cin.readLine();
num[i] = Integer.parseInt(temp);
loopCount++;
}
System.out.println("Loop Counted :: "+loopCount);
largest=num[0];
smallest=num[0];
for(int i=0;i<4;i++)
{
if(num[i]>largest)
{
largest=num[i];
}
else if(num[i]<smallest)
{
smallest=num[i];
}
}
System.out.println("Smallest = "+smallest);
System.out.println("Largest = "+largest);
}
}
import java.io.*;
class loop
{
public static void main(String args[])throws IOException
{
BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
String temp,name;
int number,largest,smallest;
System.out.print("Enter Your Name: ");
name = cin.readLine();
int num[] = new int[4];
System.out.println(name+", Please enter 4 numbers");
for(int i=0;i<4;i++)
{
System.out.print("Number "+(i+1)+":");
temp = cin.readLine();
num[i] = Integer.parseInt(temp);
}
largest=num[0];
smallest=num[0];
int i;
for(i=0;i<4;i++)
{
if(num[i]>largest)
{
largest=num[i];
}
else if(num[i]<smallest)
{
smallest=num[i];
}
}
System.out.println("Smallest = "+smallest);
System.out.println("Largest = "+largest);
System.out.println("The loop has executed "+i+" times");
}
}
Here's my answer after a few try and error. Thanks for help.
import java.io.*;
class loop
{
public static void main(String args[])throws IOException
{
BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
String temp,name;
int largest,smallest,time=0,i;
do{
System.out.print("Enter Your Name: ");
name = cin.readLine();
int num[] = new int[4];
System.out.println(name+", Please enter 4 numbers\n");
for(i=0;i<4;i++)
{
System.out.print("Number " +(i+1)+":");
temp = cin.readLine();
num[i] = Integer.parseInt(temp);
}
largest=num[0];
smallest=num[0];
for(i=0;i<4;i++)
{
if(num[i]>largest)
{
largest=num[i];
}
else if(num[i]<smallest)
{
smallest=num[i];
}
}
System.out.println("Smallest = "+smallest);
System.out.println("Largest = "+largest+"\n");
time++;
}while(smallest<=5);
System.out.println("The program has been repeated for "+(time)+" times");
System.out.print("Thanks You!!");
}
}
import java.util.Scanner;
class Digitsdisplay {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
System.out.println("Please enter a value: ");
int value = input.nextInt();
int total = 0;
String digit = "" + value;
System.out.print("");
for (int i = 0; i < digit.length(); i++) {
int myInt = Integer.parseInt(digit.substring(i, i + 1));
System.out.println(myInt);
total += myInt;
}
}
}
output:
Please enter a value:
789
7
8
9
How do I reverse the output? For example, when I enter the number 123, it would display 321 with each digit on a new line.
If the user is inputting values in base 10, you could instead use the modulo operator along with integer division to grab the rightmost values successively in a while loop as so:
import java.util.Scanner;
public class Digitsdisplay {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
System.out.println("Please enter a value: ");
int value = input.nextInt();
int quotient = value;
int remainder = 0;
while(quotient != 0){
remainder = quotient%10;
quotient = quotient/10;
System.out.print(remainder);
}
}
}
This might be a better method that attempting to convert the int to a string and then looping through the string character by character.
Loop you printing for loop in the reverse direction:
import java.util.Scanner;
class Digitsdisplay {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
System.out.println("Please enter a value: ");
int value = input.nextInt();
int total = 0;
String digit = "" + value;
System.out.print("");
for (int i = digit.length()-1; i >= 0; i--) {
int myInt = Integer.parseInt(digit.substring(i, i + 1));
System.out.println(myInt);
total += myInt;
}
}
}
Edit: No reason to reverse the string itself, ie. using a Stringbuilder like the other answers say. This just adds to the runtime.
import java.util.*;
public class Francis {
public static void main(String[] args) {
Scanner input= new Scanner (System.in);
System.out.println("Please enter a value: ");
int value = input.nextInt();
int total = 0;
String digit = "" + value;
System.out.print("");
for (int i = 0; i < digit.length(); i++) {
int myInt = Integer.parseInt(digit.substring(i, i + 1));
System.out.println(myInt);
total += myInt;
}
}
}
Simply reverse the string before looping through it
digit = new StringBuilder(digit).reverse().toString();
I do not know how to display entries entered by user, and also to match and sort them.
import java.util.Scanner;
public class Case1
{
public static void main(String[] args) {
Scanner in=new Scanner (System.in);
System.out.println("Input a size of an array: ");
int size = in.nextInt();
int num[]=new int[size];
int i=0;
for (i=0;i<num.length;i++) {
System.out.println("Input a number: ");
num[i]=in.nextInt();
}
for (int c=0;i<num.length;c++){
for (int a=0; a<num.length;a++){
if(num[c]>num[a]){
int temp = num[c];
num[c]= num[a];
num[a]=temp;
}
}
}
for (int d=0;i<num.length;d++){
int value = 0;
if(value==num[i]) {
System.out.println("Match Found!");
}
}
}
}
help please.
It should look like this ->
import java.util.Scanner;
public class Case1
{
public static void main(String[] args) {
Scanner in=new Scanner (System.in);
System.out.println("Input a size of an array: ");
int size = in.nextInt();
int num[]=new int[size];
int i=0;
for (i=0;i<num.length;i++) {
System.out.println("Input a number: ");
num[i]=in.nextInt();
}
// DISPLAY ENTRIES
System.out.println("You entered the following entries.");
for (int index=0; index<num.length; index++) {
System.out.print(index + ": " + num[index] + " ");
}
// END DISPLAY ENTRIES
for (int c=0;c<num.length;c++){ // Changed i to c
for (int a=0; a<num.length;a++){
if(num[c]>num[a]){
int temp = num[c];
num[c]= num[a];
num[a]=temp;
}
}
}
for (int d=0;d<num.length;d++){ // Changed i to d
int value = 0;
if(value==num[d]) { // Changed i to d
System.out.println("Match Found!");
}
}
}
}
This should work, but when you asked in your question about matching, did you want to match each entry with 0 (which makes no sense) or did you want to compare each entry to 0?