-1

I'm trying to write a program which will take user input from a list of options then either:

  • write to a file
  • alter an existing file or
  • delete a file.

At the moment I'm stuck on just writing to the file from the user input. I have to use regex notation for each of the users input as seen in the snippet. Any help or guidance on what i could do will be highly appreciated, and yes there are a lot of errors right now. Thanks!

import java.io.*;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


private UserInput[] list;
class Input {
    public static void main(String[] args) {

        Scanner in = (System.in);
        string Exit ="False"; 
        System.out.println("person details");
        System.out.println("");
        System.out.println("Menu Options:");
        System.out.println("1. Add new person");
        System.out.println("2. Load person details ");
        System.out.println("3. Delete person Entry");
        System.out.print("Please select an option from 1-5\r\n");
        int choice = in.nextLine();


        if (choice == 1)
            system.out.println("you want to add a new person deails.");
            AddStudnet(); 


       else if (choice == 2){
            system.println("would you like to: ");
            system.println("1. Load a specific entry");
            system.println("2. Load ");


        }


//Error checking the options

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        try {
            int input = Integer.parseInt(br.readLine());

            if(input < 0 || input > 5) {
                System.out.println("You have entered an invalid selection, please try again\r\n");
            } else if(input == 5) {
                System.out.println("You have quit the program\r\n");
                System.exit(1);
            } else {
                System.out.println("You have entered " + input + "\r\n");
            }
        } catch (IOException ioe) {
            System.out.println("IO error trying to read your input!\r\n");
            System.exit(1);
        }
    }

}





//Scanner reader = new Scanner(System.in);{  // Reading from System.in
//System.out.println("Please enter your name: ");
//String n = reader.nextLine();
//}


  //      File file = new File("someFile.txt", True);
    //    FileWriter writer = new FileWriter(file);

      //  writer.write(reader);
//        writer.close();




public static void addPerson(String args[]) throws IOException{
    class input Per{

    scanner in = new scanner (system.in);
    system.out.print("Please enter you Name: ");
     String name = in.nextLine()
     final Pattern pattern = Pattern.compile("/^[a-z ,.'-]+$/i");
     if (!pattern.matcher(name).matches()) {
        throw new IllegalArgumentException("Invalid String");

     //String Name = regex ("Name")

     //regex below for formatting



    system.out.println("Please enter your Job title:");
    //String CourseNum = regex ("JobTitle");



    system.out.println("Please enter your Town:");
   //String Town = regex("Town");

    system.out.println("Please enter your postcocde:");
    //String postcocde = regex("postcocde");

    system.out.println("Please enter your street:");
    //String Street = regex ("Street");

    system.out.println("Please enter your House Number:");
    //String HouseNum = regex ("HouseNum");


    }

}


//public static void
vapurrmaid
  • 2,141
  • 2
  • 11
  • 27
Alex550
  • 19
  • 2
  • 1
    What kind of guidance is it that you're after? You pasted a piece of code, but didn't actually ask a question. – SeverityOne Apr 11 '18 at 14:27
  • For starters, `Scanner in = (System.in);` is not valid Java. You want to do this: `Scanner in = new Scanner(System.in);`. Next, you offer your users to input integers 1-5, but only give them the information for what 1, 2, and 3 do. Next, I'm not sure why you're reading a file for input since the input should be coming from the `Scanner`. Maybe your end goal is to read from the file so you can present that information and add to it via the command line? Lastly, you say you're having trouble writing to a file, but nowhere in your code have you made an attempt to write to a file. – Francis Bartkowiak Apr 11 '18 at 14:32
  • I need help finding out how to write to files form the user input. and then view said files. I have made an attempt to write to files but i commented it out because it kept on breaking ? – Alex550 Apr 11 '18 at 14:34
  • 1
    Possible duplicate of [How do I create a file and write to it in Java?](https://stackoverflow.com/questions/2885173/how-do-i-create-a-file-and-write-to-it-in-java) – Lars Apr 11 '18 at 14:36
  • I've seen that post and tried what it suggested with the printwriter function but i'm trying to get the information form the user not just a pre made txt file – Alex550 Apr 11 '18 at 14:38
  • Then this might help: https://stackoverflow.com/q/11871520/3579095 – Lars Apr 11 '18 at 14:42
  • thanks yeah that did help, but once the user input is stored into a variable how do i write that information into a text file – Alex550 Apr 11 '18 at 14:46

1 Answers1

-1

Is not clear that you look for " i'm stuck on just writing to the file"

here exemple on how to create/write to a file

String export="/home/tata/test.txt";
if (!Files.exists(Paths.get(export)))
                Files.createDirectory(Paths.get(export));
List<String> toWrite = new ArrayList();
toWrite.add("tata");
Files.write(Paths.get(export),toWrite);

look at java tm https://docs.oracle.com/javase/tutorial/essential/io/file.html

lio

Lio
  • 41
  • 7