Results 1 to 2 of 2
Thread: class problem
- 02-24-2009, 03:07 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 47
- Rep Power
- 0
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
I know I'm missing code in the main, but my main focus right now is figuring out what it wrong with the randomizing.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."); } } }
- 02-24-2009, 03:26 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
In your switchLines() method you swap two array entries. Or, at least you mean to.
Don't you mean theList[line2] rather than theLines(line2) in that middle assignment?Java Code:public void switchLines(int line1, int line2) { String middleMan = ""; middleMan = theLines.get(line1); theList[line1] = theLines.get(line2); theList[line2] = middleMan; }
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
-
problem calling function from class to class
By alin_ms in forum New To JavaReplies: 3Last Post: 12-19-2008, 07:35 PM -
Im new to java. how do i fix the problem with class
By lexlukkia in forum New To JavaReplies: 2Last Post: 11-18-2007, 04:47 AM -
Class problem
By Deagel in forum New To JavaReplies: 1Last Post: 10-31-2007, 07:33 PM -
Problem calling another class
By adlb1300 in forum New To JavaReplies: 3Last Post: 10-25-2007, 02:05 PM -
problem with date class
By gabriel in forum New To JavaReplies: 3Last Post: 08-03-2007, 01:28 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks