Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Java
  4. Scanner isn't scanning Strings Correctly.

Scanner isn't scanning Strings Correctly.

Scheduled Pinned Locked Moved Java
javahelpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    johtnkucz
    wrote on last edited by
    #1

    import java.util.Scanner;

    public class ScannerTester{
    public static void main (String args[]){

    Scanner aScanner_input = new Scanner(System.in);
    String aScanner;
    aScanner= aScanner_input.nextLine();
    if (aScanner == "s"){
    System.out.println("A Scanner is s!");
    }
    if (aScanner == "Y"){
    System.out.println("A scanner is Y!");
    }

    }
    }

    Hey, the code is above. I wager this is some kind of cast or data type problem. I tried aScanner_input.next() and aScanner_input.nextLine(). With both it doesnt' recognize Y as Y and s as s!?!! i.e. none of the if-blocks can get accessed regardless of input. I can get scanner to work with other data types like int (and nextInt()). Appreciate if anyone knows what's going on. Cheers. Thanks. (*I love this forum. Very helpful. And as said before, I go to this forum and I don't use stackoverflow). Thanks!

    L J 2 Replies Last reply
    0
    • J johtnkucz

      import java.util.Scanner;

      public class ScannerTester{
      public static void main (String args[]){

      Scanner aScanner_input = new Scanner(System.in);
      String aScanner;
      aScanner= aScanner_input.nextLine();
      if (aScanner == "s"){
      System.out.println("A Scanner is s!");
      }
      if (aScanner == "Y"){
      System.out.println("A scanner is Y!");
      }

      }
      }

      Hey, the code is above. I wager this is some kind of cast or data type problem. I tried aScanner_input.next() and aScanner_input.nextLine(). With both it doesnt' recognize Y as Y and s as s!?!! i.e. none of the if-blocks can get accessed regardless of input. I can get scanner to work with other data types like int (and nextInt()). Appreciate if anyone knows what's going on. Cheers. Thanks. (*I love this forum. Very helpful. And as said before, I go to this forum and I don't use stackoverflow). Thanks!

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      if (aScanner == "s"){

      should be

      if (aScanner.equals("s")){

      See also here[^] for more information on Java Strings.

      Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

      1 Reply Last reply
      0
      • J johtnkucz

        import java.util.Scanner;

        public class ScannerTester{
        public static void main (String args[]){

        Scanner aScanner_input = new Scanner(System.in);
        String aScanner;
        aScanner= aScanner_input.nextLine();
        if (aScanner == "s"){
        System.out.println("A Scanner is s!");
        }
        if (aScanner == "Y"){
        System.out.println("A scanner is Y!");
        }

        }
        }

        Hey, the code is above. I wager this is some kind of cast or data type problem. I tried aScanner_input.next() and aScanner_input.nextLine(). With both it doesnt' recognize Y as Y and s as s!?!! i.e. none of the if-blocks can get accessed regardless of input. I can get scanner to work with other data types like int (and nextInt()). Appreciate if anyone knows what's going on. Cheers. Thanks. (*I love this forum. Very helpful. And as said before, I go to this forum and I don't use stackoverflow). Thanks!

        J Offline
        J Offline
        johtnkucz
        wrote on last edited by
        #3

        OMG I realized this is the exact same problem I had before when posting on my first post. I need to use a String.equals() comparison for strings in the if-conditional. I will try that. (thanks...to my recollection of solving that problem the first time! lol xD). Marvelous. got it. Sweet. solved. nice code symmetry (start code and end fixed code, too)!

        import java.util.Scanner;

        public class ScannerTester{
        public static void main (String args[]){

        Scanner aScanner_input = new Scanner(System.in);
        String aScanner;
        aScanner= aScanner_input.next();
        if (aScanner.equals("s")){
        System.out.println("aScanner is s!");
        }
        if (aScanner.equals("Y")){
        System.out.println("aScanner is Y!");
        }

        }
        }

        //*/

        S 1 Reply Last reply
        0
        • J johtnkucz

          OMG I realized this is the exact same problem I had before when posting on my first post. I need to use a String.equals() comparison for strings in the if-conditional. I will try that. (thanks...to my recollection of solving that problem the first time! lol xD). Marvelous. got it. Sweet. solved. nice code symmetry (start code and end fixed code, too)!

          import java.util.Scanner;

          public class ScannerTester{
          public static void main (String args[]){

          Scanner aScanner_input = new Scanner(System.in);
          String aScanner;
          aScanner= aScanner_input.next();
          if (aScanner.equals("s")){
          System.out.println("aScanner is s!");
          }
          if (aScanner.equals("Y")){
          System.out.println("aScanner is Y!");
          }

          }
          }

          //*/

          S Offline
          S Offline
          sandbad ae90
          wrote on last edited by
          #4

          You can say:

          import java.util.Scanner;

          public class ScannerTester
          {

          public static void main ( String[] args )
          {
          Scanner aScanner_input = new Scanner( System.in );
          String aScanner;

            aScanner = aScanner\_input.next();
          
            if ( sScanner == "s" )
            {
               System.out.println("aScanner is s!");
            }
          
            if ( aScanner == "Y" )
            {
               System.out.println("aScanner is Y!");
            }
          

          }
          }

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups