import javax.swing.*;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
File file = new File(args[0]);
Scanner input = new Scanner(file);
int i = 0;
String names[] = null;
int slices[] = {};
while (input.hasNext()) {
names[i] = input.next();
slices[i] = input.nextInt();
i++;
}
JFrame f = new JFrame("Pie chart");
f.setSize(600, 350);
f.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
f.add(new PieChart());
f.setVisible(true);
}
}
Here is my second file...
import java.awt.*;
import javax.swing.*;
public class PieChart
extends JComponent {
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
Graphics2D g3 = (Graphics2D) g.create();
g3.setColor(Color.BLACK);
g2.setColor(Color.BLUE);
for (int i = 0; i < 5; i = i + 1) {
g2.fillRect(230, 20 * i + 50 , 20, 20);
g3.drawString("swallow", 255, 20 * i + 65);
g3.drawString("37.0%", 385, 20 * i + 65);
}
g2.fillArc(50, 50, 150, 150, 0, 360);
}
}
I am trying to read from a text file into two separate arrays. I want one array to hold the names and then one to hold the values. I then want to be able to then access the values from the array from my second file. My current problem is that it isn't accepting the input.next(); into the arrays for names[] and slices[]. How can I fix this?
Here is a sample text file:
swallow 10
magpie 5
fairywren 7
osprey 2
fantail 3