import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.io.*;
import java.util.Scanner;
private void saveFile(File file) //throws IOException
{
FileWriter fw = null;
try
{
fw = new FileWriter(file);
}
catch (IOException e)
{
System.out.println("Usage : java FileIOdemo <filename> " );
System.exit(0);
}
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter outFile = new PrintWriter(bw);
for (int i = 0 ; i < eng.returnRowCount() ; i++)
{
for (int j = 0 ; j < eng.returnColumnCount(i) ; j++)
outFile.print(booleanToString(eng.getCell(i, j)) + " ") ;
outFile.println();
}
outFile.close();
}
private void openFile(File file) //throws IOException
{
try
{
scan = new Scanner(file);
}
catch (IOException e)
{
System.exit(0);
}
String s1;
s1 = scan.nextLine();
String tmp[] = s1.split(" ");
for (int i=0; i < eng.returnRowCount(); i++)
{
s1 = scan.nextLine();
tmp[] = s1.split(" ");
for (int j=0; j < eng.returnColumnCount(0); j++)
{
eng.setCell(i, j, stringToBoolean(tmp[j]));
System.out.println (i + " " + j);
}
}
}
public static final boolean stringToBoolean(String str)
{
if (str.equals("0")) {
return false;
}
//if it's non false, it's true by definition
else
return true;
}
public static final int booleanToString(boolean bool)
{
if (bool)
{
int a = 1;
return a;
}
else
{
int a = 0;
return a;
}
}
}
Upon running this code to output, I get a 100x100 grid (0-99 x 0-99), which is correct.
However, when trying to take that back in as input, it processes up through row #98, column 99, and then throws this exception:
98 99 <--- this being the output generated by nested for loops to show which cell the loop is handling.
Exception in thread "AWT-EventQueue-0" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1471)
at GridPanel.openFile(GridPanel.java:252)
at GridPanel.access$700(GridPanel.java:8)
at GridPanel$ComboListener.actionPerformed(GridPanel.java:196)
at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1197)
at javax.swing.JComboBox.setSelectedItem(JComboBox.java:561)
at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:597)
at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(BasicComboPopup.java:808)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:232)
at java.awt.Component.processMouseEvent(Component.java:5554)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
at apple.laf.AquaComboBoxUI$AquaComboPopup$1.processMouseEvent(AquaComboBoxUI.java:443)
at java.awt.Component.processEvent(Component.java:5319)
at java.awt.Container.processEvent(Container.java:2010)
at java.awt.Component.dispatchEventImpl(Component.java:4021)
at java.awt.Container.dispatchEventImpl(Container.java:2068)
at java.awt.Component.dispatchEvent(Component.java:3869)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4256)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3936)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3866)
at java.awt.Container.dispatchEventImpl(Container.java:2054)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3869)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:176)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)