Ошибка: Неразрешенная проблема компиляции: - PullRequest
0 голосов
/ 06 октября 2019

/ * Простая база телефонных номеров, которая использует список свойств * /

Я много раз запускал эту программу в Eclipse, но это показывает мне нерешенную проблему компиляции

package phoneBook;

import java.io.*;

import java.io.File;

import java.util.Properties;

class meths


{       

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

    FileInputStream fin=null;





    try {

        fin=new FileInputStream("PhoneBook.txt");

    }catch(FileNotFoundException e)

    {

        e.printStackTrace();

    }





    String name,number;

    Properties pr=new Properties();





    if(fin!=null)

    {

        try {

            pr.load(fin); fin.close();

        }catch(FileNotFoundException e)

        {

            e.printStackTrace();

        }

    }





    void find() throws IOException

    {       

        System.out.println("Enter name to find number");

        name=br.readLine();

        System.out.println("Number is:"+pr.getProperty(number));

    }




    void enter()

    {

        FileOutputStream fout=new FileOutputStream("PhoneBook.txt");

        System.out.println("Enter name and number:");

        name=br.readLine();number=br.readLine();



        pr.put(name, number);
        pr.store(fout, "Phone Boook");
    }


     void operation()
        {   

            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            int opt=0;
            System.out.println("1 for stop operation\n2 for enter new entry\n3 for searching number");


            while(opt!=1)
            {
                // Reading option
                try {
                    opt=br.read();
                } catch (IOException e) {
                    e.printStackTrace();
                }// Read opearation is complete


                switch(opt)
                {
                case 2: enter();    break;
                case 3: find();     break;
                }
            }
        }
}


public class PhoneBook 
{
    public static void main(String args[]) throws Exception
    {``
        meths ph=new meths();
        ph.operation();
    }
}

Ответы [ 2 ]

0 голосов
/ 06 октября 2019

Вот возможное исправление:

package phoneBook;

import java.io.*;
import java.util.Properties;

class meths {
 BufferedReader br;
 FileInputStream fin;
 String name, number;
 Properties pr;
 File file;
 String path = "D:/PhoneBook.txt";

 public meths() {
    file = new File(path);
    pr = new Properties();
    br = new BufferedReader(new InputStreamReader(System.in));

    //If file doesn't exist, create it and load it
    if (!(file.exists() && file.canRead())){
        try {
            file.createNewFile();
        } catch (IOException e) {
            System.out.println("File can't be created");
        }
    }
        try {
            fin = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            System.out.println("File can't be read");
        }

    // If file is not empty, load properties
    if (fin != null) {
        try {
            pr.load(fin);
            fin.close();

        } catch (IOException e) {
            System.out.println("Properties not loaded");
        }
     }
  }

 void find() throws IOException {
    System.out.println("\n>Enter name to find number:");
    name = br.readLine();
    System.out.println("Number is: " + pr.getProperty(name));
 }

 void enter() throws IOException {
    System.out.println("\n>Enter the name :");
    name = br.readLine();
    System.out.println("\n>Enter the number :");
    number = br.readLine();

    FileOutputStream fout = new FileOutputStream(file);

    pr.put(name, number);
    pr.store(fout, "Phone Boook");
 }

 void choice() throws IOException {
    System.out.println("\n>Enter your choice :");
 }

 public boolean isInteger(String chaine) {
    try {
        Integer.parseInt(chaine);
    } catch (NumberFormatException e) {
        return false;
    }
    return true;
  }

void operation() throws IOException {
    int opt = 0;

    System.out.println("1 for stop operation\n2 for enter new entry\n3 for searching number\n");

    while (opt != 1) {
        choice();
        String value = br.readLine();
        if (isInteger(value)) {
            opt = Integer.parseInt(value);
            switch (opt) {
            case 2:
                enter();
                break;
            case 3:
                find();
                break;
            }
         }
      }
   }
 }

 public class PhoneBook {

  public static void main(String args[]) throws Exception {
    meths ph = new meths();
    ph.operation();
  }
}

Вид консоли:

enter image description here

Файл:

enter image description here

В заключение скажу, что вам нужно поработать над основами, чтобы понять, как работают класс, конструктор, методы и члены.

0 голосов
/ 06 октября 2019

Вы не можете написать код, как будто, try-catch и т. Д. Вне блока метода, как упомянуто Эллиоттом Фришем. Код ниже находится вне любого метода. Поэтому вам нужно поместить эти строки в метод, а затем вызвать его из другого метода.

try {

     fin=new FileInputStream("PhoneBook.txt");

} catch(FileNotFoundException e) {
        e.printStackTrace();
}

и

if(fin!=null) {
   try {

       pr.load(fin); fin.close();

   } catch(FileNotFoundException e) {
            e.printStackTrace();
        }
   }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...