0
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    

3 Answers3

0

My current problem is that it isn't accepting the input.next(); into the arrays for names[] and slices[]. How can I fix this?

while (input.hasNext()) { should probably be while (input.hasNextLine()) {

You may then need to use input.nextLine() to get the actually text and use a second Scanner to parse the individual line

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.

You should be passing the arrays to your PieChart class, maybe via the constructor.

You should also be calling super.paintComponent before doing any custom painting

It is giving me a NullPointerException error and it says it's on line 16 which is "names[i] = input.next();"

names is never initialised (is null)...

String names[] = null;
int slices[] = {};

Arrays are not dynamic, so unless you know in advance how many lines there are, it's not particular easy to manager.

In you case, assuming that the file doesn't change, you could use something like...

String names[] = new String[5];
int slices[] = new int[5];

Another choice would be to use a List of some kind or a Map, which are dynamic, so you don't need to know how many elements you might need before hand.

See Collections Trail for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

You should use an array if you know before you start adding items what the size of it will be (e.g. it will have 20 indices). Here, it seems that you don't know until runtime. So an array would not be the best data structure for this. Consider an array list instead.

ArrayList<String> names = new ArrayList()<String>;
while(...){
   names.add(input.nextLine());
}
System.out.print(names.get(3)) //prints the 4th name
Community
  • 1
  • 1
faraza
  • 416
  • 9
  • 21
0
String names[] = null;

You need to assign a non-null array to name before you can assign values to its elements; otherwise you will get the NullPointerException you observe.

However, you don't know how many elements you are going to need, so you should use a resizable data structure like an ArrayList.

Had you assigned {} to names like you did for slices, you'd get an ArrayIndexOutOfBoundsException, because it has no elements, but you are trying to assign a value to the zeroth one.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243