I am taking a parameter of type String and checking if either the city or state contains the "keyword", whatever it may be. I am using an arrayList of 10,000 customers and adding any customer that lives in a city or state that contains the keyword to a new arrayList and then returning that arrayList. I thought my solution was correct, but when using the JUnitTest provided, it is not quite passing the test. My solution is as follows.
Java Code:
public ArrayList <Customer> getMailingList(String keyword){
ArrayList <Customer> key = new ArrayList<Customer>();
keyword = keyword.toLowerCase();
for(Customer c : data){
if(c.getState().contains(keyword) || c.getCity().contains(keyword)){
key.add(c);
}
}
return key;
} mh_sh_highlight_all('java');
How does the Scanner goes through the String which is passed to it as a parameter. For example, String input = "222 dddd 222 ddd22" is passed to the method and here's the code:
Java Code:
public static void sum(String anything) { Scanner input = new Scanner(anything) while(input.hasNext()) { if(input.hasNextDouble()) { double nextNumber = inut.nextDouble(); sum += nextNumber } ...... ...... } mh_sh_highlight_all('java'); {
So, how does Scanner calculates a passed String? I just want to know the way it calculates/reads.
I wrote a couple classes and am trying a test driver but it is having an error I do not know how to solve.
Student Class:
public class Student{ private Course[] courseList; private static int numCourses; private final int maxCourses; public Student(int max){ maxCourses = max;
[Code] .....
Error: javac tester.java tester.java:6: error: cannot find symbol one = new Course(name); ^ symbol: variable name location: class tester 1 error
Same issue, just only one error as there is only one line. It seems like it does not accept my parameters as it cannot find symbol.
I forgot to put the "" in the brackets, it's been a month since I have looked at any java and made this simple mistake.
Question: 1. Declare and implement a class named Substrings. The class will contain the following data members and methods:
Data members: string S
Methods: setS() take a string as a parameter and store it in S printSub1(), printSub2(), printSub3(), printSub4() print all substrings of S. If S = "abcd",
Alright so after doing some googling and watching tutorials I managed to put together the first two but I still don't exactly understand how nested for loops work, however just a single loop I do understand.
Secondly, the 3rd and 4th prints are completely destroying my brain using for loops, which we are supposed to use. Check out my code below, also keep in mind the 3rd is giving errors and the 4th i had to revert back to the same as the first print temporarily because my code because so messed up. How to understand nested for loops better.
[code=Java]
import java.util.Scanner; class subString { String S; void setSubstring(String SS) { S = SS;
[Code] .....
Compiling: error for myS.printSub3 and myS.printSub4 is the same as 1 because i had to revert it after ruining the code.
I know when including remember me token in request header, it will contain expiry date. does this mean the token generated must be able to be reversed back to it's original string?
So basically we have this question to do : Write a method dayNumber that determines the number of days in a year up to and including the current day. The method should have three int parameters: year, month, and day. If the value of any parameter is invalid, the method should print a warning message and return the value zero. The table gives some examples of the action of the method. Accept any non-negative year as being valid. You may want to assume the existence of a method numberOfDays that returns the number of days in a given month of a given year. And you should have a method call isLeapYear, if the user enter a year that is a leap year.
This is what I did so far:
class dayMonthYear { public static void main(String[] args) { System.out.println("Enter a year"); int year = In.getInt(); System.out.println("Enter the month for '1' to be January - '12' to be december");
[Code] ....
It works and compiles but my problem is that: let say I enter "12" for December and December has 31 days, so what my program will do since December has 31 days, it thinks each month has 31 days and add them up which will give me 372 when it's suppose to give me 365. How do I make it that it won't do that and that it will work if the year is a leap year too.
I need to print contents of database to excel file.Apache POI is the solution but I am not finding a way to print the DB column names/headers into the excel.
E.g.:
incase csv we have opencsv CSVWriter writer = new CSVWriter(new FileWriter(fileName)); System.out.println("writer"); writer.writeAll(rs, true);
will print the db contents with the column headers dynamically whatever the query may be.
Any solution where we can print data and column names without know the query previously as we are getting query at the run time and each time the query is different.
I'm having a bit of trouble with using the Scanner and the Printwriter. I start with a file like this (1 = amount of Houses in the file)
1 FOR SALE: Emmalaan 23 3051JC Rotterdam 7 rooms buyprice 300000 energylevel C
The user gets (let's say for simplicity) 3 options:
1. Add a House to the file, 2. Get all Houses which fullfil requirements (price, FOR SALE / SOLD etc.) and 3. Close the application.
This is how I start:
Scanner sc = new Scanner (System.in); while (!endLoop) { System.out.println("Make a choice); System.out.println("1) Add House"); System.out.println("2) Show Houses"); System.out.println("3) Exit"); int choice = sc.nextInt();
Then I have a switch for all of the three cases. I keep the scanner open, so Java can get the user input (house = for sale or sold, price = ... etc). If the user chose option 1, and all information needed is inputted and scanned, the House will be written to the file (which looks like what I typed above).
For this, I use try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("Makelaar.txt", false)))). This works perfectly (at least so it seems.)
If the user chose option 1, and all requirements are inputted and scanned, the Houses will be read (scanner) from the file and outputted. For this I use the same Scanner sc. This also works perfectly (so it seems atleast).
My problem is as follows: If a House has been added, I can only read the House(s) which were already in the file. Let's say I have added 2 houses, and there were from the start 3 houses. If option 2 is chosen, the first 3 houses will be scanned perfectly. An exception will be caught for the remaining 2 (just added) Houses. How can I solve this? I tried to close the Scanner, and reopening it, but apparently Java doesn't agree with this
I developed an application that is accessing some property files. The condition was that the user should be able to modify the content or parameters of those property file.How can I distribute the application using Java web start that also includes those property file in the client side?
I use Jade an I'm able to send messages. Now I try to send a Message with not only integers or strings as content but with another concept as content. Here I want to send a Message of the Media, which contains any number of Trackdata.(I left out the import and other blabla stuff cause i is working)
I was practicing my java skills and came across an exercise in which a non parameter constructor calls a two parameter constructor. I tried a few searches online but they all came back unsuccessful. This is the part I am working on:
public PairOfDice(int val1, int val2) { // Constructor. Creates a pair of dice that // are initially showing the values val1 and val2. die1 = val1; // Assign specified values die2 = val2; // to the instance variables. } public PairOfDice() { // Constructor that calls two parameter constructor }
I tried calling the two constructor using the line "this(val1, val2)" but I get an error because val1 and val2 are local variables.
Then I tried to use the same signature: "this(int val1, int val2)" but that didn't work either.
I'm displaying a 3-column table with the column names ID, name, and salary. I made the ID into a link to go to the EditServlet class. What I'm trying to do is figure out which ID# was clicked to get onto the servlet page, how to implement that dynamically?
I know that if you put something like ?x=1 and then getParameter("x") on the servlet page would work, but then all the IDs would have the same parameters since I'm using a for loop to print out my ArrayList of objects.
I'm using Java in BlueJ where I'm trying to write a class to store cycling club membership details in a map. I'm trying to pass a map as a parameter to a function that will iterate over the map and print out the member details. My code is:
public class CtcMembers { //instance variables private Map<String, Set<String>> memberMap; /** * Constructor for objects of class CtcMembers
[Code] ....
When I execute the following in the OU workspace:
CtcMembers c = new CtcMembers(); c.addCyclists();
I can see a map populated with the expected details.
When I execute the following in the OU workspace:
c.printMap(c.getMap());
I get the following error:
java.lang.ClassCastException: java.util.HashSet cannot be cast to java.lang.String in CtcMembers.printMap(CtcMembers.java:81) in (OUWorkspace:1)
I wasn't aware I was trying to cast anything so this has got me baffled.
I am trying to create a method called getVariable will has a parameter of a variable that has a type of Test, an enumeration. An int variable called x is assigned a value of 5, and through control statements, I want its value to change depending on what the argument is. I was able to correctly create this method, but when I use it I get an error saying, "non static method getVariable(Test) cannot be referenced from a static context" on line 28.
Here is the code:
package javaapplication5; public class Product implements Cloneable {
import acm.program.*; import java.io.*; import java.io.PrintWriter.*; import acm.util.*; import javax.swing.*; public class PrintWriter extends ConsoleProgram {
public void run() {
try { PrintWriter wr = new PrintWriter( new FileWriter ("hello.txt") ) ; wr.println("Hello world!"); wr.close(); } catch (IOException er) { println("File could not be saved."); } } }
(I added the imports at top myself.) However, I am getting compiler errors for the lines: PrintWriter wr = new PrintWriter( new FileWriter ("hello.txt") ) , which says that the constructor PrintWriter( FileWriter ) is undefined, and wr.close();, which says close() method is undefined.
Pretty sure the only real problem is that PrintWriter is not accepting the FileWriter as constructor, but I don't see why. I have tried this on machine with JRE 1.4 and it worked as expected, creating new file titled "hello.txt", prints line of "Hello world!" in that file, and saves it to the directory I picked in the dialog. But I can't get it to work on this machine that uses Compiler 1.6, Java Runtime v8u25.
I have also tried using just a string in the parameter for PrintWriter, such as PrintWriter wr = new PrintWriter ("hello.txt") , and from what I can tell by reading the java spec for java 8, this should work. [URL] .... But I get error message for that constructor as well.
So method invia call the method popolaScompiute, inside popolaScompiute there is an iteration through some id and for some id can occur an error; what i want is the getting the value of id in the first method invia, using the block try/catch. Is there a way to accomplish this?
I have a variable <c:set var="var1" value = "myvalue" /> , I want to pass var1 as <%= new customclass().method1(var1) %>.what is the syntax to pass this value.
I am trying to run an example in which I pass a parameter to imported file but its not working.
This is my header.jspf file which I will include in a jsp file.
<img > <br>${param.sub}
And This is the JSP file contact.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <body> <c:import url="header.jspf"> <c:param name="sub" value="We love to hear from our clients------------" /> </c:import> <br><br>
Now this is JSP page content
</body> </html>
When I hit the contact.jsp file , It does show me the image I have on header.jspf , but its not able to print the parameter that I am setting in <c:param>
So after showing image it just shows ${param.sub}. So EL is not translated properly.
So, this is weird for me because I don't really understand why the BorderLayout class constructor is being initialized as a parameter for the setLayout..
I have multiple Java classes that holds lots of time based parameters, e.g.: (simplified):
Java Code: class Engine { float rpm; boolean enabled; int runningTime;
[code]....
Is there any simply solution to map all these parameters from my TimeRecord to one list(vector,map) of params that user can choose from and see the value in proper form (floating type, boolean type) ?
I just want that user can pick a paremter's name from list or combobox and choose parameter that he wants to see. I don't want to duplicate the data - just some kind of mapping algorithm between list of parameters names and my TimeRecord. eg. some collection class that holds all names and connections: Parameters params; fill combo/list from this class for ( String name : params ) { combo.add(name);}, then when user picks some field from combo: params.getValue( myTimeRecord, name ) return correct parameter value (just my first thought)
Later, I want to replace all floats,ints,booleans with some kind of templated class eg. Value which will hold all kinds of measurement unit with conversion support - but it's only a plan now.