Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (408)
games submitted by our members
Games in WIP (293)
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 889 times)
0 Members and 1 Guest are viewing this topic.
Offline gmmaster

Junior Newbie





« Posted 2012-03-07 03: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 03: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 Member


Medals: 3



« Reply #2 - Posted 2012-03-07 03: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 03: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 03:13:58 »

synchronous answer x 3 bonus !

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

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

Offline RylandAlmanza

Junior Member


Medals: 3



« Reply #6 - Posted 2012-03-07 03: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

Junior Newbie





« Reply #7 - Posted 2012-03-07 03: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: 264
Projects: 2


I'm the King!


« Reply #8 - Posted 2012-03-07 04: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.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Try the Free Demo of Revenge of the Titans

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (105 views)
2013-05-17 21:29:12

alaslipknot (114 views)
2013-05-16 21:24:48

gouessej (142 views)
2013-05-16 00:53:38

gouessej (138 views)
2013-05-16 00:17:58

theagentd (150 views)
2013-05-15 15:01:13

theagentd (135 views)
2013-05-15 15:00:54

StreetDoggy (178 views)
2013-05-14 15:56:26

kutucuk (201 views)
2013-05-12 17:10:36

kutucuk (202 views)
2013-05-12 15:36:09

UnluckyDevil (208 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.172 seconds with 21 queries.