I have this piece of code, which adds marks into 2 different arrayLists, one for homework marks, and one for examination marks..
ArrayList<Double> homeworkMark = new ArrayList<Double>();
ArrayList<Double> examinationMark = new ArrayList<Double>();
boolean markCheck = true;
do{
//
the purpose of this try catch is to make sure that the entered mark is a valid number. If the program encounters an exception markCheck will become true and the loop will begin again asking the user to enter the marks. If the marks are all encountered correctly markCheck will be false and the loop will end.
try{
for (int i = 1; i <= amountAssignment; i++ ) {
sc.reset();
System.out.print("Homework Mark for Assignment " +(i) + ": ");
homeworkMark.add(sc.nextDouble());
System.out.print("Examination Mark for Assignment " + (i) + ": ");
examinationMark.add(sc.nextDouble());
markCheck = false;
import javax.swing.*; //import javax swing package import java.awt.*; //import java awt package import java.awt.event.*; //import java awt event package public class JPizzaSize extends JFrame implements ActionListener //class name { final int SMALL = 7; final int MEDIUM = 9;
[code]....
My variable numClicks counts all of the clicks on every button instead of each individual button and adds the cumulative total to the JTextField. What have I missed? If I click button1 twice, $14 appears in the text field, then if I click button2, $27 appears in the text field instead of $23. I though that numClicks would stop counting at the end of the curly bracket for that block of code?
I wrote code to remove question marks and exclamation points from some text. However, one can find those in my output file. Interesting enough, the periods (full stops) were removed.
import java.util.Scanner; public class TextTokenizer { private Scanner scanner; private String[] removableCharacters = { ".", "?", "!", ":", ";", """, "'" }; public TextTokenizer(char[] texts) { scanner = new Scanner(new String(texts));
I'm new to Java, & am using Eclipse Helios. How do I change the text colour within the Quotation marks "Try Again" to red, or any other colour, so when I run it, it will display the new colour.
I'm trying to make a program that will count each letter like if I put the word
e-1 h-1 l-2 o-1
The problem I'm running into is that if I put something like How now, brown cow? It should give me something like this
b - 1 c - 1 h - 1 n - 2 o - 4 r - 1 w - 4
but it doesn't instead the whole program crashes because of the punctuation marks and the spacing. Is there a way for me to fix that? All I know is that it has something to do with the alphabet[pos(letter)]++;
import java.util.Scanner;
public class CharCount { static Scanner keyboard = new Scanner(System.in); //this method should allow the user to enter a line of text (that should all be lower case) and then go to the printResults method public static void main(String[] args) { String text, character; int count; char letter; int[] alphabet; System.out.println("Enter a line of text:"); text = keyboard.nextLine().toLowerCase();// any text entered will be lower case alphabet=countLetters(text);
public static void main(String[] args) { Scanner Keyboard = new Scanner(System.in); System.out.print("Please enter your name."); String name = Keyboard.next(); name = "name";
I have to make ask for 2 houses or more (INSTANSED CLASS, with 2 ints ) with the same size(int ), but i they cant use the same door number(int). how do i check in my ArrayList that they have different numbers while not caring about different size? .
The ArrayList is in a class called system. I dont know if i should try to Override the equal or try something else.
>Suppose that aList and bList are instances of java.util.ArrayList. Use two iterators to find and display all the objects that are common to both lists. Do not alter the contents of either list. Write the segment of code assuming that the objects are of type String.
If my understanding is correct, I need to create two Iterators (one for aList and one for bList) to compare both ArrayLists and I should output any value that appears in both lists.
This is my attempt at it:
import java.util.ArrayList; public class Test { public static void main(String[] args) { ArrayList<String> aList = new ArrayList<String>(); ArrayList<String> bList = new ArrayList<String>();
aList.add("One"); aList.add("Two");
[Code] ....
The output:
`One`
`Two`
It stops there. It's clear that my loop is wrong, but I don't really know how to go about fixing it.
I have two arraylists. One is personalContactList. The other is businessContactList.
1. I want to take user input that references an object attribute (int), 2. Use it to determine which object is referenced, 3. Find the "type" attribute of that object, 4. Determine which arraylist the object belongs to, based on that type, 5. Use the if else/statement to print out some attributes that depends on which arraylist the object is in.
I believe the code successfully does 1-3, and probably 4. But there is a hangup on 5. I get an indexoutofbounds execption. Which I'm not sure I understand very well.
Here is the code--it's part of a switch statement:
case "c": { //some code here prints out a list boolean b = false; int g; while (b == false) { Scanner detailScanner = new Scanner(System.in); System.out.print("Enter a number to see details: ");
I'm new to java and up until now whenever I have created an object it has been via the BlueJ interface. I would make an object manually and then manipulate it with my methods.
I have made a basic program which adds objects into arraylists via the BlueJ interface. However I now need to prompt a user to enter a string which should create the specific object and add to its ArrayList.
Is there a way to create objects without manual specifying the parameters? I can provide some of my code if it is needed I am trying to make a crude version of Plant vs Zombies for an assignment.
I am aware of the scanner class but I only know how to prompt a user but I feel like if I can figure a way to create the objects I can dynamically I can associate the string inputs to the proper arraylists.
I have two ArrayLists and I want to compare them for common elements, and based on the result I want to update the first Arraylist to only have these elements. sort of like the opposite of RemoveAll() which removes elements in common and keep the ones that are unique. so far I thought of using for loop and .contains() in case it was fault,element not present, remove from list. but I was wondering in what other ways, perhaps APIs i can use to do that?
So I am learning HashMaps/Arraylists and I can't really understand the diamond operator or what it's for. Take the following code for example: Why could we not just do this without the diamond?
Java Code:
import java.util.HashMap; class Untitled { public static void main(String[] args) { HashMap<String, String> map = new HashMap<String, String>();
So I have a program where you can log in and add/remove friends to and from the friends arraylist. Also I can like a certain thing and that thing will be stored into the likes arraylist. I'm asked to make undo and redo options for whichever action I do.I want to add apple as a friend. After that when I select the undo option, I can undo that action so apple wouldn't be my friend. How I can approach this with a Command Pattern when the input is whatever name or word I inputted to store into the friends arraylist?
I did some research and found that using a command pattern could be my best bet since this has to be done under the Facebook Class I already have. I decided to add parts of what I have ...
In the driver program
Facebook facebook1 = new Facebook();
if (userInput == 6) { System.out.println("Login"); String operand1 = getOperand("What is the Username? "); String operand2 = getOperand("What is the Password? "); System.out.println("Enter a friend to be added. "); String operand3 = getOperand("What is the Username? "); facebook1.friend(operand3);
I have been doing a program that allows the user to input students names and numerical grade values. I have already completed 3/4 of the program but am stuck on the bubblesort method as this is what it requires:
The program must have an indefinite loop, which prompts the user to select a sorting criterion or to end the program, and must also use bubble sort. Note: The user may either select name or grade as the sorting criteria. The program must use bubble sort to sort that data according to the specific criteria and then use another loop to display the data. This process must continue until the user ends the program.
My problem is, while i can easily do the bubblesort, where to look on how to link elements of the two arraylists I'm using (one for grades, one for names) so that when the user decides what sort they want, the individual's name and grade stays together.
[URL] I made a program that takes 2 stock values by URL tickers, and now I need it to draw a graph with the values for the 2 stocks, so they can be compared visually. So far my idea was to create 2 arraylists that consist of the values for each stock, so that I can draw the graph so that x always move with 1 step, and y0 is the first coordinate, and y1 is the second, and after that y1 is the first, y2 second etc.
I have come across an issue with arraylists. I am writing a text based RPG game as something to start with ...
Initially I had a single zone which was all stored in an arraylist and everything was working in regards to the player moving around. The problem I now have is how to add further zones to my game. Ideally I would like an arraylist for each zone, and would use the below to create each arraylist
public static ArrayList<RoomsClass> castleMap = new ArrayList<>();
The problem I now have is how to handle the player moving, initially with a single zone/arraylist I could reference that arraylist directly
public void findRoomCoords(int ID) { for (int i = 0; i < castleMap.size(); i++) { if (castleMap.get(i).roomID.equals(ID)) { PLAYER.setCurrentRoomZone(castleMap.get(i).roomZone); PLAYER.setCurrentRoomX(castleMap.get(i).roomX); PLAYER.setCurrentRoomY(castleMap.get(i).roomY); PLAYER.setCurrentRoomZ(castleMap.get(i).roomZ); } } }
My initial thought was to use a getter/setter to remove the reference of castleMap from my movement code in order to access different arraylists, however this is where things have fallen over, I can't seem to work out how to get the arraylist name to change, depending on the outcome of the setZoneMap() method.
public void setZoneMap() { switch (PLAYER.getCurrentRoomZone()) { case 0: { zoneMap = Castle.castleMap; break;
I have an issue with an IndexOutOfBoundsException. I am trying to populate a db from another using arraylists and arrays. I can get the data but the program fails when trying to run the inserts. I am trying to perform SQL in batches of 5. I have added a comment to the failing line.
/*Set in code at beginning*/ ArrayList<String[]> privacyList = new ArrayList<String[]>(); ArrayList<String[]> statementBuffer = new ArrayList<String[]>();
/*Some sql is performed and the following String array is populated*/ while (rs.next()) { String[] row = new String[55]; resultSetIsEmpty = false; row[0] = rs.getString("ID");
I have a class with static ArrayLists to hold objects such as Members,Players etc.I want to save the class with the arrays so as to reload them again and hold onto the list of objects within those ArrayLists.
The ArrayClass
import java.io.Serializable; import java.util.ArrayList; public class ArrayClass implements Serializable {
[code]....
The arrays within the ArrayClass are empty when i reload the application.I cant tell if the arrays are being properly saved or is it in the reloading from file???
I was a bit confused of the code generated by the Axis2 Code Gen.
I have created a logIn(String username, String password) method in my service and used Axis2 Code Gen to generate the Stub.
I try to access the logIn method from my Client using the Stub like below:
TestServiceStub stub = new TestServiceStub("http://localhost:8080/axis2/services/TestService"); String test = stub.logIn("user","pass").
but instead of UserName and password as the parameters, the Stub created a Login object as the parameter for the logIn method. like the one below:
stub.logIn(Login login1);
I wanted to try the logIn method by providing a static userName and password but it seems impossible because the Stub changed the parameter for the logIn method.
The code here I have works fine if I just want to ask the user to enter four digits: //java application that asks user to input binary numbers(1 or 0) and convert them to decimal numbers import java.util.Scanner; //program uses class scanner public class binarynumber{
//main method that executes the java application public static void main(String args[]){ //declares variables
int digit; int base=2; int degree; double decimal; int binary_zero=0; int binary_one=1; //create scanner for object input
[code]....
The thing is, I want the java application to input more than four digits for the user and I want it to loop it manytimes f until the user ask it to stop.
I need to create a Java program that takes an input a color code in HSV and outputs the equivalent RGB code and Vice versa.
Example: If you test on the color RED in RGB: Input: (255,0,0) Output: (0,100%,100%) If you test on the color RED in HSV: Input0,100%,100%) Output: (255,0,0)
According to what I read, "when programming in Swing, your GUI creation code should be placed on the Event Dispatch Thread (EDT). This will prevent potential race conditions that could lead to deadlock." (See below for code.)
Why is this? How could making a GUI lead to deadlock?
public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); }
I have a assignment in which the code has to scrape a web page for a little bit of data including the link to the next page, follow the link, and do this 100 times. It is scraping all the data correctly, including the link, but it isn't following the link to scrape the data on that next page. Instead, it is displaying the first page's data 100 times. I have the code in a while loop where it reads in one character at a time from the page into a string and then uses pattern matching to get the data and the next link from the string. Then it correctly displays the data and should loop back to connect to that next link and read one character at a time into the string and so on.
I have printed the link to the console and it is good. I don't get any errors. I just can't figure out where exactly the problem is. Here is my code: