Results 1 to 2 of 2

Thread: class problem

  1. #1
    McChill is offline Member
    Join Date
    Feb 2009
    Posts
    47
    Rep Power
    0

    Default class problem

    I'm not sure which method is messed up, but I think it is either switchLines(...) or randomizeList()

    Whenever I run the program it displays the list at first correctly. Then the two println's"The lines have been.....". But whenever it displays the list after being randomized I get mized results. Sometimes the list will not have changed (or changed back to the same?), sometimes there will be multiples of the same line like this for example --
    This is the second line
    This is the second line
    This is the first line
    This is the fourth line

    Java Code:
    package test;
    import java.util.HashMap;
    import java.util.Random;
    import java.io.*;
    
    public class Test {
    	
    	private HashMap<Integer, String> theLines = new HashMap<Integer, String>();
    	private String theList[];
    	private Random random;
    	
    	
    	public Test() {}
    	
    	/**
    	 * Set the lines into a hashmap
    	 */
    	public void setLines()
    	{
    		theLines = new HashMap<Integer, String>();
    		theLines.put(0, "This is the first line");
    		theLines.put(1, "This is the second line");
    		theLines.put(2, "This is the third line.");
    		theLines.put(3, "This is the fourth line.");
    	}
    	
    	/**
    	 * Set the lines into an array
    	 */
    	public void setList()
    	{
    		theList = new String[4];
    		for(int x = 0; x < 4; x++)
    			theList[x] = theLines.get(x);
    		
    	}
    	
    
    	public String[] getList()
    	{
    		return theList;
    	}
    	
    	/**
    	 * A function to switch two lines.
    	 * @param line1 The line to be switched
    	 * @param line2 The line to switch it with
    	 */
    	public void switchLines(int line1, int line2)
    	{
    		String middleMan = "";
    		middleMan = theLines.get(line1);
    		theList[line1] = theLines.get(line2);
    		theList[line2] = middleMan;
    	}
    	
    	/**
    	 * The function to randomize the lines. 
    	 * loop it a random number of times
    	 * when a and b are not equal, switch the lines
    	 */
    	public void randomizeList()
    	{
    		random = new Random();
    		int x = random.nextInt();
    		while(x == 0)
    			x = random.nextInt();
    		for(int t=0;t < random.nextInt();t++) {
    			int a = random.nextInt(4);
    			int b = random.nextInt(4);
    			while(a == b) {
    				a = random.nextInt(4);
    				b = random.nextInt(4);
    			}
    			switchLines(a, b);
    		}
    	}
    	
    	//Displays the list
    	public void displayTheList()
    	{
    		for(int x = 0;x < 4;x++)
    			System.out.println(theList[x] + "\n");
    	}
    	
    	
    	
    	
    	
    	public static void main(String args[]) {
    		Test t = new Test();
    		String temp = "";
    		t.setLines();		
    		t.setList();		
    		t.displayTheList();
    		t.randomizeList();
    		System.out.println("The lines have been randomized.\n");
    		System.out.println("It is up to you to put them back in order.\n");
    		t.displayTheList();
    		System.out.println("Type in switch to change the order");
    		BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
    		try{
    			temp = dataIn.readLine();
    			if(temp.equals("switch"))
    			{
    				System.out.println("Okay, what two lines should be switched?");
    				String which = dataIn.readLine();
    
    				//under construction...	
    					
    				System.out.println("\n\nThe new order is:\n");
    				t.displayTheList();
    						
    			}
    		}catch(IOException e) {
    			System.out.println("Error in getting input.");
    		}
    				
    
    	}
    
    }
    I know I'm missing code in the main, but my main focus right now is figuring out what it wrong with the randomizing.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    In your switchLines() method you swap two array entries. Or, at least you mean to.

    Java Code:
    public void switchLines(int line1, int line2)
    {
        String middleMan = "";
        middleMan = theLines.get(line1);
        theList[line1] = theLines.get(line2);
        theList[line2] = middleMan;
    }
    Don't you mean theList[line2] rather than theLines(line2) in that middle assignment?

    BTW - you don't really need to assign middleMan an empty String like that, you can just say:

    Java Code:
        String middleMan = theLines.get(line1);

Similar Threads

  1. problem calling function from class to class
    By alin_ms in forum New To Java
    Replies: 3
    Last Post: 12-19-2008, 08:35 PM
  2. Im new to java. how do i fix the problem with class
    By lexlukkia in forum New To Java
    Replies: 2
    Last Post: 11-18-2007, 05:47 AM
  3. Class problem
    By Deagel in forum New To Java
    Replies: 1
    Last Post: 10-31-2007, 08:33 PM
  4. Problem calling another class
    By adlb1300 in forum New To Java
    Replies: 3
    Last Post: 10-25-2007, 03:05 PM
  5. problem with date class
    By gabriel in forum New To Java
    Replies: 3
    Last Post: 08-03-2007, 02:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •