This site requires JavaScript, please enable it in your browser!
Greenfoot back
8bitcarrotjuice
8bitcarrotjuice wrote ...

2013/11/29

ArrayList out of index? (range 1, index 1)????

8bitcarrotjuice 8bitcarrotjuice

2013/11/29

#
I am working on an application in Eclpise and this is the only place I could think of where I could get help. Here is the code in the class affected:
import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
public class Main extends JFrame {
	Class newClass;
	JTabbedPane tab;
	JPanel panel1;
	JTabbedPane tabbedPane=new JTabbedPane();
	int numberStudents;
	String numberOfStudents;
    JPanel topPanel = new JPanel();
    public int classOffset=0;
    ArrayList<Class> newClasses=new ArrayList();
public Main()
	{
		super("Grading System");
        setSize(400, 500);
        setBounds(600,300,400,550);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel pane = new JPanel();
		topPanel.setLayout( new BorderLayout() );
		getContentPane().add(topPanel);

        newClass();
    	
        JToolBar bar=new JToolBar();
        JButton addClass=new JButton("class");
        addClass.setIcon(new ImageIcon(getClass().getResource("/plus.png")));   
        addClass.addActionListener(new ActionListener() { 
			  public void actionPerformed(ActionEvent e) { 
				    newClass();
				    //JOptionPane.showMessageDialog(null, String.valueOf(selected));
				  } 
				} );
        bar.add(addClass);
        pane.add("North",bar);
        add("North",bar);
        setVisible(true); 
		

    }
	
	private void FileManager(String string, String noOfStudents) {
		// TODO Auto-generated method stub
		
	}

	public static void main(String[] args) {
		Main main=new Main();

	}
	public void actionPerformed(ActionEvent e) { 
	    Main newClass=new Main();
	    JOptionPane.showMessageDialog(null, String.valueOf(213124));

	 } 
	public void newClass()
	{
		File f = new File("H:/marker");
    	f.mkdirs(); 
    	try { f.createNewFile(); } catch (IOException e) { e.printStackTrace(); }
        
        String buttonText[]={"Choose","Make"};
        int response = JOptionPane.showOptionDialog(null, "Do you wish to choose a class, or make a new one?", "Option Dialouge", 0, JOptionPane.INFORMATION_MESSAGE,null,buttonText,buttonText[0]);
        if(response==1)
        {       	
        	String nameOfClass = JOptionPane.showInputDialog(null, "What do you want to name this class?", JOptionPane.QUESTION_MESSAGE);
        	String noOfStudents = JOptionPane.showInputDialog(null, "How many students are in the class?", JOptionPane.QUESTION_MESSAGE);
        	FileManager("H:\\marker\\classes\\"+nameOfClass+"\\settings.txt",noOfStudents);
        	newClass=new Class("H:\\marker\\classes\\"+nameOfClass,nameOfClass,noOfStudents);
    		tabbedPane = new JTabbedPane();
    		tabbedPane.addTab( newClass.returnName(), newClass );
    		topPanel.add( tabbedPane, BorderLayout.CENTER );
    		setVisible(true);
    		add(topPanel);          
        }
        else if(response==0)
        {
        	FileManager fm = new FileManager("H:\\marker\\classes");
        	Class newClass = null;
        	newClasses.add(classOffset,newClass);
        	//newClasses.add(newClass);

        	while(newClasses.get(classOffset)==null)
        		{
        		if(fm.getState()==1){
        			classOffset+=1;
        		try {
					//numberOfStudents=new String(Files.readAllBytes(Paths.get("H:\\marker\\classes\\"+fm.getSelectedOption()+"\\settings.txt")));
				} catch (Exception e) {e.printStackTrace();} 
        		int temp=10;
        		newClass=new Class("H:\\marker\\classes\\"+fm.getSelectedOption(),fm.getSelectedOption(),temp);
        		fm.dispose();
    			tabbedPane.addTab( newClass.returnName(), newClass );
    			topPanel.add(tabbedPane, BorderLayout.CENTER);
    			setVisible(true);
    			add(topPanel);
        		}
        	}

        }
		
		
	}


}
This is the error message:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
	at java.util.ArrayList.rangeCheck(Unknown Source)
	at java.util.ArrayList.get(Unknown Source)
	at Main.newClass(Main.java:92)
	at Main.<init>(Main.java:32)
	at Main.main(Main.java:57)
Tell me if you need code from classes that work with this one. Thanks!!
8bitcarrotjuice 8bitcarrotjuice

2013/11/29

#
I got it sorted, but its very messy :P ( I make a Class array with 999 slots ) Would be appreciated if someone could look into this :) EDIT: also, I have a problem with my program, and if anyone would be able to help me with it, could you just tell me? Thank you in advance!
Gevater_Tod4711 Gevater_Tod4711

2013/11/29

#
The problem is that your ArrayList newClasses has only one field and you try to get the second field in your while loop. If you change the loop in line 92 to while(newClasses.size() > classOffset && newClasses.get(classOffset)==null) it should work I think.
8bitcarrotjuice 8bitcarrotjuice

2013/12/2

#
Tod would you also be kind enough to help me with a problem that I have with my program? If yes, at the end of this post, I will attach the project file that you can put into your workspace and run. The problem is that when you chose for the second time (when you open new class in the class gui) the program opens up a new file manager yet it shows a white screen. here is the file: https://dl.dropboxusercontent.com/u/67302738/Marking%20System.zip Thanks! (by the way, change the H: drive to C:, and delete the file 'marker' after you have finished with it :))
Gevater_Tod4711 Gevater_Tod4711

2013/12/2

#
When I start the project there is a IOException because the program can't find the path. So I think the project will not work correctly on my computer. Could you explain what is the problem? May I can help you without the project.
8bitcarrotjuice 8bitcarrotjuice

2013/12/3

#
I will post a version with the use of the C: drive... Give me a sec :)
8bitcarrotjuice 8bitcarrotjuice

2013/12/3

#
Here you go: https://dl.dropboxusercontent.com/u/67302738/project Basically, run the file 2 times, then choose a file in the main interface, and the window will freeze. I do not know how to fix this :(. Thanks!
8bitcarrotjuice 8bitcarrotjuice

2013/12/3

#
Also a problem is, that when you create a new class, there is ment to be a settings.txt file created as well. I think there is a supressed IO error, but I don't know how to fix it...
Gevater_Tod4711 Gevater_Tod4711

2013/12/3

#
When I try downloading the project I get this error: Error (404) We can't find the page you're looking for. Check out our Help Center and forums for help, or head back to home. Any ideas why this occours?
8bitcarrotjuice 8bitcarrotjuice

2013/12/4

#
http://speedy.sh/6JMMn/Marking-System Tried another hosting site :) And also I have a problem where it does not create the text file called 'settings.txt', and thus next time you load up a file it gives you a nullpointer due to no data being read... I made it so the data is not read from the file but uses a 7, just temporarily. I tried changing the FileOutputStream to a Scanner, applying filters, etc... I have no idea! D:
Gevater_Tod4711 Gevater_Tod4711

2013/12/4

#
Again this error. Not found. But I'll have a look at the file systems. Maybe I can find the problem in the version I already have.
8bitcarrotjuice 8bitcarrotjuice

2013/12/7

#
I just tried this project on my MAC, and it gives me an error when the object Class is initiated... I have no idea what the problem is...
You need to login to post a reply.