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) {
I am learning iterating through lists. What I have so far is two Hash Sets and two Tree sets. Hash Set 1 and Tree set 1 include the words from Roughing it by Mark Twain. Hash set 2 and tree set 2 include the words from Adventures of Huckleberry Finn by Mark Twain. (Everything is read from a file I made).
I am stuck trying to find out how to "Iterate through the words in HashSet1 and search for these words in both TreeSet2 and in HashSet2".Here is my code:
public class UsingSets { public static void main(String[] args) throws FileNotFoundException { String riHashIterator = null; HashSet<String> riHash = new HashSet<>(); Scanner input = new Scanner(new File("roughingit.txt")); while(input.hasNext()){ String riHashWords = input.next(); riHashWords = riHashWords.toLowerCase(); riHash.add(riHashWords);
I have to make a method called search that goes through the linked list and checks to see if whatever String the user entered matches a String in the linked list. With this code, every time I enter an existing String it outputs "There is no element that contains that information". How come?
public class LinkedListExample { List InfoList = new LinkedList(); public void doLinkedListExample() { // add original data to linked list InfoList.add("Computer"); // string (original 3 elements) InfoList.add("Programs"); InfoList.add("in Java");
I just trying to find the most efficient way to do this. I read in a csv file into a linked list and then I split the linked list into sections based on category for each element. Of course I used an array to split each element of the list. However I can do the sequential search by either ID and Name by using hashmap and making the key = name + ID and then doing key.contains(charSequence);. However I feel like this is inefficient and I would like to use the linked list instead of a hashmap which could be done by splitting the user input and used for method overloading by passing an int in one and a string in another. I feel like this approach is a little more redundant and maybe their is a better approach for searching for id and name. Below is an example of the elements in a linked list.
note: their are more elements than this.
element 1
name: George address: 4410 something dr. phone number: 978-888-6666 id: 43
element 2
name: Karla address: 339 something dr. phone number: 334-338-6556 id: 23
// 1 ***** student writes this method /** Searches for key in integer array named arr // arr is an instance variable of the class and has been instantiated and filled with random values. // @param key value to search for // @return if key is found, the index of the first element // in array whose value is key; if key is not found, // the method returns -1 */
public int sequentialSearch( int key ) { // Note: To animate the algorithm, put this method call as the first statement in your for loop // animate( i, 0 ); // where i is the index of the current array element return 0; // replace this statement with your return statement } // end of sequentialSearch
Doing an early exercise out of the Java Examples in a Nutshell book and they are asking for 'an efficient search algorithm to find the desired position' of two floats in a sorted array that bound an int. My try is below:
public static int search(int searchnum, float[] nums){ int low = 0; int high = nums.length - 1; int mid = (low + high) / 2; while(low < high){ if(nums[mid] < searchnum){
[Code] ....
This is working for the example but I would like to know if it is considered 'efficient' or even good?
I'm just getting two errors concerning line 38 where it has Arrays.sort(int roomList); and the errors state that ".class is expected" and so is a semicolon. What did I do wrong? Also, how might I tweak the code to display "Occupied" or "Unoccupied" depending on the room that was entered?
Also we're not allowed to make use of API method for binary search so that's out of the question.
import java.util.Scanner; import java.util.Arrays; public class HotelRoom { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); int[] roomList = new int[25]; // occupied rooms
I am attempting to find the element that holds the lowest time ( i have used System.currentMillisTime ) in the array using a linear search and to then print the times held by the array in lowest to highest order . While i understand how to do a linear search using user input as the key i am not to sure how to do it by initializing a search key in the program to the lowest number and have little experience in using a search in a program that is not a simply linear search. i have attempted to code this, as seen below, but i know i am definitely wrong and i have tried another of different ways even Array.sort and then a binary search .
static long store_MAX[]; // Now an ‘array’ of ints static int deptSize; // Holds the length of the buffer private int shopper_MAX; // Holds the number of items in the buffer
We are required to write a binary‐search method to find an int within an array of ints. Without using any functions besides the basics (for loops, if statements, etc.)
I have this so far:
public int binarySearch(int[] ints, int n) { for(int i = 0; i < ints.length; i++) { if(ints[i] == n) { return ints[i]; } } }
Operator is undefined for argument type. Error is located at the end of the binary search method array[position] < key
import java.util.Arrays; public class binarySearch { public static <T extends Comparable<T>> int binarysearch(T key, T[] array) { int start = 0; int end = array.length - 1; int position =-1; while (start <= end && position == -1) {
I'm (failing at) writing a program that searches an array using binary search, but I can't get it to work.
My code so far is this:
Java Code:
package sorting;
import java.lang.*; import java.util.*; public class sorteh { public static void main(String [] args){ int[] array=new int [20]; //creates new array for (int x=0;x<array.length;x++){ //populates array array[x]=x*3+1;
[code]...
I copied what a website did for the sorting part, but if I have low=0 and high=19, wouldn't mid not be an int?
I'm trying to build a method that can search a binary search tree for a specific target and then return the number of probes it took to get there. It seems to me that the best way to do this would be a recursive search method and a counter that tracks the number of calls. But I'm struggling with how to implement this. Here's the code I have so far. what works/doesn't work with the method.
// Method to search the tree for a specific name and // return the number of probes public T search(BTNode<T> btNode) {
I have been creating a Java program to track inventory using an array. I need to add a search function to it to take a term, input through a text field, and locate a matching instance within the array. How can I code a JButton to grab test input from a JTextField and search for it within the array then display the corresponding information? I also need to add buttons to add and delete items as well.
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)
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?
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.
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();
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)
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)
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?
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.