Help with Java

Status
Not open for further replies.

iFargle

Linux / HPC / K8s SME
Staff member
Messages
4,403
Location
N/A
Been messing with Java for the last couple of hours...

Here's what I have:
Code:
import java.util.Scanner;
import static java.lang.System.in;
import static java.lang.System.out;

public class TicketPriceWithDiscountBooleanOperators {
	public static void main(String args[])	{
		Scanner Keyboard = new Scanner(in);
		int age; double price = 0.00; char reply;
		boolean isKid, isSenior, hasCoupon, hasNoCoupon;
		
                out.print("Have a coupon? (Y/N) "); 
                reply = Keyboard.findInLine(".").charAt(0);
                
		out.print("How old are you? "); age = Keyboard.nextInt();
		
		
		isKid = age < 12; isSenior = age >= 65;
		hasCoupon = reply == 'Y' || reply == 'y';
		hasNoCoupon = reply == 'N' || reply == 'n';
		
		if (!isKid && !isSenior)	{
			price = 9.25;
		}
		if (isKid || isSenior)	{
			price = 5.25;
		}
		
		if (hasCoupon)	{
			price -= 2.00;
		}
		if (!hasCoupon && !hasNoCoupon)	{
			out.println("Huh?");
		}
		
		out.println("Please pay $" + price + ".  Enjoy the show!");
			
	}
}
which outputs correctly like this:
Code:
run:
Have a coupon? (Y/N) Y
How old are you? 13
Please pay $7.25.  Enjoy the show!
BUILD SUCCESSFUL (total time: 4 seconds)

If I reverse the order of these lines

Code:
out.print("Have a coupon? (Y/N) "); 
reply = Keyboard.findInLine(".").charAt(0);

Code:
out.print("How old are you? "); age = Keyboard.nextInt();

I get this error:
Code:
How old are you? 13
Have a coupon? (Y/N) Exception in thread "main" java.lang.NullPointerException
	at TicketPriceWithDiscountBooleanOperators.main(TicketPriceWithDiscountBooleanOperators.java:13)

telling me this line is in correct:

Code:
reply = Keyboard.findInLine(".").charAt(0);

And I can't figure out why this is happening... If it means anything, it happens in both Eclipse and in NetBeans both on Windows and Elementary OS (Ubuntu spinoff, Eclipse only)

Any help would be much appreciated :thumbsup:
 
Bump / Update / New Need for Help :tongue:

I'm having troubles with this too:
Code:
import java.util.Random;
import java.util.Scanner;
import static java.lang.System.out;
import static java.lang.System.in;

public class PlayTwentyOne {
	public static void main(String[] args)	{
		Random CardRand = new Random();
		Scanner Keyboard = new Scanner(in);
		char start;
		int card = 0, total = 0, hit = 1;
	
		out.println("Would you like to play a game?");
		out.print("Y/N: "); start = Keyboard.findInLine(".").charAt(0);
		
		if (start == 'Y' || start == 'y')  {
			while (total < 21 && hit == 1)	{
				out.println("Your current card total is " + total + ".");
				out.println("Do you want a hit?");
				out.print("1 for Yes / 0 for No: "); hit = Keyboard.nextInt();
					if (hit == 1 || hit == 1)	{
						card = CardRand.nextInt(10)+1;
						total += card;
					} else { 
						out.println("Your total is " + total + ".  Thank you for playing!");
					}
				}
			} if (total > 21) {
				out.println("You went over! So sorry. Your total was " + total + ".");
			} if (total == 21)	{
				out.println("You have 21! You win... nothing.  HA!");
			} else {
				out.println("Good bye.");
		}
	}
}

This code works properly. However, I would love to be able to use
Code:
Keyboard.findInLine(".").charAt(0);
or
Code:
Keyboard.nextLine();
which would be best (So I could type "Yes' or "No" instead of "Y" or "N" or "1" or "0".)

When I use the findInLine(".").charAt(0); it won't work unless it's the first keyboard entry. I don't even know how to get the nextLine(); one to work. I know it has to be a String type, but it still won't work properly no matter what I've tried.
 
keyboard.nextLine(); should work fine?

String blahblah = keyboard.nextLine();

Also, if you do that you could use string.match

e.g. (and apologies for the code :p haven't done Java in a while. Also, changed your "huh" check for the coupon into something more useful lol. Same thing needs to be done for the age thing, or at least put some try/catch blocks in)
edit: just had a thought too, is this for school? Coz you know the rules. I'll trust your reply lol, you've been here long enough.

Code:
                Scanner Keyboard = new Scanner(in);
		int age, chk; double price = 0.00; String reply;
		boolean isKid, isSenior;

                for ( ; ; ){
                out.print("Have a coupon? "); 
                reply = Keyboard.nextLine();
                 if (reply.matches("[Yy][Ee][sSpP]|[Nn][Oo]|[Yy]|[Nn]")){
                     chk=1;
                     break;
                 }
                 else{
                     out.println("Please enter Yes or No"); 
                 }
                }
		out.print("How old are you? "); age = Keyboard.nextInt();
                
		
		isKid = age < 12; isSenior = age >= 65;
		
		if (!isKid && !isSenior)	{
			price = 9.25;
		}
		if (isKid || isSenior)	{
			price = 5.25;
		}
		
                if (chk==1){
                    price-= 2.00;
                }
		out.println("Please pay $" + price + ".  Enjoy the show!");
 
Been messing with Java for the last couple of hours...

Here's what I have:
Code:
import java.util.Scanner;
import static java.lang.System.in;
import static java.lang.System.out;

public class TicketPriceWithDiscountBooleanOperators {
	public static void main(String args[])	{
		Scanner Keyboard = new Scanner(in);
		int age; double price = 0.00; char reply;
		boolean isKid, isSenior, hasCoupon, hasNoCoupon;
		
                out.print("Have a coupon? (Y/N) "); 
                reply = Keyboard.findInLine(".").charAt(0);
                
		out.print("How old are you? "); age = Keyboard.nextInt();
		
		
		isKid = age < 12; isSenior = age >= 65;
		hasCoupon = reply == 'Y' || reply == 'y';
		hasNoCoupon = reply == 'N' || reply == 'n';
		
		if (!isKid && !isSenior)	{
			price = 9.25;
		}
		if (isKid || isSenior)	{
			price = 5.25;
		}
		
		if (hasCoupon)	{
			price -= 2.00;
		}
		if (!hasCoupon && !hasNoCoupon)	{
			out.println("Huh?");
		}
		
		out.println("Please pay $" + price + ".  Enjoy the show!");
			
	}
}
which outputs correctly like this:
Code:
run:
Have a coupon? (Y/N) Y
How old are you? 13
Please pay $7.25.  Enjoy the show!
BUILD SUCCESSFUL (total time: 4 seconds)

If I reverse the order of these lines

Code:
out.print("Have a coupon? (Y/N) "); 
reply = Keyboard.findInLine(".").charAt(0);

Code:
out.print("How old are you? "); age = Keyboard.nextInt();

I get this error:
Code:
How old are you? 13
Have a coupon? (Y/N) Exception in thread "main" java.lang.NullPointerException
	at TicketPriceWithDiscountBooleanOperators.main(TicketPriceWithDiscountBooleanOperators.java:13)

telling me this line is in correct:

Code:
reply = Keyboard.findInLine(".").charAt(0);

And I can't figure out why this is happening... If it means anything, it happens in both Eclipse and in NetBeans both on Windows and Elementary OS (Ubuntu spinoff, Eclipse only)

Any help would be much appreciated :thumbsup:

You can reproduce the NPE with the unmodified code if you respond to the first question by pressing enter. You could just replace findInLine(".") with next().
 
Nah Its not for school. Summer time and I bought a book "Java for Dummies" :tongue:
Keyboard.nextLine(); doesn't work (the ways I've tried it... :confused:

I'll give the rest of these recommendations a try when I'm back on a computer. Thanks :thumbsup:
 
Oh, and for the error where if you swap the lines around, you need to clear first by putting in something (like a newLine) after the Keyboard.nextInt line.

e.g.

out.print("How old are you? "); age = Keyboard.nextInt();
Keyboard.nextLine();
out.print("Have a coupon? (Y/N) ");
reply = Keyboard.findInLine(".").charAt(0);


Or just use next() :p
 
I tried the Keyboard.nextLine(); in the ticket price one.. Works like a charm, thank you :thumbsup:

However, when I put it in the card game one it doesn't work. It stops and waits for user input. I'm in the process of deciphering the other suggestions, so.. gimme a few :tongue:

Update: Oh this is wonderful! it works :thumbsup:

Code:
import java.util.Random;
import java.util.Scanner;
import static java.lang.System.out;
import static java.lang.System.in;

public class PlayTwentyOne {
	public static void main(String[] args)	{
		Random CardRand = new Random();
		Scanner Keyboard = new Scanner(in);
		String start, hit = "Yes";
		int card = 0, total = 0;
	
		out.println("Would you like to play a game?");
		out.print("Yes/No:  "); start = Keyboard.nextLine();
		
		if (start.[B]matches [/B]("Yes"))	{
			while (total < 21 && (hit.[B]matches [/B]("Yes")))	{
				out.println("Your current card total is " + total + ".");
				out.println("Do you want a hit?");
				out.print("Yes/No: "); hit = Keyboard.nextLine();
					if (hit.[B]matches [/B]("Yes"))	{
						card = CardRand.nextInt(10)+1;
						total += card;
					} if (hit.[B]matches[/B]("No"))	{
						out.println("Your total is " + total + ".  Thank you for playing!");
					} if (!hit.[B]matches [/B]("Yes") && !hit.[B]matches[/B]("yes") && !hit.[B]matches[/B]("no") && !hit.[B]matches[/B]("No")) { 
						out.println("Invalid Input.");
					}
				}
			} if (total > 21) {
				out.println("You went over! So sorry. Your total was " + total + ".");
			} if (total == 21)	{
				out.println("You have 21! You win... nothing.  HA!");
				out.println("For reference.... You have " + total);
			} if (start.[B]matches [/B]("No") || start.[B]matches[/B]("no")) {
				out.println();
				out.println();
				out.println("Oh, well that's too bad.  Good bye.");
			} else {
				out.println();
				out.println();
				out.println("Good bye.");
			
		}
	}
}
outputs to...
Code:
Would you like to play a game?
Yes/No:  Yes
Your current card total is 0.
Do you want a hit?
Yes/No: Yes
Your current card total is 8.
Do you want a hit?
Yes/No: Yes
Your current card total is 12.
Do you want a hit?
Yes/No: No
Your total is 12.  Thank you for playing!


Good bye.
 
I'll take a look now. Bear in mind the last time I did Java was last year, decided to teach myself for two weeks lol then got distracted by Python again :p so my coding practices are probably gonna be things you should NOT do haha

Lol you beat me to it, nice work :thumbsup:
Just a note. This

if (start.matches ("No") || start.matches("no"))

is the same as this

if (start.matches ("[Nn]o")
 
Made a dealer routine lol
Code:
public class PlayTwentyOne {
	public static void main(String[] args)	{
		Random CardRand = new Random();
		Scanner Keyboard = new Scanner(in);
		String start, hit;
		int card = 0, total = 0, chk = 1, totalSys = 0;
	
		out.println("Would you like to play a game?");
		out.print("Y/N: "); start=Keyboard.nextLine();
                
                if (start.matches("[Yy][Ee][sSpP]|[Yy]|[Yy][Ee][Aa][Hh]")){
			while (total < 21 && chk == 1)	{
				out.println("Your current card total is " + total + ".");
				out.print("Do you want a hit? "); hit = Keyboard.nextLine();
					if (hit.matches("[Yy][Ee][sSpP]|[Yy]|[Yy][Ee][Aa][Hh]")){
						card = CardRand.nextInt(10)+1;
						total += card;
					} else { 
						out.println("Your total is " + total);
                                                chk=0;
					}
				}
			} if (total > 21) {
				out.println("You went over! So sorry. Your total was " + total + ".");
			} else if (total == 21)	{
				out.println("You have 21! You win... nothing.  HA!");
			} else {
                                while (totalSys <=16){
				card = 0;
                                card = CardRand.nextInt(10)+1;
                                totalSys += card;
                                }
                                if (total > totalSys){
                                    out.println("Dealer total is " + totalSys);
                                    out.print("You WIN!");
                                }
                                else {
                                    out.println("Dealer total is " + totalSys);
                                    out.print("Sorry, you lose!");
                                }
		}
	}

Outputs to this

Code:
run:
Would you like to play a game?
Y/N: YEP
Your current card total is 0.
Do you want a hit? yes
Your current card total is 4.
Do you want a hit? YeAh
Your current card total is 10.
Do you want a hit? Y
Your current card total is 16.
Do you want a hit? yes
Your current card total is 18.
Do you want a hit? no
Your total is 18
Dealer total is 20
Sorry, you lose!BUILD SUCCESSFUL (total time: 17 seconds)
 
I was just about to start something like that :tongue:
Thanks for the help :thumbsup:
 
Status
Not open for further replies.
Back
Top Bottom