4 Replies - 598 Views - Last Post: 27 August 2017 - 01:43 PM Rate Topic: -----

#1 BlazeDaBlur  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-August 17

Why does my scanner return something that I don't want it to

Posted 16 August 2017 - 03:00 PM

The title might not be very descriptive, but i have a scanner:
String c;
    System.out.println("Surface area or Volume");
    c = sc.next();
    if (c == "Surface area") {
    } else {
      System.out.println("ok, surface area coming right up:");
      System.out.println("The answer to a cylinder's surface area: " + surArea);
      System.out.println("The math:");
      System.out.println("r = " + r + ", h = " + h + ", er = " + er);
      System.out.println("sav1 = " + sav1 + ", 2 * pi = " + ap + " * r = " + ne);
      System.out.println("sav2 = " + sav2 + ", 2 * pi = " + ap + ", * er = " + na + ", * h = " + lo + ", and + sav1 = " + surArea);
    }
    if (c == "Volume") {
    } else {
      System.out.println("ok, volume coming right up:");
      
    }

and if I say: Surface area, I get:
System.out.println("ok, surface area coming right up:");
      System.out.println("The answer to a cylinder's surface area: " + surArea);
      System.out.println("The math:");
      System.out.println("r = " + r + ", h = " + h + ", er = " + er);
      System.out.println("sav1 = " + sav1 + ", 2 * pi = " + ap + " * r = " + ne);
      System.out.println("sav2 = " + sav2 + ", 2 * pi = " + ap + ", * er = " + na + ", * h = " + lo + ", and + sav1 = " + surArea);

but if I say Volume, i get:
System.out.println("ok, surface area coming right up:");
      System.out.println("The answer to a cylinder's surface area: " + surArea);
      System.out.println("The math:");
      System.out.println("r = " + r + ", h = " + h + ", er = " + er);
      System.out.println("sav1 = " + sav1 + ", 2 * pi = " + ap + " * r = " + ne);
      System.out.println("sav2 = " + sav2 + ", 2 * pi = " + ap + ", * er = " + na + ", * h = " + lo + ", and + sav1 = " + surArea);

Why is this, and there is almost nothing on the internet about scanners.

Is This A Good Question/Topic? 0
  • +

Replies To: Why does my scanner return something that I don't want it to

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3417
  • View blog
  • Posts: 15,410
  • Joined: 20-September 08

Re: Why does my scanner return something that I don't want it to

Posted 16 August 2017 - 03:33 PM

Quote

if (c == "Surface area") {

You use String.equals in Java to test string equality. == will test reference equality - something usually of no interest

This post has been edited by g00se: 16 August 2017 - 03:34 PM
Reason for edit:: Clarification

Was This Post Helpful? 2
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1565
  • View blog
  • Posts: 3,513
  • Joined: 05-April 11

Re: Why does my scanner return something that I don't want it to

Posted 16 August 2017 - 03:49 PM

On top of g00se's suggestion, you'll probably have to use scanner.nextLine(); instead of scanner.next();. It all depends on your delimiter.
Was This Post Helpful? 2
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3417
  • View blog
  • Posts: 15,410
  • Joined: 20-September 08

Re: Why does my scanner return something that I don't want it to

Posted 17 August 2017 - 01:35 AM

There's an even worse problem actually - your if blocks are empty
Was This Post Helpful? 2
  • +
  • -

#5 Basic_Coder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 26-August 17

Re: Why does my scanner return something that I don't want it to

Posted 27 August 2017 - 01:43 PM

scanner.next(); only reads until a space. e.g.
in the input "adda 0000", it will stop at the space, so your input (for the variables) will be "adda".

scanner.nextLine(); reads everything inputted. e.g.
in the input "adda 0000", it will read all of it, so your input (for the variables) will be "adda 0000".

This post has been edited by g00se: 28 August 2017 - 04:23 AM
Reason for edit:: No need to shout

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1