У меня есть следующий код, который открывает этот файл:как я могу пропустить « п» в этом коде
procedure TeTruena is
PI : constant Float := 3.141592;
MAX: constant Int := 30;
type Array is array(1 .. MAX) de
Int;
type Matrix es array(1 .. MAX, 1.. MAX)
of Int;
и я должен разделить каждый из слов этого файла в лексемы, как :
процедура (один маркер), есть (другой маркер), и т.д ..
и этот код должен делать все это:
public class Lexic {
public static void main(String[] args) {
int car;
String nameFile,lexem;
Scanner t = new Scanner(System.in);
System.out.print("Give me the name of the file: ");
nameFile = t.nextLine();
t.close();
try
{
File file = new File(nombreArchivo);
RandomAccessFile af = new RandomAccessFile(file,"r");
while((car = af.read()) != -1)
{
if(car != ' ' && car != '\n')
{
lexem = Lexic.readFile(nameFile,af);
System.out.print("["+lexem+"]");
}
}
af.close();
}
catch(IOException ex)
{
System.out.println("Can not open file");
}
}
public static String readFile(String name,RandomAccessFile r)
{
int car;
char c;
long position;
String s = null;
StringBuilder sb = new StringBuilder();
try
{
position = r.getFilePointer();
r.seek(position-1);
car = r.read();
if(specialChar(car))
{
c = (char)car;
sb.append(c);
s = sb.toString();
}
else
{
while((car != -1) && (car != ' ') && (car!= '\n') && (!specialChar(car)))
{
c = (char)car;
sb.append(c);
car = r.read();
s = sb.toString();
}
if(esCaracterEspecial(car))
{
position = r.getFilePointer();
r.seek(position-1);
}
}
}
catch(IOException ex)
{
System.out.println("Can not open file");
}
return s;
}
public static boolean specialChar(int car){
char character[] ={'+','-','*','/','%',';',',','{','}','(',')','=','<','>',':'};
for(int i = 0;i < character.length;i++){
if(car == character[i])
return true;
}
return false;
}
, но проблема заключается в том, что код не обращая внимания на «\ п» и печатает:
[процедура] [TeTruena] [является ]
почему это ??
Использовать BufferedReader и readLine – MadProgrammer
Вы в Windows? Убедитесь, что ваш персонаж не '\ r'. Текстовые файлы в Windows имеют как '\ r', так и' \ n' между каждой строкой, и если вы используете класс, предназначенный для двоичных файлов, вы должны беспокоиться об обоих. – ajb
'\ n' для Unix и Unix подобных систем. Используйте свойство system 'line.separator', чтобы сделать рабочую платформу независимой. – asgs