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

Thread: Scanner does weird things

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Scanner does weird things

    import java.util.Scanner;
    public class MainClass {
    	static Scanner sc = new Scanner(System.in);
    	public static void main(String[] args) {
    		int input = sc.nextInt();
    		System.out.println("Welcome to the guess game!");
    		System.out.println("Bob and Angela joined the table");
     
    		}
    }


    Dear all,
    I'm trying to make a guess game as a total noob where you guess a word between 0 to 10 and two NPC's who will guess to. If you or one NPC got the number right, you or a NPC win. Else, re-run the program. I got an idea of the code in my head but now this problem occurs. You see that i have declared the scanner, and make the program say the intro. Before the welcoming message I declared the integer that you will guess. But the welcoming message and the message that the NPC's have joined only shows when I type a number. Why is this?
    Now i'm asking anyway, i have no idea how to re-run the program if no-one got it right. I guess i check if anyone won, and then make an else statement. What do I put in the else statement to re-run the program?
    Sorry, English isn't my native language.
    Thanks in advance,
    Niels.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Scanner does weird things

    the welcoming message and the message that the NPC's have joined only shows when I type a number
    The computer executes the instructions in the same order that they are in the program.
    The nextInt() method will wait until a user enters an int value
    After the number is read, the println statements will be executed.

    Usually you print a message to the user telling him about what the program wants him to enter BEFORE trying to read his input.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Scanner does weird things

    do it
    System.out.println("Welcome to the guess game!");
    System.out.println("Bob and Angela joined the table");
    int input = sc.nextInt();

Similar Threads

  1. Weird Compiling Error.
    By sakonpure6 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 22nd, 2013, 12:12 AM
  2. VERY WEIRD OUTPUT... HELP PLEASE?
    By Medo Almasry in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 23rd, 2011, 10:02 AM
  3. [SOLVED] Weird calendar.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 3rd, 2011, 08:19 PM
  4. Weird thing with JFrame
    By Brt93yoda in forum AWT / Java Swing
    Replies: 2
    Last Post: August 23rd, 2010, 05:00 PM
  5. Jsp weird problem
    By johniem in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: February 5th, 2010, 06:46 AM