Formatting Array List?

Oct 16, 2014

How do you format an arraylist?

Mine looks like this:

[<?xml version="1.0" encoding="UTF-8" standalone="no"?> <DefEnv id="Dev">, <Envt id="Test">, , <DB id="DM">,

But I want it to look like: I'd prefer if the '[' , '<>' and ',' were not on them also but I'm not too bothered about that bit.

[<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<DefEnv id="Dev">,
<Envt id="Test">, ,
<DB id="DM">, ]

View Replies


ADVERTISEMENT

Formatting The Way Array Information Is Displayed

Mar 21, 2015

import java.util.*;
public class TrafficIncidents {
public static void main(String[] args) {
Scanner s = new Scanner (System.in);
// Array for days
String [][] days = {{"Sat"},

[Code] ....

This is my output:

Day AM PM
--------------------------------
Sat
Sun
Mon
Tues
Wed
Thurs
Fri
5 1 1 2 4 1 0

Basically I want to align the AM traffic incidents under the heading "AM" but am not sure how to accomplish this. I am using separate arrays for all three of these types of information and need the info to line up with their respective titles.

View Replies View Related

Cannot Read Into Array List

Apr 5, 2014

I am creating a hangman game and I want to read in a list of words from a text file, but after the user inputs the name of the text file. I get 'Exception in thread "main" java.lang.NullPointerException'.

Here is the code, where I think the problems lie.

public void runModel(){
ArrayList<String> pirateWordsList = new ArrayList<String>();
System.out.println("What is the name of the file you would like to load? (The file included is called piratewords.txt'");
Scanner in=new Scanner(System.in);
String file=in.next();
load(file);

[Code] ....

The full error message is this:

Exception in thread "main" java.lang.NullPointerException
at uk.ac.aber.dcs.pirate_hangman.Model.load(Model.jav a:108)
at uk.ac.aber.dcs.pirate_hangman.Model.runModel(Model .java:45)
at uk.ac.aber.dcs.pirate_hangman.Main.main(Main.java: 6)

View Replies View Related

Highest Value In Array List?

Feb 2, 2015

I have 3 classes Pet, Cat and Dog classes. Cat and Dog classes are childs of Pet class.

Each pet has the properties of

private String myName;
private int myX;
private int myY;
private int mySpeed;
private int myAge;

I have an array list which creates 100 dogs. How can I print out the dogs with the two highest age values?

View Replies View Related

Alternatives To Array List?

Nov 23, 2014

Im trying to create a program in which I read line by line the contents of a text file, and then report each letter along with its frequency. I was wondering how to read through the lines and process it so that my program knows to increase by a number each time a letter appears in my text file. For example, if A appears in my file 5 times, B 3 times, and C 0 times I want to eventually print out

A -- 5
B-- 3
C-- 0

My first thought was to do this using array lists but is there any way I could do this without using one?

View Replies View Related

Why Array List Is Needed

Apr 28, 2015

method called []getLetterGrades but the only hint My professor told me was that I needed to declare another array list for this method and he wouldnt tell me anything else so bummer. But I don't understand why if what we are returning is a char. It would make sense to return an array list of char to get letter grade. Which is what i did but since the function is a char, the array list character wont work as a return.Primarily i would like to know the type that is needed. I just want an explanation for an array list in this method and how it would serve in this method.

import java.io.File;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

[code]....

View Replies View Related

Sort Both Array List

Jun 29, 2014

Directions: public static void initialize(ArrayList names, ArrayList sores)

You should write a function that sorts both array lists, based on the values in the scores array list. This is one of the more conceptually challenging parts of the assignment. You want to sort the scores array list, and simultaneously alter the names array list so that the names continue to line up with their respective scores by index. In the example data above, when the score 9900 moves to the first element of the scores array list, the name "Kim" should also be moved to the top of the names array list. The function should have the following signature:

I'm having trouble figuring out how to sort the lists.

import java.util.ArrayList;
import java.util.Scanner;
public class Assignment5
{
/**
*/
public static void main(String[]args) {
intializeArrays();

[Code] ....

View Replies View Related

Array List And Method

Mar 28, 2014

I had to write a program that prompts the cashier to enter all prices and names, adds them to two arrays lists, calls the method that I implemented, and displays the result and use 0 as the sentinel value. I am having difficulty coming up with a for loop in the method, I believe I have the condition right but I am having trouble with the statements. I now know that String does not have the get property, but I have only done examples with integers and I am not very good with for loops and wouldn't know how to fix it.

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
ArrayList<Double> sales = new ArrayList<Double>();
ArrayList<String> names = new ArrayList<String>();
System.out.print("Enter Number of Customers");
double salesAmount;
System.out.print("Enter Sales for First Customers");
salesAmount = in.nextDouble();
while(salesAmount != 0)

[code]....

View Replies View Related

Removing Duplicates In Array List

Sep 18, 2014

I am stuck on this exercise and I don't know what exactly is wrong. I think it's something with the .remove and the for each loop, but I am not sure.

public class seven {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("aaa");
list.add("brr");
list.add("unni");

[Code] ....

This is what i get

Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at seven.removeDuplicates(seven.java:24)
at seven.main(seven.java:18)

View Replies View Related

Enum Type And Array List

Feb 10, 2015

Here I have an enum class

public enum Money{

ONE_PENNY(1),
TWO_PENCE(2),
FIVE_PENCE(5),
TEN_PENCE(10),
TWENTY_PENCE(20),
FIFTY_PENCE(50),
ONE_POUND(100),
TWO_POUNDS(200);

private int coin;
Money(int c) {
coin = c;
}
int showCoin() {
return coin;
}

and for a test class, I need an array list with a couple of coins in it (i.e. ONE_POUND, TWO_POUNDS) and a loop that adds together the values of the coins in the list and prints the result. How can I do this?

View Replies View Related

Input From Console To Array List?

Dec 10, 2014

I have this very simple application just to test console input:

import java.util.ArrayList;
import java.util.Scanner;
public class WriteTester {

[Code]....

When I let it run, only every third entry is put into the array list and I have to hit "enter" three times for the "break" in line 21 to trigger. I cannot find out why.

View Replies View Related

Adding Array To A List - String?

Sep 8, 2014

Im making a simple code to add an array to a List (the code im referring to is <String> )

import java.util.*;
public class L5_ArrayListProgram {
public static void main(String[] args){
String[] things = {"lasers","ghouls", "food", "dark"};
List<String>list1 = new ArrayList<String>();
for(String x: things)
list1.add(x);

My simple question is - what are the <String> ...<String> for? I understand it makes the list1 variable a string, but why is it made like this? do we usualy use <String> when we need to make a variable a String?

View Replies View Related

Convert Integer List To Int Array?

Sep 24, 2008

is there a way, to convert a List of Integers to Array of ints (not integer). Something like List<Integer> to int []?

View Replies View Related

How To Use JOptionPane To Search Array List

Jun 14, 2014

My assignment is to create an array list and compare the total salary of two salespeople. I'm wondering if I can use JOptionPane to select an existing sales person from the list and print their information.

I am not having any trouble with the calculations and comparisons, but I am finding limited resources on "searching" for a specific person with JOptionPane.

Here's what I have so far.

public class SalesPeople {
String personName;
double annualSalary;
double salesAmount;
double percentComission;
public SalesPeople(String xPersonName, double xAnnualSalary, double xSalesAmount, double xPercentComission) {

[code]....

View Replies View Related

Sort Array List Print?

Nov 23, 2014

I am having an issue trying to print different types of arrays using one method. Eclipse tells me to convert my print method for each array type:

change method 'printList <e>' to 'printList(ArrayList<integer>)'
change method 'printList <e>' to 'printList(ArrayList<Double>)'
change method 'printList <e>' to 'printList(ArrayList<Character>)'
change method 'printList <e>' to 'printList(ArrayList<String>)'

I would like to accomplish printing all four different arrays using one print method. How to accomplish it or provide examples or links to examples?

import java.util.ArrayList;
public class SortArrayList {
public static class Sort {

[Code].....

View Replies View Related

Array List Scrolled In Applet

Jan 6, 2015

I have to make an applet that uses an array to list 5 songs and the applet has to scroll the list of song titles, one at a time, each song title should start at the top of the applet and scroll to the middle then, scroll off the right hand side, each new song title should scroll in a different color, the applet should loop, when it gets to the end of the list, start over again at the beginning of the list. the code compiles fine put when I go to run the applet nothing shows up in it and a wall of text appears in the command prompt.

This is the code for the html

<html>
<body>
<applet code = "scroll.class" width=250 height=250>
</applet>
</body>
</html>
]

And this is the code for the program :

import java.awt.*;
import java.util.Random;
import java.awt.Color;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
public class scroll extends java.applet.Applet implements Runnable

[Code] ...

View Replies View Related

Char Array To List Not Working

Mar 30, 2014

Example code that works:

Java Code: List<String> list = Arrays.asList(strDays); mh_sh_highlight_all('java');

My code that doesn't work:

Java Code: List<char> list = Arrays.asList(userInputChars); mh_sh_highlight_all('java');

Error that I get:

Assignment6_2.java.java:45: error: unexpected type

List<char> list = Arrays.asList(userInputChars);
^
required: reference
found: char

1 error

The entire program (that works, except for the error above):

Java Code:

import java.util.Collections;
import java.util.List;
import java.util.Arrays;
import java.util.Scanner;
class Assignment6_2 {
public static void main(String[] args) {

[Code] .....

View Replies View Related

Searching Through Array (List Of Names)

Oct 5, 2014

An array which contain a list of names. I let for a user input and I use that to search through the array. For example lets say the string name "Christian" is stored inside the names array and the user types in Chri, the program looks in the array and finds a name that contains Chri and prints them out. How do I go about doing this?

View Replies View Related

Array List Requiring Variable Not A Value?

Dec 11, 2014

The IDE says an error of "required variable, found value". I thought you need a index value to get a item in the arraylist.

package SuperHero;
import java.util.ArrayList;
public class Driver
{
public static void main ( String args[]) {
//intallizing the supers

[Code] ....

View Replies View Related

Drawing Rectangles From Array List

Nov 13, 2014

The program is where you will click anywhere in the jframe and it will draw a new rectangle. I have altered the code given to us but the problem that I am having is the fact that when I click the rectangle will always be connected to the upper left hand part of the jframe, no matter where I click. The program should create rectangles of the same length and width wherever you click. Here is what I have. I believe I have to change something in the addRectangle method, but nothing that I have tried works.

Java Code:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.JComponent;
import java.util.ArrayList;
public class RectangleComponent2 extends JComponent

[Code] .....

View Replies View Related

Array List That Contain String Object

May 5, 2014

Suppose that you have an ArrayList and that it contains String objects. Which declaration of the ArrayList requires that objects retrieved using the get method be cast to Strings before calling a String method?

I. ArrayList a = new ArrayList();
II. ArrayList<Object> a = new ArrayList<Object>;
III. ArrayList<String> a = new ArrayList<String>;

A. I only
B. II only
C. III only
D. I and II only
E. I, II, and III

I know that all of these are ways to declare an Array List, but I am unfamiliar with the last two since I usually just declare my Array Lists with the first option.

View Replies View Related

Associating Properties With Array List Elements?

Dec 17, 2014

In my project I need to create a pizza ordering system. I have created a array list of pizza toppings and each pizza topping must have a cost property associated with it, then the user can pick the toppings they want and the cost of each topping will be added up and the total cost of the whole pizza will be displayed. But I don't know how to associate a cost with each topping. I have been told that I can access the cost through a get/set method and store it in a private member variable. But I don't know how to do this?

I've tried to do it, but it hasn't worked. This is the code

public void setCost( double cost){ //my get/set method
this.cost = cost;
}
public double getCost(){
double price = 0;
return price;

[Code] .....

But there is an error on each of the ingrediantCost method lines, saying

The primitive type double of cost does not have a field Pepperoni

...and the same for chicken, ham and onion.

View Replies View Related

Reusing Entered List Of Array Elements

Sep 11, 2014

If you see in the code below where I print "Would you like to use previous key" I need to be able to save the previous key answers to be used again.. not sure how to go about doing this..

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String[] studentName = new String[20];
int[] studentAverage = new int[20];
Scanner input = new Scanner(System.in);

[Code] .....

View Replies View Related

Count Occurrence Of A String In Array List?

Jan 31, 2014

How to count occurrence of a string in a string array list. I have 800,000 strings in string list..

View Replies View Related

Removing Brackets From Array List Printout

Apr 10, 2009

When you "system.out.print(arraylist)" an arraylist, it will give you something like [item1, item2].

Im wondering how I can remove the "[" and "]" brackets fromt he printout, or even if i pass it in as another variable.

View Replies View Related

Printing Random Values From Array List

Jun 15, 2014

I am having a hard time trying to figure out how to print random numbers from a an array list. I tried google but nothing worked. I have to pick certain values from two lists and print them on the screen. I have included comments in the code to facilitate the explanation.

import java.util.Random;
public class Parachute {
public static void main(String[] args) {
Random randomNumbers=new Random();
int number;
int array []={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21};
char A[] = {'a', 'b', 'c','d','e','f','g','h', 'i','j','k','l','m','n','o','p','q'};

[Code]...

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved