Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  me and my noobish brother are having problems with Scanners  (Read 1864 times)
0 Members and 1 Guest are viewing this topic.
Offline gmmaster

Senior Newbie





« Posted 2012-03-07 02:07:05 »

The problem is, every time we type "no", we have to type it twice??
We don't understand why? Please help.... Smiley

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
package dandd.aanda;

import java.util.Scanner;

public class TestPrintf {
   public static void main(String[] args)  {
     
      String yes = "yes";
      String no = "no";
     
      System.out.println("type yes or no");
     
      Scanner input = new Scanner(System.in);
     
      if (input.nextLine().equals(yes)) {
         System.out.println("Follow me then boooyyyoo!!!!");
      } else if (input.nextLine().equals(no)) {
         System.out.println("Alright then laddy, you just going to sit on the porch all day and crrry then?");
      } else {
         System.out.println("that didn't help me, yes or no?");
      }
     
   }

loopish's brother Cheesy
Offline UprightPath
« Reply #1 - Posted 2012-03-07 02:12:14 »

input.nextLine() requests input each time you call it.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
String inputValue;
...
inputValue = input.nextLine();

if(inputValue.equals(yes)) {
...
} else if(inputValue.equals(no)) {
...
} else {
...
}

Offline RylandAlmanza

Junior Devvie


Medals: 3



« Reply #2 - Posted 2012-03-07 02:12:33 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
package dandd.aanda;

import java.util.Scanner;

public class TestPrintf {
   public static void main(String[] args)  {
     
      String yes = "yes";
      String no = "no";
     
      System.out.println("type yes or no");
     
      Scanner input = new Scanner(System.in);

      String line = input.nextLine();
      if (line.equals(yes)) {
         System.out.println("Follow me then boooyyyoo!!!!");
      } else if (line.equals(no)) {
         System.out.println("Alright then laddy, you just going to sit on the porch all day and crrry then?");
      } else {
         System.out.println("that didn't help me, yes or no?");
      }
     
   }

Try that ^
When you call input.nextLine(), it waits for input, so first it waits for input, then checks if it's yes. Then it waits for input again, and checks if it's no. to solve it, just call input.nextLine() once, and store it in a variable.

EDIT: Dammit UprightPath! I thought I was helping someone, and I get "Warning - while you were typing a new reply has been posted. You may wish to review your post." Why am I always too slow to help people?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Cero
« Reply #3 - Posted 2012-03-07 02:12:42 »

well everytime you do call input.nextLine() you get it and then it empties the buffer.

just do String answer = input.nextLine()

and then do if-cases on it

Offline Cero
« Reply #4 - Posted 2012-03-07 02:13:58 »

synchronous answer x 3 bonus !

Offline UprightPath
« Reply #5 - Posted 2012-03-07 02:15:08 »

I've been practicing short-ish answers so that I can be the first!

Offline RylandAlmanza

Junior Devvie


Medals: 3



« Reply #6 - Posted 2012-03-07 02:17:33 »

Short-ish answers, huh? I will learn this new practice, and once I have mastered it, it will the the end of your reign as first poster!
Offline gmmaster

Senior Newbie





« Reply #7 - Posted 2012-03-07 02:26:34 »

First of all, thanks to all that replied SO FAST!!! and second, me and my VERY noobish brother totally just LOL-ed ALL over the floor Cheesy it was grand! Thanks for the java-lin exp. Cheesy and IT WORKS!!!!! WWOOOOOTTTT!!!

loopish's brother Cheesy
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2012-03-07 03:21:21 »

C-C-C-C-C-C-COMBO BREAKER!!!!

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (28 views)
2014-12-15 09:26:44

Mr.CodeIt (19 views)
2014-12-14 19:50:38

BurntPizza (39 views)
2014-12-09 22:41:13

BurntPizza (74 views)
2014-12-08 04:46:31

JscottyBieshaar (36 views)
2014-12-05 12:39:02

SHC (49 views)
2014-12-03 16:27:13

CopyableCougar4 (44 views)
2014-11-29 21:32:03

toopeicgaming1999 (112 views)
2014-11-26 15:22:04

toopeicgaming1999 (98 views)
2014-11-26 15:20:36

toopeicgaming1999 (29 views)
2014-11-26 15:20:08
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!