Results 1 to 11 of 11
  1. #1
    cew27 is offline Member
    Join Date
    Dec 2007
    Posts
    13
    Rep Power
    0

    Smile 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 :D

  2. #2
    DwayneWayne is offline Member
    Join Date
    Jan 2008
    Posts
    4
    Rep Power
    0

    Default

    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:
    Java Code:
    String number_s = "1";
    int number_i = Integer.parseInt(number);
    You can also remove whitespaces in your case.. " Hello" with the trim() method:
    Java 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 06:07 PM.

  3. #3
    cew27 is offline Member
    Join Date
    Dec 2007
    Posts
    13
    Rep Power
    0

    Default

    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

  4. #4
    DwayneWayne is offline Member
    Join Date
    Jan 2008
    Posts
    4
    Rep Power
    0

    Default

    I don't know if you mean multiply "if" statements after each other... but that would be done like this:

    Java Code:
    if (statement) {
    something here
    } else if (statement) {
    something here
    } else if (statement) {
    something here
    }

  5. #5
    cew27 is offline Member
    Join Date
    Dec 2007
    Posts
    13
    Rep Power
    0

    Default

    sorry but none of the above codes are working
    Java 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
    Java 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

  6. #6
    DwayneWayne is offline Member
    Join Date
    Jan 2008
    Posts
    4
    Rep Power
    0

    Default

    Your first problem should be solved with this code:
    Java 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:
    Java 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 { }

  7. #7
    cew27 is offline Member
    Join Date
    Dec 2007
    Posts
    13
    Rep Power
    0

    Default

    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

  8. #8
    cew27 is offline Member
    Join Date
    Dec 2007
    Posts
    13
    Rep Power
    0

    Default

    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:)

  9. #9
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    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.

    Java 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)

    Java 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.

  10. #10
    sasderwn is offline Member
    Join Date
    Mar 2009
    Location
    usa
    Posts
    1
    Rep Power
    0

    Default Hello World

    Hello World

  11. #11
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    please don't revive old topics for no reason. if you have anything to post, please do it in appropriate sub-forum and topic.

Similar Threads

  1. MixedTypeInput example (Scanner)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-18-2008, 02:11 PM
  2. Scanner class
    By ajaymenon.k in forum Advanced Java
    Replies: 1
    Last Post: 11-26-2007, 07:01 AM
  3. Using ava.util.Scanner
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-20-2007, 04:47 PM
  4. help with IP scanner
    By tommy in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:00 PM
  5. JDK 5.0 Scanner Class
    By Sircedric88 in forum New To Java
    Replies: 3
    Last Post: 07-27-2007, 06:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •