Scanner Class help?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thename1000
    New Member
    • Sep 2007
    • 2

    Scanner Class help?

    Hi,

    I'm trying to create this output:

    Input team 1's name:
    Team 1
    Input team 1's ranking:
    90.4
    etc.

    For 8 teams.

    This is what I have:


    import java.util.Scann er;

    public class Tournament {
    public static void main(String args[]) {
    System.out.prin tln("Input team 1's name:");
    Scanner a = new Scanner(System. in);
    System.out.prin tln("Input team 1's ranking:");
    Scanner b = new Scanner(System. in);
    System.out.prin tln("Input team 2's name:");
    Scanner c = new Scanner(System. in);
    System.out.prin tln("Input team 2's ranking:");
    Scanner d = new Scanner(System. in);
    System.out.prin tln("Input team 3's name:");
    Scanner e = new Scanner(System. in);
    System.out.prin tln("Input team 3's ranking:");
    Scanner f = new Scanner(System. in);
    System.out.prin tln("Input team 4's name:");
    Scanner g = new Scanner(System. in);
    System.out.prin tln("Input team 4's ranking:");
    Scanner h = new Scanner(System. in);
    System.out.prin tln("Input team 5's name:");
    Scanner i = new Scanner(System. in);
    System.out.prin tln("Input team 5's ranking:");
    Scanner j = new Scanner(System. in);
    System.out.prin tln("Input team 6's name:");
    Scanner k = new Scanner(System. in);
    System.out.prin tln("Input team 6's ranking:");
    Scanner l = new Scanner(System. in);
    System.out.prin tln("Input team 7's name:");
    Scanner m = new Scanner(System. in);
    System.out.prin tln("Input team 7's ranking:");
    Scanner n = new Scanner(System. in);
    System.out.prin tln("Input team 8's name:");
    Scanner o = new Scanner(System. in);
    System.out.prin tln("Input team 8's ranking:");
    Scanner p = new Scanner(System. in);

    }

    }

    But when I try to compile it gives me 33 errors: "Tournament.jav a:36: cannot resolve symbol
    symbol : class Scanner
    location : class Tournament
    Scanner [whatever variable]= new Scanner(System. in);"

    Even if it does compile will the code I provided produce that output? I think I might need to use loops, but how would I do that?

    Any ideas?

    Thanks
  • thename1000
    New Member
    • Sep 2007
    • 2

    #2
    EDIT:

    I have gotten help from someone and he gave me the following code:

    import java.util.Array List;
    import java.util.List;
    import java.util.Scann er;



    class Tournament {

    public static void main (String[] args) {

    int numberOfTeams = 8;

    Scanner in = new Scanner(System. in);

    List<Team> teams = new ArrayList<Team> ();

    for (int i = 0; i < numberOfTeams; i++) {

    System.out.prin tf("\nInput team %d's name: ", i + 1);

    String name = in.nextLine();

    System.out.prin tf("Input team %d's ranking: ", i + 1);

    double rank = Double.parseDou ble(in.nextLine ());

    Team team = new Team(name, rank);

    teams.add(team) ;

    }

    System.out.prin tln("\n\nHere are the teams you entered:");

    for (Team team : teams) {

    System.out.prin tln(">>> " + team);

    }

    }

    }



    class Team {

    private String name;

    private double rank;



    public Team(String name, double rank) {

    this.name = name;

    this.rank = rank;

    }



    public String toString() {

    return String.format(" Team[%s, rank: %.1f]", this.name, this.rank);

    }

    }


    I understand mostly what this is doing, (it does exactly what i wanted) but I don't understand the addition of arrays and the "Team team"

    Can someone please explain roughly what each part of this chunk of code does?

    Thanks

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      So you asked for help somewhere else, and he gave you the full code without bothering to explain? In effect, you haven't learned how to do the project on your own. You should go back to this person and tell them you didn't learn anything, and you'd actually like an explanation of how to do it so you can become a better programmer.

      Comment

      • sreekandank
        New Member
        • Jun 2009
        • 45

        #4
        Try with JDK1.5.0 or any higher version of Java. Because Scanner class is available from JDK1.5.0

        Comment

        Working...