Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: whats with the scanner?????

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    17
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default whats with the scanner?????

    hey all,

    i was writing a program which allows users to input either a square, circle or rectangle, and depending on the input, the program would go on to get the area and perimeter of that shape.

    when i run the program, it works fine till i input the shape i want, then it just goes on to say:

    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:840)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextInt(Scanner.java:2091)
    at java.util.Scanner.nextInt(Scanner.java:2050)
    at Shapes.main(Shapes.java:31)

    Here's the code:

    import static java.lang.System.out;
    import java.util.Scanner;
    public class Shapes {

    public static void main(String[] args) {
    Scanner myScanner = new Scanner(System.in);
    double diameter = 0;
    double circumference = 0;
    double radius = 0;
    double area = 0;
    double perimeter = 0;
    double side1 = 0;
    double side2 = 0;
    int shape = 0;
    int square = 0;
    int rectangle = 0;
    int circle = 0;
    System.out.println("This program finds the area and perimeter of either a square, rectangle or circle.");
    System.out.println("");
    System.out.println("Firstly, input what shape you want to find the perimeter and area of.");
    System.out.println("");
    shape = myScanner.nextInt();
    if (shape == square){
    System.out.println("");
    System.out.println("Please input the side length of the square.");
    System.out.println("");
    side1 = myScanner.nextDouble();
    area = side1 * side1;
    perimeter = side1 * 4;
    System.out.println("The area of your square is " + area + " and the perimeter is " + perimeter + ".");
    }

    The ifs continue for circle and rectangle.
    Pls pls pls help. i cant figure out wat is wrong

    Thanks


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: whats with the scanner?????

    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:840)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextInt(Scanner.java:2091)
    at java.util.Scanner.nextInt(Scanner.java:2050)
    at Shapes.main(Shapes.java:31)
    You will get an InputMismatchException at runtime when you ask the scanner for some particular type of input (like an int) but the scanner hits something that it simply can't interpret as that type of thing.

    I am guessing line 31 is hereabouts:

    System.out.println("Firstly, input what shape you want to find the perimeter and area of.");
    System.out.println("");
    shape = myScanner.nextInt(); // <-- is this line 31?
    if (shape == square){
        System.out.println("");

    If, at runtime, you don't enter something that is an integer numeral ("0", "42", "-666" etc) - in particular of you enter "square" - you will get the InputMismatchException.

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    JavaN00b101 (February 9th, 2012)

  4. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    17
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: whats with the scanner?????

    thank. do you know what i need to change so that i can input "square"?

  5. #4
    Junior Member
    Join Date
    Jan 2012
    Posts
    10
    My Mood
    Lurking
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: whats with the scanner?????

    Are you using an integer so that you can use the if (a==b), if you are you set all three shapes to 0 so that all your if statements will check for 0 == shape... second, if you want to compare "square" you could try using shape.equalsIgnoreCase("Square") but your shape must be a string and not an int...

  6. The Following User Says Thank You to rchrispink3913 For This Useful Post:

    JavaN00b101 (February 9th, 2012)

  7. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: whats with the scanner?????

    Quote Originally Posted by JavaN00b101 View Post
    thank. do you know what i need to change so that i can input "square"?
    You mean the String "square"? or the dimensions for square?

  8. The Following User Says Thank You to Mr.777 For This Useful Post:

    JavaN00b101 (February 9th, 2012)

  9. #6
    Junior Member
    Join Date
    Nov 2011
    Posts
    17
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: whats with the scanner?????

    i mean what do i need to change in my code so that when i input a shape, i won't get a user input mismatch error

  10. #7
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: whats with the scanner?????

    Either input what your program demands or change the program to get input what you want it to get.

  11. The Following User Says Thank You to Mr.777 For This Useful Post:

    JavaN00b101 (February 9th, 2012)

  12. #8
    Junior Member
    Join Date
    Nov 2011
    Posts
    17
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: whats with the scanner?????

    i wanna change it but i dont know wat i need to change

  13. #9
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: whats with the scanner?????

    i mean what do i need to change in my code so that when i input a shape, i won't get a user input mismatch error
    Step back from the code a little and ask yourself what you expect the user to enter as a response to "Firstly, input what shape you want to find the perimeter and area of."

    You have complete freedom here. You might (a) want the user to enter a word (string) like "square", "circle" etc. Or (b) you might want them to enter a number like "0" (meaning square), "1" (meaning circle) etc. This is something you have to decide. Once you have you can think about what needs to change in your code.

    I assume from your earlier answer that you mean to enter a string like "square", "circle". If so there a few points that could be made:

    First get rid of variables you are not using. This will simplify your code. If you don't use the variable square anywhere, then remove it. Don't be afraid of throwing quite a lot of your code away and getting, say, just the square case to work. Working in small steps is good.

    Next since you want the user to enter a string you should not say

    shape = myScanner.nextInt();

    because that line reads an int, and you want to read a String. Go through the API docs, your textbook etc and find a Scanner method that will read a String.

    Thirdly, since you are reading a String you had better declare shape to be a String and not an int. Again consult your textbook or tutorials to find out what "declaring" a variable means and why it is important to declare variables to be the right type.

    That's enough to be going on with: remove unused variables, declare shape as a String, and use a different Scanner method to get the user's response. If you still have problems, post your new code and describe what happens when you compile and run it.

  14. The Following User Says Thank You to pbrockway2 For This Useful Post:

    JavaN00b101 (February 9th, 2012)

  15. #10
    Junior Member
    Join Date
    Nov 2011
    Posts
    17
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: whats with the scanner?????

    thanks so much

  16. #11
    Junior Member
    Join Date
    Nov 2011
    Posts
    17
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: whats with the scanner?????

    i got rid of unnecessary variables and declared shape as a string and changed the scanner method. the code now looks like this:


    import static java.lang.System.out;
    import java.util.Scanner;
    public class Shapes_2 {

    public static void main(String[] args) {
    Scanner myScanner = new Scanner(System.in);
    String shape;
    String square;
    double area = 0;
    double perimeter = 0;
    double side1 = 0;
    System.out.println("This program finds the area and perimeter of either a square, rectangle or circle.");
    System.out.println("");
    System.out.println("Firstly, input what shape you want to find the perimeter and area of.");
    System.out.println("");
    shape = myScanner.next();
    if (shape == square){ // error is here//
    System.out.println("");
    System.out.println("Please input the side length of the square.");
    System.out.println("");
    side1 = myScanner.nextDouble();
    area = side1 * side1;
    perimeter = side1 * 4;
    System.out.println("The area of your square is " + area + " and the perimeter is " + perimeter + ".");
    }
    }
    }

    when i run it, there is an error on line 25, which says variable square might not have been initialized. when i define square with "double", it says that they are incompatible types, so how do i define square.

  17. #12
    Junior Member
    Join Date
    Nov 2011
    Posts
    17
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: whats with the scanner?????

    i declared shape as a String, got rid of unnecessary code, and change the Scanner method. but there is still an error. the code now looks like this:

    import static java.lang.System.out;
    import java.util.Scanner;
    public class Shapes_2 {

    public static void main(String[] args) {
    Scanner myScanner = new Scanner(System.in);
    String shape;
    String square;
    double area = 0;
    double perimeter = 0;
    double side1 = 0;
    System.out.println("This program finds the area and perimeter of either a square, rectangle or circle.");
    System.out.println("");
    System.out.println("Firstly, input what shape you want to find the perimeter and area of.");
    System.out.println("");
    shape = myScanner.next();
    if (shape == square){ //error is here//
    System.out.println("");
    System.out.println("Please input the side length of the square.");
    System.out.println("");
    side1 = myScanner.nextDouble();
    area = side1 * side1;
    perimeter = side1 * 4;
    System.out.println("The area of your square is " + area + " and the perimeter is " + perimeter + ".");
    }
    }
    }


    there is an error on line 25, it says that square mat not have been initialized. when i define square with " double ", it says they are incompatible types, so how do i define square?

  18. #13
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: whats with the scanner?????

    The error like
    square might not have been initialized
    means that you have declared but forgot to initialize the variable.
    So, simply initialize the variable with any initial value. In this case, most common is null or "" (empty String)
    Also, why did you try to change it to double???
    Don't you think you want square to get String value not the double value.
    Don't just play hit and miss with compiler. Think logically, what you need and how your program behaves.

  19. #14
    Junior Member
    Join Date
    Nov 2011
    Posts
    17
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: whats with the scanner?????

    i didn't try to change it, i originally had it. thanks for the help

  20. #15
    Junior Member
    Join Date
    Nov 2011
    Posts
    17
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: whats with the scanner?????

    is it ok if i initialized it by writing:

    String square = ("");

    im a noob so i dont really know how

  21. #16
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: whats with the scanner?????

    Did you try doing this? What did compiler do?

  22. #17
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: whats with the scanner?????

    Quote Originally Posted by JavaN00b101 View Post
    is it ok if i initialized it by writing:

    String square = ("");

    im a noob so i dont really know how
    Don't you mean

    String square = "square";

    The thing is you want to compare what the user enters with the string s-q-u-a-r-e, not with a variable square.

    -----

    Another point is that you can't compare strings with ==. Use the equals() method for that.

    if(shape.equals(square)) {

  23. #18
    Junior Member
    Join Date
    Nov 2011
    Posts
    17
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: whats with the scanner?????

    thanks all for the help

Similar Threads

  1. Hey, whats up everybody?
    By Metalshadow24 in forum Member Introductions
    Replies: 2
    Last Post: October 29th, 2011, 03:37 PM
  2. Whats wrong here?
    By Java Sucks in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 8th, 2011, 08:33 PM
  3. whats logic behind this?
    By X0X0 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 26th, 2011, 01:48 PM
  4. could you tell me whats wrong....
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 6th, 2010, 04:35 PM
  5. help whats wrong
    By silverspoon34 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 3rd, 2009, 01:41 AM