String s;
int k,l,j=0;
Scanner sc=new Scanner(System.in);
System.out.print("satrni kirit..s=");
s=sc.nextLine();
Scanner sc1=new Scanner(s);
while (sc1.hasNext()){
l=sc1.nextInt();
j=j+l;
}
System.out.println("bu sonlar yig;indisi "+j+" ga teng");
}
}
Satrlar oqimi bilan ishlaganda biror satrda ixtiyoriy miqdorda so’zlar kiritilgan bo’lsa bu satrda nechta so’z borligini aniqlab beradi
import java.io.*;
import java.util.*;
public class leo {
public static void main(String [] args) throws IOException{
String s,s1;
int k=0,l,j=0,i=0;
Scanner sc=new Scanner(System.in);
System.out.print("satrni kirit..s=");
s=sc.nextLine(); i++;
Scanner sc1=new Scanner(s);
k=0;
while (sc1.hasNext()){
s1=sc1.next();
k++;
}
System.out.println("satrda " +k+ " ta so'z bor ");
}
}
Endi shu dasturning kengroq ko’rinishi biror A fayl yaratamiz va bu faylga ixtiyoriy matn kiritsak bu matnning har bir satrida nechtadan so’z borligini aniqlab beradi
import java.io.*;
import java.util.*;
public class gg {
public static void main(String[] args) throws IOException{
Scanner sc=new Scanner(new File("a.txt"));
String s=" ";
int i=0,k=0;
while (sc.hasNext()){
s=sc.nextLine();k=0; i++;
Scanner sc1=new Scanner(s);
while(sc1.hasNext()){
s=sc1.next();
k++;
}
System.out.println(i+" - satrda "+k+" ta so`z bor");
}
}
}
2.2. JAVA DASTURLASH TILIDA FAYLLAR OQIMI USTIDA AMALLAR........
Fayllar va satrlar bilan ishlaganda ishlatiladigan ba’zi funksiyalar
Statistik qatorlar String klassi yordamida tuziladi literali yordamida avtomatik tuziladi va yana ” +” operatsiyasi qatorlarni birlashtirish uchun ishlatiladi.
Agar operandlardan bittasi qator bo’lmasa u avtomatik qatorga o’zgartiriladi. Shu obyektlarni ishlatish uchun toString() metodi oraqali amalga oshiriladi
Do'stlaringiz bilan baham: |