ورود

View Full Version : سوال: روش کار با Scanner



AVR programmer
یک شنبه 13 تیر 1395, 21:35 عصر
برنامه زیر یک خط اطلاعات از ورودی صفحه کلید میگیره و اونا رو در یک فایل txt ذخیره میکنه و دوباره می خونه و به کاربر نمایش میده

در برنامه زیر دو تا شی از کلاس Scanner تعریف کردم به نام های input و input2 که کار اولی گرفتن دیتا از کاربر هست و دومی جریان ورودی دیتا از فایل txt هست .

اما این برنامه کار نمی کنه مگر اینکه جریان ورودی input رو حذف کنم .

حالا میخوام بدونم چطور میشه این برنامه رو بدون مشکل نوشت ؟ کلا چطور میشه از کلاس Scanner دو تا شی تعریف کرد و هر دو رو بکار برد ؟
باتشکر.


package file_test_1;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Formatter;
import java.util.Scanner;


import javax.swing.JOptionPane;


public class test1 {
static Scanner input = new Scanner(System.in);


public static void main(String[] args) {
String word = "";
try {


System.out.print("Enter your data :");
word = input.nextLine();
} catch (Exception ex) {
System.out.print("can not read from keyboard");
}


create_file();
write_to_file(word);
read_file();


}


static Formatter file1;


public static void create_file() {
try {
file1 = new Formatter("f:\\test3\\pejman1.txt");
System.out.print("\nFile created successfully");
} catch (FileNotFoundException e) {
System.out.print("\ncan not create file !");


}
}


public static void write_to_file(String test) {
try {
file1.format("%s%d", test, 227654);
System.out.print("\nwritting is successful");
file1.close();
} catch (Exception ex) {
System.out.print("\ncan not write to file");
}
}


public static void read_file() {
Scanner input2;
try {
input2 = new Scanner(new File("f:\\test3\\pejman1.txt"));
while (input2.hasNext()) {
String a = input2.next();
JOptionPane.showMessageDialog(null, a);
}
System.out.print("reading is seccessful");
input2.close();
} catch (Exception ex) {
System.out.print("can not read file");


}
}


}

AVR programmer
یک شنبه 13 تیر 1395, 22:11 عصر
ببخشید یه اشتباهی شده
فقط این دوتا حالت در اجرا شدن توسط اکلیپس کمی فرق دارن !
در حالتی که فقط یک جریان ورودی از فایل txt داریم ّپنجره های مزبوط به JOptionPane بصورتی ظاهر میشن که روی صفحه اصلی برنامه میان و لی در حالتی که دو شی از کلاس Scanner تعریف میکنیم این پنجره ها پشت پنجره اصلی برنامه ظاهر میشن !

mohsensaeb14
جمعه 18 تیر 1395, 22:58 عصر
داداش برنامه ت کاملا درسته فقط برای formatter باید فایل رو اول تشکیل بدی
import javax.swing.*;import java.io.File;
import java.util.Formatter;
import java.util.Scanner;




public class test {
static Scanner input = new Scanner(System.in);




public static void main(String[] args) {
String word = "";
try {




System.out.print("Enter your data :");
word = input.nextLine();
} catch (Exception ex) {
System.out.print("can not read from keyboard");
}




create_file();
write_to_file(word);
read_file();




}




static Formatter file1;




public static void create_file() {
try {
File as = new File("/Users/mohsensaeb/Desktop/asddd/a.txt");
as.createNewFile();
file1 = new Formatter("/Users/mohsensaeb/Desktop/asddd/a.txt");
System.out.print("\nFile created successfully");
} catch (Exception e) {
System.out.print("\ncan not create file !");




}
}




public static void write_to_file(String test) {
try {
file1.format("%s%d", test, 227654);
System.out.println("\nwritting is successful");
file1.close();
} catch (Exception ex) {
System.out.print("\ncan not write to file");
}
}




public static void read_file() {
Scanner input2;
try {
input2 = new Scanner(new File("/Users/mohsensaeb/Desktop/asddd/a.txt"));
while (input2.hasNext()) {
String a = input2.next();
JOptionPane.showMessageDialog(null, a);
}
System.out.print("reading is seccessful");
input2.close();
} catch (Exception ex) {
System.out.print("can not read file");




}
}




}