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 6 of 6

Thread: Error in Java program

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Error in Java program

    I'm trying to write a program that asks a user for their name and number. Then the program should display the name and a random number from 0 to that number. Here is the code:

    import java.util.*;

    class name {
    public static void main(String args[]){
    Random rannumber = new Random();
    Scanner input = new Scanner(System.in);
    int name, number,randomnumber;
    System.out.println("What is your name? ");
    name = input.nextInt();
    System.out.println("Give me a number: ");
    number = input.nextInt();
    System.out.println("Hi "+name);
    randomnumber = rannumber.nextInt(number);
    System.out.println("The random number from 0 and "+number+"is "+randomnumber);

    }
    }

    Program output:
    What is your name?
    test
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at name.main(name.java:9)


  2. #2
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    3
    Thanked 3 Times in 1 Post

    Default Re: Error in Java program

    The user is prompted to enter a name, but you're telling the scanner to expect an int.

    Should be name = input.next() or input.nexLine()

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Error in Java program

    It's says Type mismatch: cannot convert from string to int.

  4. #4
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    3
    Thanked 3 Times in 1 Post

    Default Re: Error in Java program

    you also defined name as an int at the beginning. It should be a String name, not int name.

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Error in Java program

    nvm, fixed the problem by converting the variable name to a string

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Error in Java program

    You were just told what the problem is. Reading in one type and assigning to a variable of a different type. Make sure you don't do that on every line of code.

    Slow!
    Improving the world one idiot at a time!

Similar Threads

  1. error in my java program
    By ayushgarg11 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 2nd, 2013, 12:54 AM
  2. My Java program gets a strange error
    By 119mikkel in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 12th, 2013, 12:47 PM
  3. [SOLVED] Java Simple Rectangle Program Error
    By XP360 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2013, 12:07 AM
  4. Replies: 1
    Last Post: January 17th, 2013, 07:04 AM
  5. Java Error cannot be applied to (java.lang.String), phone book entry program.
    By iceyferrara in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 23rd, 2011, 07:32 AM