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

2022/12/4

how to place a object down in order as it reads a text file

Ilovemarcus Ilovemarcus

2022/12/4

#
I cant figure this out and its bothering me. I have a assignment where i have to sort a bar but i am struggling with placing the bars in order. I can place it but it is either out of order or half of the bar is out of the world. I really need help with this. i need help with much more but this is the main thing bothering me. here is the code
import greenfoot.*;
import java.awt.FileDialog;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


/**
 * Write a description of class MyWorld here.
 * 
 * @author Marcus Jones 
 * @version 12/4/22
 */
public class MyWorld extends World
{
    
    public static void swap(int inArray[], int index1, int index2) 
	{
		int temp = inArray[index1];
		inArray[index1] = inArray[index2];
		inArray[index2] = temp;
	}
        
    public static int findMinIndex(int arr[], int fromIndex)
	{
		int minIndex = fromIndex;
		
		for(int searchIndex=fromIndex+1; searchIndex<arr.length; searchIndex++)
		{
			if (arr[searchIndex] < arr[minIndex])
				minIndex=searchIndex;
		}
		
		return(minIndex);
	}
	
    public static void sort(int arr[])
	{
		for (int targetIndex=0; targetIndex<arr.length; targetIndex++)
		{
			int minPos = findMinIndex(arr, targetIndex);
			swap(arr, minPos, targetIndex);
		}
	}
	
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        super(800, 600, 1); 
    }
    
    public void act()
    {
        int delayCount = 0;
        if(Greenfoot.isKeyDown("l"))
        {
            prepare();
        } 
        
        if (Greenfoot.isKeyDown("N"))
        {
            
        }
        
        Boolean autoSort; 
         if (Greenfoot.isKeyDown("s"))
        {
            
            autoSort = true;// Remember to automatically sort.
            //delayCount = 250/barArray.length; // Delay value should be as calculated.
            //sort(values);
        }
    
        
    }
    
    public void prepare()
    {
        /**
         * Took the fileDialog from Lab6
         */
        FileDialog fd = null;
        fd = new FileDialog(fd, "Title for the GUI", FileDialog.LOAD);
        fd.setVisible(true);
 
        String filename = fd.getFile();  //what
        String path = fd.getDirectory(); //where
 
        String fullName = path + filename;
        
        File myFile = new File(fullName);
        Scanner reader;  // declare
        
        try  // attempt the following code
        {
            reader = new Scanner(myFile); // attempt to assign
        }
        
        catch(FileNotFoundException e)  // I know there might be a problem, I'm gonna catch it
        {
            System.out.println("User error on read file!");
            System.out.println(e);
            return;
        }
        
        int size = (int) reader.nextInt();
        int[] values = new int[size];
        int x3 = 800;
        int x1 = 800/size;
        int y1 = 600;
        
        for(int index = 0; index < size; index++)
        {
            int value = reader.nextInt();
            values[index] = value;
            
            int x2 = x1;
            x3 -= x2;
            int y = 600/size;
            int x = (int) values[index];
            //SortingBar bar = new SortingBar(x, y);
            addObject(new SortingBar(x,y),x3,y1);
        }
    
        fd.dispose();
        reader.close();
    }
}
danpost danpost

2022/12/4

#
One problem may be that you are using "Greenfoot.isKeyDown()" in your code. None of the actions of your key inputs should be done more than one time per request. However, when pressing a key, several act steps will occur before the key is released; therefore, the action is repeatedly done until the key is released (when using "isKeyDown"). Should use "Greenfoot.getKey()" to register key inputs rather than looking at the condition of the key (which is what 'isKeyDown' does). You can index the input in a concatenated string of possible inputs ( "lNs" ) using "indexOf"; and, on that value perform the specific task (using a switch statement here would not be a bad idea). Something like this:
String key = Greenfoot.getKey();
if (key == null || key.length() != 1) return;
switch ("lNs".indexOf(key))
{
    case 0:
        removeObjects(getObjects(SortingBar.class));
        prepare();
        break;
    case 1:
        break;
    case 2:
        // sort(values);
        break;
}
I took the liberty of adding in line 6. You may have been getting false reading due to excess SortingBar actors in the world (since the prepare method was being executed several times. It looks like you are adding your SortingBar objects into your world from right to left. That seems counter-intuitive. Should not they be going from left to right? Maybe it does not matter as you have no values showing to give them a particular direction. Also, you should add, or subtract in your case, half of x1 to x3 to center the actors in their particular column.
Spock47 Spock47

2022/12/5

#
After danpost already did all the hard work and presented a good solution, I just want to add one small detail: Since JDK7, you can use a String value directly with switch, e.g.
final String key = Greenfoot.getKey();
if (key == null) { return; }
switch (key) {
    case "l":
        removeObjects(getObjects(SortingBar.class));
        prepare();
        break;
    case "N":
        break;
    case "s":
        // sort(values);
        break;
}
You need to login to post a reply.