Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-26-2008, 07:18 PM
Member
 
Join Date: Dec 2007
Posts: 13
cew27 is on a distinguished road
how to use Scanner with a number
right im just trying to make simple text program here and have a few questions
how can i make it so the below statements refers to a number rather than a string?
Scanner myScanner = new Scanner(in);
String choice = myScanner.next();
a few more n00b questions
1. is there anyway to make it so that i can if an if statement that has 2 acceptable options or more e.g. if string = "hello" or "hello " or " hello"
just to help with human error
2. what is the difference between == and .equals?
3. what hold the largest amount of numbers double or long (or any other that i don't know about)
your help is much appreciated
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-26-2008, 07:59 PM
Member
 
Join Date: Jan 2008
Posts: 4
DwayneWayne is on a distinguished road
Hello.
This is my first post on this forum, but I'll try to help you anyway

A String can also contain a number. You can use the parseInt method to parse an number in a String to an int.

Example:
Code:
String number_s = "1"; int number_i = Integer.parseInt(number);
You can also remove whitespaces in your case.. " Hello" with the trim() method:
Code:
String text = " Hello"; //this will remove trailing or leading whitespace from your String. text.trim();
The difference between the usage of == and .equals is quite big:
the == will compare the pointers of each of the objects, and see if they point at the same address.

the .equals will compare to object to eachother, etc. "hello" and "Hello" will not equal - return boolean false... because there is a difference between uppercase and lowercase letters... here you would use .equalsIgnoreCase. So the .equal compares the actual Strings in this case and check if their size equals.

double will hold the largest number, this is the largest primitive data-type.

I hope this helped a little... feel free to throw some other questions

Last edited by DwayneWayne : 01-26-2008 at 08:07 PM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-26-2008, 08:36 PM
Member
 
Join Date: Dec 2007
Posts: 13
cew27 is on a distinguished road
thanks for that ill have a dabble later
also is there a way to have multiple allowances in an if? for later reference

also i presume the only way to have multiple ifs is this way
if whateever == whatever
code
if whatever == whaveter
code
else
sorry thats wrong
else sorry thats wrong
i cant remember where the { go so i left them out
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-26-2008, 09:15 PM
Member
 
Join Date: Jan 2008
Posts: 4
DwayneWayne is on a distinguished road
I don't know if you mean multiply "if" statements after each other... but that would be done like this:

Code:
if (statement) { something here } else if (statement) { something here } else if (statement) { something here }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-26-2008, 09:22 PM
Member
 
Join Date: Dec 2007
Posts: 13
cew27 is on a distinguished road
sorry but none of the above codes are working
Code:
if(choice.equalsIgnoreCase("area")) out.println("Please enter the diameter of the circle"); Scanner diameter = new Scanner(in); String diameter_s = diameter.next(); int diameter_i = Integer.parseInt(number);
i have import java.lang.Integer.*; and it still refuses to work
also how can i do this with a double?

and where do i place the trim code in this code
Code:
public static void main(String[] args) { out.println("Please enter your choice from the list above "); out.println("circle, triangle, cylinder: "); // scanner for the shape choice Scanner myScanner = new Scanner(in); String choice = myScanner.next();
once again much appretiated
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-26-2008, 09:51 PM
Member
 
Join Date: Jan 2008
Posts: 4
DwayneWayne is on a distinguished road
Your first problem should be solved with this code:
Code:
System.out.println("Please enter the diameter of the circle"); Scanner diameter = new Scanner(System.in); String diameter_s = diameter.next(); int diameter_i = Integer.parseInt(diameter_s);
You can achieve the exact same with doubles, if you use the Double.parseDouble(String here)

Your second problem... I dont fully understand your intention, but it would be done like this:
Code:
System.out.println("Please enter your choice from the list above "); System.out.println("circle triangle cylinder: "); // scanner for the shape choice Scanner myScanner = new Scanner(System.in); String choice = myScanner.next(); choice.trim(); //this will remove any whitespaces in the String
Just a reminder... remember to enclose your if-statements in with if { }
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-27-2008, 01:03 AM
Member
 
Join Date: Dec 2007
Posts: 13
cew27 is on a distinguished road
thanks for the help i am progressing now
just a few more questions left
is there anyway to name a whole process of function
for example
if answer = yes then call up the yes function (other if statements and scanner)
else if answer = no call up no function ??
also i still don't quite get the difference between the == and the .equals ? is there any documentation that tells me what each possibility is
like == and .equals and = or ect
sorry if this is hard to understand i don't know hot to set out this question
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 01-28-2008, 08:07 PM
Member
 
Join Date: Dec 2007
Posts: 13
cew27 is on a distinguished road
basically what i want to know is can you lay out stuff like this in java
function1{ =
out.println("hello");
SCANNER hello = new scanner(system.in)
int hello = parseint(number);
}
thats incredibly rough and just a visual representation of what i asume the function to be
so if int 1 == <23 {
function1
}
else{ function 2
}
i hope you understand what i just wrote
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 01-29-2008, 08:22 AM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
I didn't really read anyone else's posts, but why don't you just use the nextInt() method of the Scanner class. Now, if you want to ensure that the user enters an Integer, you can do a few different things.

Code:
Scanner in = new Scanner(System.in); System.out.print("Please enter a number: "); int x = 0; if(in.hasNextInt()) x = in.nextInt(); //do something with this number
Here is a more user-friendly way of doing it, because you always have to assume that the user will do something wrong, so if they do, we have to handle their mistake through exception handling (due to the fact that if they enter something other than an int, an InputMismatchException will be thrown)

Code:
Scanner in = new Scanner(System.in); System.out.print("Please enter a number: "); int x = 0; try { x = in.nextInt(); //Do something } catch(InputMismatchException e) { System.out.println("You must enter an integer value"); }
Now, of course, you can make it loop until the user enters an int value.
__________________
//Haha javac, can't see me now, can ya?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
MixedTypeInput example (Scanner) Java Tip Java Tips 0 01-18-2008 04:11 PM
Scanner class ajaymenon.k Advanced Java 1 11-26-2007 09:01 AM
Using ava.util.Scanner Java Tip Java Tips 0 11-20-2007 06:47 PM
help with IP scanner tommy New To Java 1 08-06-2007 10:00 PM
JDK 5.0 Scanner Class Sircedric88 New To Java 3 07-27-2007 08:55 PM


All times are GMT +3. The time now is 02:41 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org