8 Replies - 634 Views - Last Post: 29 November 2013 - 05:37 PM Rate Topic: -----

#1 Bailey12349   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-November 13

Question about scanners for strings in Java

Posted 29 November 2013 - 04:48 PM

I'm very new to Java and programming in general but am trying very hard to learn. I'll post the code below this, but before that I'll tell what it's supposed to do. The program asks you what your favorite food is, and if you reply with "pizza," then it says, "Cool... Me too!" All I'm doing is trying to figure out how to use a scanner with something other than integers, like if the program asks you a question then you'll be able to reply with words. I've been searching everywhere and found out that you would use String instead of int for the data type. I keep getting an error saying "Test.java:15: error: ';' ." This error is for line 10. Here is the code... I really appreciate the help! Thanks!

import java.util.Scanner;

public class Test
{
public static void main (String []args)
{
Scanner input = new Scanner (System.in);

System.out.println("What is your favorite food?");
String food = input.String next();
if (food == "pizza")
{
System.out.println("Cool... Me too!");
}
}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Question about scanners for strings in Java

#2 x68zeppelin80x   User is offline

  • D.I.C Addict

Reputation: 130
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: Question about scanners for strings in Java

Posted 29 November 2013 - 04:50 PM

View PostBailey12349, on 29 November 2013 - 07:48 PM, said:

I've been searching everywhere and found out that you would use String instead of int for the data type.


Use:

String myString = input.nextLine();


From the official javadocs: java.util.Scanner

Type       Signature                 Description
-------    ---------                 -----------

String     next()                    // Finds and returns the next complete token from this scanner.
String     next(Pattern pattern)     // Returns the next token if it matches the specified pattern.
String     next(String pattern)      // Returns the next token if it matches the pattern constructed from the specified string.
BigDecimal nextBigDecimal()          // Scans the next token of the input as a BigDecimal.
BigInteger nextBigInteger()          // Scans the next token of the input as a BigInteger.
BigInteger nextBigInteger(int radix) // Scans the next token of the input as a BigInteger.
boolean    nextBoolean()             // Scans the next token of the input into a boolean value and returns that value.
byte       nextByte()                // Scans the next token of the input as a byte.
byte       nextByte(int radix)       // Scans the next token of the input as a byte.
double     nextDouble()              // Scans the next token of the input as a double.
float      nextFloat()               // Scans the next token of the input as a float.
int        nextInt()                 // Scans the next token of the input as an int.
int        nextInt(int radix)        // Scans the next token of the input as an int.
String     nextLine()                // Advances this scanner past the current line and returns the input that was skipped.
long       nextLong()                // Scans the next token of the input as a long.
long       nextLong(int radix)       // Scans the next token of the input as a long.
short      nextShort()               // Scans the next token of the input as a short.
short      nextShort(int radix)      // Scans the next token of the input as a short.

This post has been edited by x68zeppelin80x: 29 November 2013 - 04:57 PM

Was This Post Helpful? 0
  • +
  • -

#3 Bailey12349   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-November 13

Re: Question about scanners for strings in Java

Posted 29 November 2013 - 04:54 PM

Oh, I apologize... The error was actually Test.java:10: error: ';'

View Postx68zeppelin80x, on 29 November 2013 - 04:50 PM, said:

Use:

String myString = input.nextLine();

Thanks so much, I was having a lot of trouble figuring that out! That makes sense.

This post has been edited by andrewsw: 29 November 2013 - 04:55 PM

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is online

  • Unprocessable Entity
  • member icon

Reputation: 6592
  • View blog
  • Posts: 26,823
  • Joined: 12-December 12

Re: Question about scanners for strings in Java

Posted 29 November 2013 - 04:54 PM

Quote

I've been searching everywhere

?? How about the documentation for Scanner:

Scanner nextLine :the docs
Was This Post Helpful? 0
  • +
  • -

#5 x68zeppelin80x   User is offline

  • D.I.C Addict

Reputation: 130
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: Question about scanners for strings in Java

Posted 29 November 2013 - 05:01 PM

If you need to scan in different type, I updated my answer to include other tokens as well.
Was This Post Helpful? 0
  • +
  • -

#6 Bailey12349   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-November 13

Re: Question about scanners for strings in Java

Posted 29 November 2013 - 05:28 PM

View Postx68zeppelin80x, on 29 November 2013 - 05:01 PM, said:

If you need to scan in different type, I updated my answer to include other tokens as well.

Alright, thanks!
Was This Post Helpful? 0
  • +
  • -

#7 ChrisNt   User is offline

  • cute bug

Reputation: 262
  • View blog
  • Posts: 896
  • Joined: 31-July 13

Re: Question about scanners for strings in Java

Posted 29 November 2013 - 05:31 PM

Also to compare Strings use method equals not ==

if (food.equals("pizza") )
{
System.out.println("Cool... Me too!");
}

Was This Post Helpful? 0
  • +
  • -

#8 x68zeppelin80x   User is offline

  • D.I.C Addict

Reputation: 130
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: Question about scanners for strings in Java

Posted 29 November 2013 - 05:37 PM

It's good practice to close your scanner when you are done with it.

Call this at the end of your main method:

input.close; // Close this resource to prevent resource leakage.

Was This Post Helpful? 0
  • +
  • -

#9 Bailey12349   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-November 13

Re: Question about scanners for strings in Java

Posted 29 November 2013 - 05:37 PM

View PostChrisNt, on 29 November 2013 - 05:31 PM, said:

Also to compare Strings use method equals not ==

if (food.equals("pizza") )
{
System.out.println("Cool... Me too!");
}

Awesome! Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1