Jump to content

Simple java input output

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
blackeagle233

blackeagle233

    Newbie

  • Members
  • Pip
  • 9 posts
I have a problem and I am losing sleep over it. I am using the Scanner class to read input from a user but i am getting a lot of run time errors. Do not worry about the classes I use.
My run time error occurs when my program ask for input on to whether"hit or stay"

import java.util.Scanner;

import java.io.*;


public class MainDisplay {



	public static void main(String[] args) {

		

		Scanner scan=new Scanner(System.in);

		

		//Introductions

		int bet=0;

		String response;

		boolean play=true;

		System.out.println("Hello and Welcome to BlackJack 1.0");

		System.out.println("This was created by Ethan Keiser");

		System.out.println("You start out with $1,000 Good Luck!");

		System.out.println("How much would you like to bet?");

			bet=scan.nextInt();

	// Creates a new player and dealer.

		Game p=new Player();

		Game d=new Dealer();

		//while(play){

			

		// Shuffle and deals cards

		p.showCards();


		d.showCards();

		// response

		System.out.println("\nWould you like to hit or stay? ");

		System.out.println("(type \"h\" for hit and \"s\" for stay.)");

		 response =scan.nextLine();

			

		if(response.equals("h"))

		{

		

		p.ifHit();

	

			if(p.isBusted()){

				

				p.lose(bet);

				System.out.println("Play again ?? y for yes n for no");

				 response =scan.nextLine();

				if(response.equals("y"))

					play=true;

				else if(response =="n")

					play=false;

					}

			else if(!p.isBusted()&&d.isBusted())

			{

				p.win(bet);

				

				 response =scan.nextLine();

				if(response.equals("y"))

					play=true;

				else if(response =="n")

					play=false;

					}

			}

			else

				p.isWinner(d);

			{

				

			}

		}

			

		

		

	}



	

any ideas?


here are the other three classes
public class Dealer extends Game {

	private int dcard1,dcard2,dcard3,total;

	

	public Dealer(){

		dcard1=0;

		dcard2=0;

		dcard3=0;

		

	}


public void shuffleCards() {

	

		dcard1=shuffle();

		dcard2=shuffle();

		dcard3=shuffle();

	}

	public void showCards(){

		shuffleCards();

		total=(dcard1+dcard2);

		System.out.print(toString());

	}

	public void addCard(int a){

		total+=a;

		

	}

	

	int getCard1() {

		

		return dcard1;

	}


	int getCard2() {

		// TODO Auto-generated method stub

		return dcard2;

	}

	int getCard3() {

		// TODO Auto-generated method stub

		return dcard3;

	}

	public int getTotal(){

		return total;

	}

	

	public String toString()

	{

		String str="\nDealers shows : "+dcard1;

		

		return str;

		

	}


	@Override

	void shuffleCard1() {

		dcard1=shuffle();

		

	}


	@Override

	void shuffleCard2() {

		dcard2=shuffle();

	}


	@Override

	void shuffleCard3() {

		dcard3=shuffle();

		

	}


	@Override

	void lose(int m) {

		

		

	}


	@Override

	void win(int m) {

	

		

	}


	

}




public abstract class Game {


	


	abstract void showCards();

	abstract void shuffleCards();

	abstract int getCard1();

	abstract int getCard2();

	abstract int getCard3();

	abstract void shuffleCard1();

	abstract void shuffleCard2();

	abstract void shuffleCard3();

	abstract void addCard(int m);

	abstract int getTotal();

	abstract void lose(int m);

	abstract void win(int m);

	

	public void ifHit()

	{

		shuffleCard3();

		addCard(getCard3());

	}

	

	public int shuffle(){

		int a=(int)((Math.random()*5)+1);

		 return a;

	}

	public boolean isBusted(){

		if (this.getTotal()>21)

			return true;

		else

		return false;

	}

	

	public boolean isWinner(Game d){

		if( this.getTotal()>= d.getTotal());

		return true;

		

	}


	


}



public class Player extends Game{

	private int pcard1,pcard2,pcard3,amount,total;

	

	

	public Player(){

		pcard1=0;

		pcard2=0;

		pcard3=0;

		amount=1000;

	}


public void shuffleCards() {

	

		pcard1=shuffle();

		pcard2=shuffle();

		pcard3=shuffle();


	}

	public void showCards(){

		shuffleCards();

		total=(pcard1+pcard2);

		System.out.print(toString());

	}

	public void shuffleCard1(){

		pcard1=shuffle();

	}

public void shuffleCard2(){

	pcard2=shuffle();

	}

public void shuffleCard3(){

	pcard3=shuffle();

	

	

}

public void addCard(int a){

	total+=a;

	

}

	

	public int getTotal(){

		return total;

	}

	

	

	

	public int getCard1(){

		return pcard1;

		}

	public int getCard2(){

		return pcard2;

	}

	public int getCard3(){

		return pcard3;

	}

	


	public void addAmount(int m){

		amount+=m;

	}

	public void subtractAmount(int m){

		amount-=m;

	}

	

	

	public int getAmount(){

		return amount;

	}

	public void win(int bet){

		System.out.println("Congradulations you won "+bet);

		addAmount(bet);

	}

	public void lose (int bet)

	{

		System.out.println("Sorry you lost "+bet);

		subtractAmount(bet);

	}

	public String toString()

	{

		String str="\nCard 1: "+pcard1+" "+"Card 2: "+pcard2;

		str+=" \nTotal: "+(pcard1+pcard2);

		return str;

		

	}

}
I am sure the error is basic but any idea's would be greatly appreciated. Its not finish of course.

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Could you post the error?

Edited by wim DC, 04 May 2010 - 10:12 PM.
typo


#3
blackeagle233

blackeagle233

    Newbie

  • Members
  • Pip
  • 9 posts

oxano said:

Could you post the error?

It compiles, however it s a run time error. When it ask for the second input, it terminates for some odd reason

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Ahh i see. It's a problem with the scanner reading something from the println (Don't ask me why). Simply add an extra scanner.nextLine() after every println IF you use a nextLine after it, It's not required for nextint.

It actually isn't an error, the scanner just reads in "\n" into response and the program continues, it compares "\n" with 'h' --> false->else side: p.isWinner(d); and the program is finished.

#5
blackeagle233

blackeagle233

    Newbie

  • Members
  • Pip
  • 9 posts

oxano said:

Ahh i see. It's a problem with the scanner reading something from the println (Don't ask me why). Simply add an extra scanner.nextLine() after every println IF you use a nextLine after it, It's not required for nextint.

It actually isn't an error, the scanner just reads in "\n" into response and the program continues, it compares "\n" with 'h' --> false->else side: p.isWinner(d); and the program is finished.

So its kinda not my fault right? Either way Thank you for help me. Usually when high school students write their first program they start out with "hello world" however instead of all that I just read 3 AP computer science books (Java language) and decided to begin writing my first program which this is.:cool:

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts

Quote

So its kinda not my fault right?
No it's not. It's very weird that Java acts like that in my opinion. The System.out.println ends the data it prints with a newline character '\n', and appearantly this character gets caught by the scanner.nextLine... :(

#7
ksemeks

ksemeks

    Learning Programmer

  • Members
  • PipPipPip
  • 57 posts
A better solution for user input would be using BufferedReader.
// d-_-b+