We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,124 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Using Scanner with String

The problem I am having is in Juno Eclipse. "school.nextString" is being underlined in red to highlight a critical error. The basic purpose of this program is to ask the user a series of simple questions with if loops to determine the output given. Eclipse is handy and gives me suggestions as to what I should change nextString to, giving me "nextLong" and "toString" as the only fixes. Both of them cause more problems without solving the first one. Here's my code:

package assignment2;

import java.util.Scanner;

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

        String highSchool;
        Scanner school = new Scanner(System.in);
        System.out.println("Did you go to Milford High School, yes or no?");
        highSchool = school.nextString();
        int totalCredits = 0;
        System.out.println("How many credits have you earned?");
        totalCredits = school.nextInt();
        double gpa = 0;
        System.out.println("Do you have a GPA higher than 4.2?");
        gpa = school.nextInt();
        boolean footballPlayer = false;
        System.out.println("Are you a football player?");
        footballPlayer = school.nextBoolean(); {

        if (highSchool.equals("Yes"))
            System.out.println("Go straight to Harvard");
        else
            if (totalCredits < 20)
                System.out.println("Stay in school");
            else 
                if (gpa > 4.2)
                    System.out.println("Go to UC");
                else
                    System.out.println("Apply to college");
        if (footballPlayer == true)
                System.out.println("Expect special treatment");


        }
    }


    }
2
Contributors
1
Reply
1 Hour
Discussion Span
7 Months Ago
Last Updated
2
Views
minnie.taylor.524
Newbie Poster
1 post since Sep 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

There is no nextString() for Scanner class (API doc). Use nextLine() or next(), but I prefer nextLine() though.

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 375
Skill Endorsements: 17

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0591 seconds using 2.67MB