0

I can get the program to add the preset values into the csv file but I want to adjust it so the user can enter the student ID Char(6) and the Student Mark (max 100) and also ensuring a minus mark cant be entered. How would I go about doing this?

public class Practicalassessment {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {

       System.out.println("Enter the Students ID:  ");
       scanner scanner = new scanner (System.in);
       String ID = scanner.nextline();
       System.out.println("You have selected Student" + ID);

       System.out.println("Enter the Students Mark");
       scanner scanner1 = new scanner(System.in);
       String mark = scanner1.nextLine();
       System.out.println("You Have Entered" + mark);

       String filepath = ("marks.txt");


       newMarks(ID,mark,filepath);
    }

    public static void newMarks (String ID, String mark, String filepath) throws IOException
    {
        try
        {
            FileWriter fw = new FileWriter(filepath,true);
            BufferedWriter bw = new BufferedWriter (fw);
            PrintWriter pw = new PrintWriter (bw);

            pw.println(ID+","+mark);
            pw.flush();
            pw.close();

            JOptionPane.showMessageDialog(null, "Records Updated Sucessfully");
        }
        catch (Exception E)
        {
            JOptionPane.showMessageDialog(null, "Records Unable to Be Updated");
        }
    }
}
sanyassh
  • 6,892
  • 12
  • 21
  • 48
shan468
  • 1
  • 2
  • If you have `JOptionPane` dialogs, why do you want to use a `Scanner`? Seems like you should gather through a GUI. – KevinO Mar 26 '19 at 19:17
  • 1
    Possible duplicate of [How can I read input from the console using the Scanner class in Java?](https://stackoverflow.com/questions/11871520/how-can-i-read-input-from-the-console-using-the-scanner-class-in-java) – KevinO Mar 26 '19 at 19:18
  • Thank you that other thread helped, I have been altering the code and for some reason still cant get it to work, I've edited the code to show what I have now – shan468 Mar 26 '19 at 19:40
  • Exception in thread "main" java.lang.UnsupportedOperationException: Not supported yet. at practicalassessment.scanner.(scanner.java:17) at practicalassessment.Practicalassessment.main(Practicalassessment.java:27) C:\Users\shan4\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 1 second) – shan468 Mar 26 '19 at 19:46
  • This is the error message I get when i try to run the program – shan468 Mar 26 '19 at 19:46

1 Answers1

0

Personally, I would keep to a single User Experience approach. If one is going to use JOptionPane to display a dialog, then one should collect information using a GUI as well.

I'd do (note that by convention Java variables are camelCase starting with a lower case):

String id = JOptionPane.showInputDialog("Enter Student's ID:");
// NOTE: need to check for null if canceled
// NOTE: should verify the input/format

The Error message noted in the comment is because the Java Scanner is with a capital letter. Not sure what that other thing is.

However, if one wants to use a Scanner, then instantiate only one:

Scanner scanner = new Scanner(System.in);

System.out.println("Enter the Students ID:  ");
String ID = scanner.nextline();
System.out.println("You have selected Student" + ID);

System.out.println("Enter the Students Mark");
String mark = scanner.nextLine();
System.out.println("You Have Entered" + mark);

...

Note that the same constraints of input validation exist as well with the Scanner input.

KevinO
  • 4,111
  • 4
  • 23
  • 34