The problem occurs in the seventh section. It will not allow me to read in a state and instead prints the else statement.
import java.util.Scanner;
public class ifElseStatements
{
public static Scanner in = new Scanner (System.in);
public static void main (String[] args)
{
one();
two();
three();
four();
five();
six();
seven();
eight();
}
public static void one()
{
System.out.print("grade: ");
int number = in.nextInt();
if(number > 70 && number <=100)
{
System.out.println("You're passing");
}
else
{
System.out.println("you're not passing!");
}
}
public static void two()
{
System.out.print("Number please");
int b = in.nextInt();
if(b <=50)
{
System.out.println("Go");
}
else
{
System.out.println("STOP");
}
}
public static void three()
{
System.out.print("Integer please >");
int c = in.nextInt();
if(c%2 == 0)
{
System.out.println("Even");
}
else
{
System.out.println("odd");
}
}
public static void four()
{
System.out.print("Integer please");
int d = in.nextInt();
if(d%5 == 0)
{
System.out.println("Multiple of 5");
}
else
{
System.out.println("Not a multiple of 5");
}
}
public static void five()
{
System.out.print("number please");
int e = in.nextInt();
if(e< 10)
{
System.out.println("one digit");
}
else if(e>= 10 && e<100)
{
System.out.println("two digits");
}
else
{
System.out.println("three digits");
}
}
public static void six()
{
System.out.print("Jersey Number");
int f = in.nextInt();
if(f == 12 || f == 80 || f == 71)
{
System.out.println("That number is retired from the seattle seahawks");
}
else
{
System.out.println("");
}
}
public static void seven()
{
System.out.print("a state is");
String g = in.nextLine();
if(g .equals ("Washington") || g .equals ("Oregon") || g .equals ("Idaho"))
{
System.out.println("That state is in the PNW!");
}
else
{
System.out.println("You should move to the PNW; Its great here!");
}
}
public static void eight()
{
System.out.print("drink size (SHORT, TALL, GRANDE, VENTI)");
String h = in.nextLine();
if(h .equals ("SHORT"))
{
System.out.println("8");
}
else if(h .equals ("TALL"))
{
System.out.println("12");
}
else if(h .equals ("GRANDE"))
{
System.out.println("16");
}
else if(h .equals ("VENTI"))
{
System.out.println("20");
}
}
}
This is what it looks like when I run the code.
grade: 70
you're not passing!
Number please12
Go
Integer please >30
Even
Integer please15
Multiple of 5
number please33
two digits
Jersey Number12
That number is retired from the seattle seahawks
a state isYou should move to the PNW; Its great here!
drink size (SHORT, TALL, GRANDE, VENTI)TALL
12 oz
The previous step just does in.nextInt(), which reads the next int, but doesn't consume the EOL character. So, the step7 method reads the next line, and consumes the EOL that hasn't been consumed by the previous step.
You should add in.nextLine() in step6 to consume the EOL character, and not just the integer.
This is a common issue resulting from you using both Scanner.nextInt() and Scanner.nextLine(), which doesn't always work as you would want it to. You can find an explanation for this by going through JavaDoc to get an idea how Scanners work, but for a simple fix just replace instances of
in.nextInt();
with
Integer.parseInt(in.nextLine());
Related
I've been trying practiceIt problems and expand them a little - and I'm stuck.
The code will give results in as many stars as it's necessary, but I do not know how to make user decide the value for n.
I've tried adding to both methods (main/starString) those code lines:
"Scanner input = new.Scanner(System.in);
int n = input.next();" [also input.nextInt]
but the code will note allow any input from console. Not to mention I've got no idea where shoud I add second println command to actually print result from this code...
help me please
import java.util.*;
public class printStars {
public static void main(String[]args) {
System.out.println("choose number and I wil show you 2^number stars");
}
public static String starString(int n) {
if (n < 0) {
throw new IllegalArgumentException();
} else if (n == 0) {
return "*";
} else {
return starString(n - 1) + starString(n - 1);
}
}
}
Do it as follows:
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Choose number and I wil show you 2^number stars: ");
System.out.println(starString(in.nextInt()));
}
public static String starString(int n) {
if (n < 0) {
throw new IllegalArgumentException();
} else if (n == 0) {
return "*";
} else {
return starString(n - 1) + starString(n - 1);
}
}
}
A sample run:
Choose number and I wil show you 2^number stars: 5
********************************
You should also be checking the input before you enter the method. Here is one approach that allows for improper input and reprompts the user to enter the correct value. This way, no exceptions need be caught.
Scanner in = new Scanner(System.in);
String prompt = "Choose number (or a char to end) \nand I will show you 2^number stars: ";
System.out.print(prompt);
while (in.hasNextInt()) {
int n = in.nextInt();
if (n > 0) {
System.out.println(starString(n));
} else {
System.out.print("Input must be greater than 0, try again: ");
continue;
}
System.out.print(prompt);
}
System.out.println("Bye!");
public static String starString(int n) {
if (n == 0) {
return "*";
} else {
return starString(n - 1) + starString(n - 1);
}
}
I am trying to do a basic Java Airline Reservation App. Here is my code, It seems that after I press '1' it is terminating and not running the rest of the code. I am not sure if it is something wrong with my loop or why it is terminating. Please if anyone has any ideas or answers I would love to hear them! thanks
import java.util.Scanner;
public class Reservation {
boolean[] seat = new boolean[11];
Scanner input = new Scanner(System.in);
public void start() {
while (true) {
makeReservation();
}
}
public void makeReservation() {
System.out.println("Please type 1 for first class and type 2 for economy");
int section = input.nextInt();
if (section == 1) {
firstClassSeat();
} else {
economySeat();
}
}
public void firstClassSeat() {
for (int count = 1; count <= 5; count++) {
if (seat[count] = false) {
seat[count] = true;
System.out.printf("First Class. Seat# %d\n", count);
break;
} else if (seat[10] == true) {
if (seat[5] == true) {
} else {
System.out.println("First class is fully booked, would you like an econmy seat");
int choice = input.nextInt();
if (choice == 1) {
firstClassSeat();
start();
} else {
System.out.println("Next flight is in 3 hours");
System.exit(0);
}
}
}
}
}
public void economySeat() {
for (int count = 6; count <= 10; count++) {
if (seat[count] = false) {
seat[count] = true;
System.out.printf("First Class. Seat# %d\n", count);
break;
} else if (seat[10] == true) {
if (seat[5] == true) {
} else {
System.out.println("Economy is fully booked. Would you like First Class? 1 for Yes 2 for No");
int choice = input.nextInt();
if (choice == 1) {
firstClassSeat();
start();
} else {
System.out.println("Next flight is in 3 hours");
System.exit(0);
}
}
}
}
}
}
First off, there is no need to compare booleans inside an if. seats[i] == false is the same as !seats[i].
Nothing is happening because you have an assignment:if(seat[count]=false instead of comparison:if(seat[count]==false. That's the first error, after fixing that you will start assigning seats.
Next, you're calling firstClassSeat inside the same method, when I think you wanted to call economySeat (when first class is full). You need to fix the logic behind the checks and economy/first class suggestions.
When I click retry on this code, it work's and asks how many times to loop flip a coin but the just prints "Flipping Coin(s)" and does nothing. Anyone know how to fix it? I think the error might be coming from X already being less than numloop but I am not sure how to fix it.
Here is my code:
import java.util.Scanner;
public class coinFlip {
public static void main (String[]args)throws InterruptedException {
Scanner sc = new Scanner(System.in);
Scanner scan = new Scanner(System.in);
int numloop;
int x = 0;
String choice;
Boolean bool = true;
while (bool=true){
System.out.println("How Many Coins Would You Like To Flip?");
numloop = sc.nextInt();
if (numloop == 13 || (numloop == 5 || (numloop == 8 || (numloop == 666)))) {
System.out.println("ILLUMINATI CONFIRMED ??????");
System.out.println();
}
System.out.println("Flipping Coin(s)...");
System.out.println();
while (x<numloop) {
int rng = (int)(Math.random()*10+1);
if (rng <= 5) {
System.out.println("You Flipped Heads");
}
else {
System.out.println("You Flipped Tails");
}
x=x+1;
}
System.out.println();
System.out.println("Would You Like To 'Quit' Or 'Retry'?");
choice = scan.nextLine();
if (choice.equalsIgnoreCase("Quit")) {
System.out.println ("Have A Nice Day");
Thread.sleep(1000);
System.exit(0);
}
if (choice.equalsIgnoreCase("Retry")) {
bool=true;
}
}
}
}
Thank You So Much!
If you move int x=0 from outside of your initial while loop to inside of it you won't have this issue. It will reset every time the user retries.
Scanner sc = new Scanner(System.in);
Scanner scan = new Scanner(System.in);
int numloop;
String choice;
Boolean bool = true;
while (bool=true){
int x = 0;
System.out.println("How Many Coins Would You Like To Flip?");
numloop = sc.nextInt();
if (numloop == 13 || (numloop == 5 || (numloop == 8 || (numloop == 666)))) {
System.out.println("ILLUMINATI CONFIRMED ??????");
System.out.println();
}
System.out.println("Flipping Coin(s)...");
System.out.println();
while (x<numloop) {
int rng = (int)(Math.random()*10+1);
if (rng <= 5) {
System.out.println("You Flipped Heads");
}
else {
System.out.println("You Flipped Tails");
}
x=x+1;
}
System.out.println();
System.out.println("Would You Like To 'Quit' Or 'Retry'?");
choice = scan.nextLine();
if (choice.equalsIgnoreCase("Quit")) {
System.out.println ("Have A Nice Day");
Thread.sleep(1000);
System.exit(0);
}
if (choice.equalsIgnoreCase("Retry")) {
bool=true;
}
}
}
Hi it's my first post here and I have a problem:
I have a program in which, at some point, asks the user if he wants to share the stars, and if he supposedly does, the program goes back to collecting them and after some time comes back to the question if he wants to share them again.
The problem is that, when the user comes back to that point, the program ignores whatever answer u give to it and goes to the "else answer block".
It looks like this:
"Do you want to share your stars?
yes
Please answer with yes or no"
Code:
import java.util.Random;
import java.util.Scanner;
public class App {
static Scanner skan = new Scanner(System.in);
static int starCount = 0;
static Random random = new Random();
public static void main(String[] args) {
starReaching();
}
static void starReaching() {
boolean starCollected = false;
int i = 0;
int j = random.nextInt(101);
while (i < j) {
i++;
System.out.println("Stars are out of reach");
}
if (i > j || i == j) {
starCollected = true;
}
if (starCollected == true) {
starCollector();
}
}
static void starCollector() {
System.out.println("You caught a star !");
starCount++;
if (starCount == 10) {
System.out.println("You have 10 stars ! :)");
System.out.println("Do you want to share your stars?");
String line = skan.nextLine();
if (line.equals("yes")) {
skan.reset();
starGiver();
} else if (line.equals("no")) {
wishMaker();
} else {
System.out.println("Please answer with yes or no");
System.exit(0);
}
} else {
starReaching();
}
}
static void starGiver() {
System.out.println("How many do you want to share?");
int starsToShare = skan.nextInt();
if (starsToShare < 10 || starsToShare == 10 && starsToShare > 0) {
starCount = starCount - starsToShare;
System.out.println("Stars shared !");
System.out.println("You now have " + starCount + " stars");
System.out.println("Go collect them again!");
starReaching();
} else if (starsToShare > 10) {
System.out.println("You don't have enough stars to share that much!");
starGiver();
} else {
System.out.println("That's not a valid option");
starGiver();
}
}
static void wishMaker() {
}
}
Every time you call skan.nextLine() you read a new line and advance the scanner's pointer, and this is the reason your code is failing: you're calling this too often.
Instead of
if (skan.nextLine().equals("yes")) {
skan.reset();
starGiver();
} else if (skan.nextLine().equals("no")) {
wishMaker();
} else {
do:
String line = scan.nextLine(); // read from Scanner **once** only
if (line.equals("yes")) {
skan.reset();
starGiver();
} else if (line.equals("no")) {
wishMaker();
} else {
Okay i've found the little bugger, i was using skan.nextInt without using skan.nextLine after that, thank you for the quick help, much love
I already wrote a code that convert from decimal number to roman number, however, i want it to do opposite way so how can i make it possible? (This is my previous code: http://pastebin.com/QFKi0xJh ) thank you! and here is my code right now.
I am just a beginner so my code is look little bit basic, please apology for that.
public static void main(String[] args) {
// Fill in the body
Scanner in= new Scanner(System.in);
String user = promptUserForNumeral(in);
while (user.length()!=0) {
int numb= convertNumeralToNumber(user);
System.out.println("The numeral "+user+ " is the decimal number "+numb);
user = promptUserForNumeral(in);
}
}
private static String promptUserForNumeral(Scanner inScanner) {
// Fill in the body
System.out.println("Enter a roman numeral (Q to quit): ");
String i = inScanner.nextLine();
while (i.length()>=0) {
if (i.length()==0) {
System.out.println("ERROR! You must enter a non-empty line!");
System.out.println("Enter a roman numeral (Q to quit): ");
i = inScanner.nextLine();
}
else if ( i.length()==1 && i.charAt(0)=='q' || i.charAt(0)=='Q') {
System.out.println("Goodbye!");
System.exit(0);
}
}
return i;
}
private static int convertNumeralToNumber(String numeral) {
// Fill in the body
int n=0;
if (numeral.equalsIgnoreCase("m")) {
n-=1000;
}
else if (numeral.equalsIgnoreCase("d")) {
n=500;
}
else if (numeral.equalsIgnoreCase("c")) {
n=100;
}
else if (numeral.equalsIgnoreCase("l")) {
n=50;
}
else if (numeral.equalsIgnoreCase("x")) {
n=10;
}
else if (numeral.equalsIgnoreCase("v")) {
n=5;
}
else if (numeral.equalsIgnoreCase("i")) {
n=1;
}
return n;
}