8 Replies - 1597 Views - Last Post: 01 August 2011 - 06:05 PM Rate Topic: -----

#1 nowayymann   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 01-August 11

Multiple entry ArrayList<String>. Trying to sort into descendi

Posted 01 August 2011 - 03:46 PM

Class #1:
package order;

import java.util.Scanner;

public class Driver {

	/**
	 * @param args
	 * Sorts lines of String input in descending order
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ThreeLn link = new ThreeLn();
		ThreeLnArray Link1 = new ThreeLnArray();
		MultiLnArrayList Link2 = new MultiLnArrayList();
		
		Scanner input = new Scanner(System.in);
		link.line(" '0' for String. '1' for Array. '2' for ArrayList.");
		int a = input.nextInt();
		
		if (a == 0)
			link.mainLoop();
		else if (a == 2)
			Link2.mainLoop();
		else
			Link1.mainLoop();
	}

}


this is what "link.line" refers to:
	void line (String a) {
		
		System.out.println(a);
	}


and the class im having problems with:
import java.util.ArrayList;
import java.util.Scanner;

public class MultiLnArrayList {

	// variables
	ThreeLn link;
	ArrayList<String> a;
	Scanner input;
	String temp;
	int arraySize;
	
	//Sort Method
	void mainLoop() {
		
                // definition
		link = new ThreeLn();
		input = new Scanner(System.in);
		arraySize = Integer.parseInt(input.nextLine());
		a = new ArrayList<String>(arraySize);
		temp = "";
		
		// input
		for (int b = 0; b < arraySize; b++) {
			
			link.line(" Enter String #" +(b+1)+ ":");
			temp =  /*String.valueOf(input.nextLine());//*/ input.nextLine();
			a.set(b, temp);
		}
		
		// Sorting
		for (int b = 0; b < arraySize - 1; b++)
			for (int c = b+1; c < arraySize; c++)
				if (a.get(c).compareToIgnoreCase(a.get(B)/>)<0) {
					
					temp = a.get(B)/>;
					a.set(b, a.get(c));
					a.set(c, temp);
				}
		
		// display sorted
		for (int b = 0; b < arraySize; b++) 
			link.line(a.get(B)/>);
	}
}


my input was:
2
3
b
---------------------------------------------------
my errors were:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.set(Unknown Source)
at order.MultiLnArrayList.mainLoop(MultiLnArrayList.java:30)
at order.Driver.main(Driver.java:24)
-------------------------------------------------------------------------------------------
the entire console display at the end read:
'0' for String. '1' for Array. '2' for ArrayList.
2
3
Enter String #1:
b
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.set(Unknown Source)
at order.MultiLnArrayList.mainLoop(MultiLnArrayList.java:30)
at order.Driver.main(Driver.java:24)
------------------------------------------------------------------------------------
i dont understand what im doing worng exactly. why is the index out of bounds?

Is This A Good Question/Topic? 0
  • +

Replies To: Multiple entry ArrayList<String>. Trying to sort into descendi

#2 nowayymann   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 01-August 11

Re: Multiple entry ArrayList<String>. Trying to sort into descendi

Posted 01 August 2011 - 03:59 PM

line 28 is line 30 in the error
           a.set(b, temp);

Was This Post Helpful? 0
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Multiple entry ArrayList<String>. Trying to sort into descendi

Posted 01 August 2011 - 04:00 PM

Where is this error
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
occurs ? Which line in main() ?
Was This Post Helpful? 0
  • +
  • -

#4 nowayymann   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 01-August 11

Re: Multiple entry ArrayList<String>. Trying to sort into descendi

Posted 01 August 2011 - 04:06 PM

main() is in "Public Class Driver", line 11
Was This Post Helpful? 0
  • +
  • -

#5 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Multiple entry ArrayList<String>. Trying to sort into descendi

Posted 01 August 2011 - 04:50 PM

View Postnowayymann, on 01 August 2011 - 07:06 PM, said:

main() is in "Public Class Driver", line 11

You don't have a class named "Public Class Driver"
Was This Post Helpful? 0
  • +
  • -

#6 nowayymann   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 01-August 11

Re: Multiple entry ArrayList<String>. Trying to sort into descendi

Posted 01 August 2011 - 04:59 PM

look at the first code window thing. where it says "class #1"
Was This Post Helpful? 0
  • +
  • -

#7 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Multiple entry ArrayList<String>. Trying to sort into descendi

Posted 01 August 2011 - 05:08 PM

Line 11 is
public static void main(String[] args) {
this is surely not where the error occured

You will have to show us a correct stack trace
we won't waste our time for unproperly described problem :^:
Was This Post Helpful? 0
  • +
  • -

#8 nowayymann   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 01-August 11

Re: Multiple entry ArrayList<String>. Trying to sort into descendi

Posted 01 August 2011 - 05:15 PM

sorry, misinterpreted what you said. in the code statement box, where it says "public class MultiLnArrayList ()", on line 28 is where the error occurs.
Was This Post Helpful? 0
  • +
  • -

#9 nowayymann   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 01-August 11

Re: Multiple entry ArrayList<String>. Trying to sort into descendi

Posted 01 August 2011 - 06:05 PM

fixed it! i tried to set a value to an non-existing slot in the ArrayList. i forgot to use ".add". by the way, i was trying to ask the right way, but i didn't know how. I just wanted some patience..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1