java load file to textarea, thanks in advance
ok so the program has a load button that loads from a file to the test area(t).
now the problem is if i use the below code their is an extra empty line between each new line and if i take out the "\n" then the lines will all be loaded on one line
any help?
public class MyActionListener3 implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if(event.getSource()== Window.button3)
{
int num=0;
String str="";
String str2="";
int temp=0;
try
{
Scanner scan = new Scanner (new File("hardcode.txt"));
while(scan.hasNextLine())
{
num++;
scan.nextLine();
}
Scanner scan2 = new Scanner (new File("hardcode.txt"));
for(int i=0;i<num;i++)
{
str += scan2.nextLine();
}
Scanner scan3 = new Scanner (new File("hardcode.txt"));
for(int i=0;i<num;i++)
{
String s1 = scan3.nextLine();
int length = s1.length();
String line1 = str.substring(0+(temp),temp+length);
temp += length;
str2 += line1+"\n";
}
Window.t.setText(str2);
}
catch (FileNotFoundException e)
{
System.out.println("file not found exception");
}
}
}
}