-1

I am new to CS and I am doing this online java class which is a joke tbh. I say that because the books we got are $15 books, custom made that were used for a summer camp that was "taught" to kids. There is no where in the book that has actual java information, it is just a bunch of programs. I did a bit of searching around and I can't seem to find what I am looking for.

One thing we have to do is modify the program so it can display first and last name. I have looked and seen that you can do two strings to get first and last name. Is there something that will get the entire line in java?

package computegrades;
import java.util.Scanner;
public class ComputeGrades {
public static void main (String[] args) {
    String[] names = getNames();
    int[] scores = getScores(names);
    double average = computeAverage(scores);
    int highestIndex = getHighest(scores);
    int lowestIndex = getLowest(scores);
    System.out.format("Average = %3.2f\n", average);
    System.out.println("Highest = " + names[highestIndex] + ": " + scores [highestIndex]);
    System.out.println("Lowest = " + names[lowestIndex] + ": " + scores [lowestIndex]);
    printLetterGrades(names, scores);
}

public static void printLetterGrades(String[] names, int[] scores)  {
    for (int i = 0; i < names.length; i++)  {
        if (scores[i] >= 90)    {
            System.out.println(names[i] + ": A");
        } else if (scores[i] >= 80) {
            System.out.println(names[i] + ": B");
        } else if (scores[i] >= 70) {
            System.out.println(names[i] + ": C");
        } else if (scores[i] >= 60) {
            System.out.println(names[i] + ": D");
        } else {
            System.out.println(names[i] + ": F");
        }
    }
}
    public static String[] getNames() {
    Scanner input = new Scanner(System.in);
    System.out.println("How many students?");
    int n = input.nextInt();
    System.out.println("Please enter their first names:");
    String[] names = new String[n];
    for (int i = 0; i < names.length; i++)  {
        System.out.print("name " + (i + 1) + ": ");
        names[i] = input.next();
    }
    System.out.println("Thank you.");
    return names;
}
public static int[] getScores(String[] names)   {
    System.out.println("Now, please enter their scores:");
    Scanner input = new Scanner(System.in);
    int[] scores = new int[names.length];
    for (int i = 0; i < names.length; i++)  {
        System.out.print(names[i] + "'s ");
        System.out.print("score: ");
        while(!input.hasNextInt()){
            input.next();
            System.out.println("Please enter an integer for the score");
            System.out.print("scores: ");
        }
        scores[i] = input.nextInt();
    }
    return scores;
}
public static double computeAverage(int[] scores)  {
    double sum = 0;
    for (int i = 0; i < scores.length; i++) {
        sum += scores[i];
    }
    return sum / scores.length;
}
public static int getHighest(int[] scores)  {
    int highestIndex = Integer.MIN_VALUE;
    int highestScore = Integer.MIN_VALUE;
    for (int i = 0; i < scores.length; i++) {
        if (scores[i] > highestScore)   {
            highestScore = scores[i];
            highestIndex = i;
        }
    }
    return highestIndex;
}
public static int getLowest(int[] scores)   {
    int lowestIndex = Integer.MAX_VALUE;
    int lowestScore = Integer.MAX_VALUE;
    for (int i = 0; i < scores.length; i++) {
        if (scores[i] < lowestScore)    {
            lowestScore = scores[i];
            lowestIndex = i;
        }
    }
    return lowestIndex;
}
}
Mammolytic
  • 155
  • 1
  • 1
  • 8
  • 6
    Hint: if your "book" doesn't contain the information you need, try a different one ;-) – GhostCat Jul 25 '16 at 13:52
  • 1
    Before posting a question here, always start by taking a look at the [javadoc](http://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#nextLine--). – azurefrog Jul 25 '16 at 13:53
  • Possible duplicate of [What's the difference between next() and nextLine() methods from Scanner class?](http://stackoverflow.com/questions/22458575/whats-the-difference-between-next-and-nextline-methods-from-scanner-class) – Jimmy M. Jul 25 '16 at 13:53
  • 4
    Try learning from [Oracle's Java tutorials](https://docs.oracle.com/javase/tutorial/) instead. – RealSkeptic Jul 25 '16 at 13:53

1 Answers1

3

Being new to Java, one of the resources you need to have on hand is the Oracle documentation pages, for example:

https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

That will guide you on what a Scanner can do.

Specifically, you should look for

.nextLine()

mayha
  • 593
  • 5
  • 15
  • 1
    I think that the [tutorial on scanning](https://docs.oracle.com/javase/tutorial/essential/io/scanning.html) is more suitable for a beginner than the Javadoc. – Andy Turner Jul 25 '16 at 13:57