Re: quick java question
- From: "Jeffrey Fidler" <jfiddler2@xxxxxxxxxxx>
- To: <programmingblind@xxxxxxxxxxxxx>
- Date: Thu, 29 Nov 2007 23:30:51 -0500
Just saw this post ... I noticed a couple of things. First, I think you
want to use the && operator instead of the || as you have to meet the
conditions that it is a number above 0, and is also less than pts. You are
now saying something like "if bet is greater than 0 or if bet is less than
pts" so your condition will pass if one of these is true, but you seem to
actually need both to be true. Also, you can do something like this:
double bet;
Scanner sc = new Scanner(System.in);
if (sc.hasNextDouble()) {
// received a double from standard input, continue testing to make sure we
have a valid amount
bet = sc.nextDouble();
if (bet > 0 && bet <= pts) { // I put <= since a bet can equal the total
pts -= bet; // bet should now be subtracted from total pts
}
} else {
System.out.println("You did not enter a number!");
}
// do something with the bet, etc. etc.
I wrote this from my mind directly, so it is not compiled/tested -- i.e. it
cannot be trusted! But, I hope it gives you some ideas.
Good luck!
- Jeff
----- Original Message -----
From: "Alex Parks" <mehgcap@xxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Thursday, November 29, 2007 11:07 PM
Subject: Re: quick java question
Thanks. My var is a double called bet (the assignment was a blackjack
game, and no you are not helping me cheat by helping--we can use whatever
to get it done). I am ensuring bet is valid; that is, above 0, below the
user's current balance, and a number and not a letter. I have:
if (bet<pts || bet>0 || bet.hasNextDouble()
I get an error about bet not being able to be dereferenced.
Have a great day,
Alex
----- Original Message -----
From: "Jeffrey Fidler" <jfiddler2@xxxxxxxxxxx
To: <programmingblind@xxxxxxxxxxxxx
Date sent: Thu, 29 Nov 2007 22:53:27 -0500
Subject: Re: quick java question
Here is an example using an integer. (Sorry, but I had not
remembered that
you were using a double when I began quickly working up this
example, and
I'm off to bed for now.) Remember that if you enter a char type,
it will
still work as it will output the ascii value! You can swap the
int for a
double and it should work. Hope this helps!
import java.util.Scanner;
public class TestInt {
public static void main(String[] args) {
System.out.print("Please enter an integer: ");
Scanner sc = new Scanner(System.in);
if (sc.hasNextInt()) {
System.out.println("Your integer is " + sc.nextInt());
} else {
System.out.println("You did not enter an integer!");
}
}
}
Kind regards,
Jeff
----- Original Message -----
From: "Sina Bahram" <sbahram@xxxxxxxxx
To: <programmingblind@xxxxxxxxxxxxx
Sent: Thursday, November 29, 2007 10:19 PM
Subject: RE: quick java question
If you'd like some help on this tonight, shoot me the file off
list ...
I'm
busy tomorrow, but hopefully Suzanne can help you then, if we
don't get
it
tonight?
sbahram@xxxxxxxxx
Take care,
Sina
-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Alex
Parks
Sent: Thursday, November 29, 2007 9:55 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: RE: quick java question
Okay, thanks! I will attach the .java file to an email and send
it to you
privately. It will save my poor cs professor from answering all
my
questions and give me a new perspective. Thanks again.
Have a great day,
Alex
----- Original Message -----
From: "Suzanne Balik" <spbalik@xxxxxxxxxxxxxx
To: programmingblind@xxxxxxxxxxxxx
Date sent: Thu, 29 Nov 2007 21:30:35 -0500 (EST)
Subject: RE: quick java question
You typically use double's rather than float's in Java. How
about this --
would this work for you?
Scanner sc = new Scanner(System.in);
if (sc.hasNextDouble())
double value = sc.nextDouble();
I'll be in my office some time after 3:00 PM tomorrow (Friday).
If you
want to send me your code, I can help you with it then.
Suzanne Balik
I tried it, but have changed the var to a float after I sent the
message. I tried var.hasNextFloat() but I get an error on that
line
that says something like "float cannot be dereferenced". I am
checking to be sure a number (the var in question) is greater
than 1
but less than another var and so thought I could just put this
simple
statement as another condition in the if statement, but I got
the
above error. I am using the latest Java.
Have a great day,
Alex
----- Original Message -----
From: "Suzanne Balik" <spbalik@xxxxxxxxxxxxxx
To: programmingblind@xxxxxxxxxxxxx
Date sent: Thu, 29 Nov 2007 21:00:11 -0500 (EST)
Subject: RE: quick java question
You can use the Scanner class to do what you want to do easily,
if you're
using Java 5.0. For example,
Scanner sc = new Scanner(System.in);
if (sc.hasNextInt())
int value = sc.nextInt();
Suzanne Balik
NC State University EB II 2318 (919)515-5617
We'll take a cup o' kindness yet, for auld lang syne.
--Robert Burns
Do a search for
Try catch tutorial
Try statements and catch clauses are the way to go if you are
going to
seriously work with java.
-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Alex
Parks
Sent: Thursday, November 29, 2007 7:55 AM
To: programmingblind@xxxxxxxxxxxxx
Subject: RE: quick java question
I am new to this language. What do I put in place of "aString"
and how do I use the exception in my if statement? Add a
throwsException to the end? Thanks.
Have a great day,
Alex
----- Original Message -----
From: "Sina Bahram" <sbahram@xxxxxxxxx
To: <programmingblind@xxxxxxxxxxxxx Date sent: Thu, 29 Nov 2007
07:07:27 -0500
Subject: RE: quick java question
If you're expecting an integer, then you can do
Integer.parseInt(aString)
That will throw a NumberFormatException if it isn't a number.
Take care,
Sina
-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Alex
Parks
Sent: Thursday, November 29, 2007 12:51 AM
To: programmingblind@xxxxxxxxxxxxx
Subject: quick java question
Hi all,
Is there a quick way of determining if the user input is a
number? I need it
to be a number; it throws an exception and exits the program if
it isn't.
Maybe something like:
if (input!=\int
I have no idea about the syntax, but something along those lines
is what I
am looking for. Thanks for any help.
Have a great day,
Alex
__________
View the list's information and change your settings at
http://www.freelists.org/list/programmingblind
__________
View the list's information and change your settings at
http://www.freelists.org/list/programmingblind
__________
View the list's information and change your settings at
http://www.freelists.org/list/programmingblind
__________
View the list's information and change your settings at
http://www.freelists.org/list/programmingblind
__________
View the list's information and change your settings at
http://www.freelists.org/list/programmingblind
__________
View the list's information and change your settings at
http://www.freelists.org/list/programmingblind
__________
View the list's information and change your settings at
http://www.freelists.org/list/programmingblind
__________
View the list's information and change your settings at
http://www.freelists.org/list/programmingblind
__________
View the list's information and change your settings at
http://www.freelists.org/list/programmingblind
__________
View the list's information and change your settings at
http://www.freelists.org/list/programmingblind
__________
View the list's information and change your settings at
http://www.freelists.org/list/programmingblind
__________
View the list's information and change your settings at
http://www.freelists.org/list/programmingblind
- References:
- Re: quick java question
- From: Alex Parks
Other related posts:
- » quick java question
- » RE: quick java question
- » RE: quick java question
- » RE: quick java question
- » RE: quick java question
- » RE: quick java question
- » RE: quick java question
- » RE: quick java question
- » RE: quick java question
- » Re: quick java question
- » RE: quick java question
- » Re: quick java question
- » Re: quick java question
- » Re: quick java question
- » Re: quick java question
- » Re: quick java question
- » Re: quick java question
- » RE: quick java question
if (bet<pts || bet>0 || bet.hasNextDouble() I get an error about bet not being able to be dereferenced. Have a great day, Alex
----- Original Message ----- From: "Jeffrey Fidler" <jfiddler2@xxxxxxxxxxx To: <programmingblind@xxxxxxxxxxxxx Date sent: Thu, 29 Nov 2007 22:53:27 -0500 Subject: Re: quick java question
Here is an example using an integer. (Sorry, but I had not
remembered that
you were using a double when I began quickly working up this
example, and
I'm off to bed for now.) Remember that if you enter a char type,
it will
still work as it will output the ascii value! You can swap the
int for a
double and it should work. Hope this helps!
import java.util.Scanner;
public class TestInt {
public static void main(String[] args) {
System.out.print("Please enter an integer: ");
Scanner sc = new Scanner(System.in);
if (sc.hasNextInt()) {
System.out.println("Your integer is " + sc.nextInt());
} else {
System.out.println("You did not enter an integer!");
}
}
}
Kind regards,
Jeff
----- Original Message ----- From: "Sina Bahram" <sbahram@xxxxxxxxx To: <programmingblind@xxxxxxxxxxxxx Sent: Thursday, November 29, 2007 10:19 PM Subject: RE: quick java question
If you'd like some help on this tonight, shoot me the file off
list ...
I'm busy tomorrow, but hopefully Suzanne can help you then, if we
don't get
it tonight?
sbahram@xxxxxxxxx
Take care, Sina
-----Original Message----- From: programmingblind-bounce@xxxxxxxxxxxxx [mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Alex
Parks
Sent: Thursday, November 29, 2007 9:55 PM To: programmingblind@xxxxxxxxxxxxx Subject: RE: quick java question
Okay, thanks! I will attach the .java file to an email and send
it to you
privately. It will save my poor cs professor from answering all
my
questions and give me a new perspective. Thanks again.
Have a great day, Alex
----- Original Message ----- From: "Suzanne Balik" <spbalik@xxxxxxxxxxxxxx To: programmingblind@xxxxxxxxxxxxx Date sent: Thu, 29 Nov 2007 21:30:35 -0500 (EST) Subject: RE: quick java question
You typically use double's rather than float's in Java. Howabout this --would this work for you?
Scanner sc = new Scanner(System.in); if (sc.hasNextDouble()) double value = sc.nextDouble();
I'll be in my office some time after 3:00 PM tomorrow (Friday).If youwant to send me your code, I can help you with it then.
Suzanne Balik
I tried it, but have changed the var to a float after I sent the message. I tried var.hasNextFloat() but I get an error on that
line
that says something like "float cannot be dereferenced". I am checking to be sure a number (the var in question) is greater
than 1
but less than another var and so thought I could just put this
simple
statement as another condition in the if statement, but I got
the
above error. I am using the latest Java.
Have a great day, Alex
----- Original Message ----- From: "Suzanne Balik" <spbalik@xxxxxxxxxxxxxx To: programmingblind@xxxxxxxxxxxxx Date sent: Thu, 29 Nov 2007 21:00:11 -0500 (EST) Subject: RE: quick java question
You can use the Scanner class to do what you want to do easily,if you'reusing Java 5.0. For example,
Scanner sc = new Scanner(System.in); if (sc.hasNextInt()) int value = sc.nextInt();
Suzanne Balik
NC State University EB II 2318 (919)515-5617
We'll take a cup o' kindness yet, for auld lang syne. --Robert Burns
Do a search for Try catch tutorial Try statements and catch clauses are the way to go if you aregoing toseriously work with java.
-----Original Message----- From: programmingblind-bounce@xxxxxxxxxxxxx [mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of AlexParksSent: Thursday, November 29, 2007 7:55 AM To: programmingblind@xxxxxxxxxxxxx Subject: RE: quick java question
I am new to this language. What do I put in place of "aString" and how do I use the exception in my if statement? Add a throwsException to the end? Thanks.
Have a great day, Alex
----- Original Message ----- From: "Sina Bahram" <sbahram@xxxxxxxxx To: <programmingblind@xxxxxxxxxxxxx Date sent: Thu, 29 Nov 2007 07:07:27 -0500 Subject: RE: quick java question
If you're expecting an integer, then you can do
Integer.parseInt(aString)
That will throw a NumberFormatException if it isn't a number.
Take care, Sina
-----Original Message----- From: programmingblind-bounce@xxxxxxxxxxxxx [mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of AlexParksSent: Thursday, November 29, 2007 12:51 AM To: programmingblind@xxxxxxxxxxxxx Subject: quick java question
Hi all, Is there a quick way of determining if the user input is anumber? I need itto be a number; it throws an exception and exits the program ifit isn't.Maybe something like: if (input!=\int I have no idea about the syntax, but something along those linesis what Iam looking for. Thanks for any help.
Have a great day, Alex __________ View the list's information and change your settings at http://www.freelists.org/list/programmingblind
__________ View the list's information and change your settings at http://www.freelists.org/list/programmingblind
__________ View the list's information and change your settings at http://www.freelists.org/list/programmingblind
__________ View the list's information and change your settings at http://www.freelists.org/list/programmingblind
__________ View the list's information and change your settings at http://www.freelists.org/list/programmingblind
__________ View the list's information and change your settings at http://www.freelists.org/list/programmingblind
__________ View the list's information and change your settings at http://www.freelists.org/list/programmingblind
__________ View the list's information and change your settings at http://www.freelists.org/list/programmingblind
__________ View the list's information and change your settings at http://www.freelists.org/list/programmingblind
__________ View the list's information and change your settings at http://www.freelists.org/list/programmingblind
__________View the list's information and change your settings at http://www.freelists.org/list/programmingblind
- Re: quick java question
- From: Alex Parks