-4

I have tried for a while to make a simple program, but since I'm new at it, it's taking me a bit to pick up on it. I'm following these instructions

  1. declare and initialize a char variable for the student grade;

  2. use a for loop that has five iterations;

  3. use the System.out.println() method in the for loop to request a grade from the student;

  4. use the System.in.read() method to receive student grades in the for loop;

  5. use the System.out.println() method to tell the student to try again if they enter anything other than one letter for a grade;

  6. use the System.out.println() method after the for loop to output a message for the student; and,

  7. compile and run your program.

This is how far I've gotten with no errors when I run it.

package grading.on.a.loop.java;
public class GradingOnALoopJava {
    public static void main(String[] args) {
        char studentgrade = 100;
        System.out.println("Please enter your Grade here.");
        for(int counter = 75; counter <= 100; counter += 5) {
            System.out.println(counter);
            System.out.println("Please enter your Grade.");
        }
    }
}

this is when I start to run into problems:

package grading.on.a.loop.java;
public class GradingOnALoopJava {
    public static void main(String[] args) {
        char studentgrade = 100;
        System.out.println("Please enter your Grade here.");
        for(int counter = 75; counter <= 100; counter += 5) {
            System.out.println(counter);
            System.out.println("Please enter your Grade.");
            System.in.read("");
        }
    }
}

The "System.in.read" part is giving me a bit of trouble, no matter what I've done to alter it, it always shows an error.

Any help is appreciated.

Przemysław Moskal
  • 3,321
  • 2
  • 8
  • 20
Timothy
  • 3
  • 3
  • 3
    `read` does not take any parameters and you are passing an empty String. – Kon Jan 10 '18 at 21:32
  • 2
    `read` returns an `int` so you can use `int b = System.in.read();` – Aniket Sahrawat Jan 10 '18 at 21:33
  • 1
    @Kon It is generally difficult to remember the difference between parameters and arguments :D – Aniket Sahrawat Jan 10 '18 at 21:38
  • 2
    @AniketSahrawat In Java they are used interchangeably from every source I've ever seen. EDIT: https://stackoverflow.com/questions/12709026/difference-between-arguments-and-parameters-in-java – Kon Jan 10 '18 at 21:40
  • You need to use a letter, a single char, not multiple chars. That’s what the assignment is looking for – Jacob B. Jan 10 '18 at 21:43
  • 2
    @Kon In almost every language issue is the same, even I get pretty confused between params and args. Anyways, check this out *[Parameters refers to the list of variables in a method declaration. Arguments are the actual values that are passed in when the method is invoked.](https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html)*. I don't want to enter into any debate because this is silly ;-) – Aniket Sahrawat Jan 10 '18 at 21:46
  • Then after reading all this, how do you put in a line in such code ^^^^ to accept user input? – Timothy Jan 10 '18 at 23:05

1 Answers1

0

Instead of using read(), you should initialize a scanner and use that.

This is what it would look like:

package grading.on.a.loop.java;

import java.util.Scanner;

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

        String[] grades = new String[5];

        for(int counter = 0; counter < 5; counter++)
        {
            System.out.println(counter * 5 + 75);
            System.out.print("Please enter your Grade: ");
            grades[counter] = in.nextLine();
            System.out.println("");
        }
    }
}