Thanks for that :-). This escaping sequence it interesting, I didn't come across that but this \|[0-9]+
escapes the '|' absolutely fine! So I'm a bit confused...
Violet_82 89 Posting Whiz in Training
rproffitt commented: The version of powershell might be why. Note: I'm not a historian on this area. +15
Violet_82 89 Posting Whiz in Training
Hi I'm trying to create a script in powershell that finds a string in a file and then modifies it but I'm having issues matching the string I want.
So, I'm trying to make sure that I can find this type of string
|d where d is is any digit, the string must not be in single or double quotes.
SO I'd like to match |1 or |12 or |333
So far this is what I have:
\|[0-9]+
is returning strings with or without single quotes around it, so |1 and also '|5';
\'\|[0-9]\'+
returns only strings with single quotes around it, so matches '|4' but not |3
\\'|'(?:\\'|[^'])*'|(\[0-9]+)
as the first one, it is returning strings with or without single quotes around it, so |1 and also '|5';
\[^']\|[0-9][?']\+
I thought this would say something like exclude ' at the beginning and at the end of the string, but it doesn't work at all
Now, you might have understood that I really don't know much about regex, so I'm trying options in the online regex checker here https://regex101.com/ but it appears to be behaving slightly different than powershell.
Now, what do I have to do to match a string without the quotes? I don't necessarily want the solution, but perhaps some hint would be appreciated :-)
Violet_82 89 Posting Whiz in Training
I'd probably implement a method like this:
Thanks, and sorry for the long delay. I could yes, but then what about the other methods using ids? I thought that one of the advantages of using rest was to be able to use ids for easier (and more reliable) retrieval, and while I was looking into it before developing, I indeed found that most of the implementations use ids...which then led to my above question, lol. So, if I use the method you suggested then the ones using ids become redundant, right?
Unless you meant like using the exta searchBooks method not to find the record but to perhaps call another method to retrieve the ids and then I could use the ids to call the get method with the relevant id...
I’m not sure what data set sever you’re using,
I'm using Tomcat at the moment (if I understood your question :-)). I don't envisage the db to grow too big though
Violet_82 89 Posting Whiz in Training
I 'm building an application to store and retrieve books, but I have an issue with retrieving data from the db.
I'm using REST and testing with postman. Everything works OK. Currently I have a series of methods written in Java at the backend like so
@Override
@POST
@Path("/add")
//@Produces("application/json")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response addBook(Book book) {
dbAccess.connectToDb();
return dbAccess.addBook(book);
}
@Override
@DELETE
@Path("/{id}/delete")
@Produces("application/json")
public Response deleteBook(@PathParam("id") int id) {
dbAccess.connectToDb();
return dbAccess.deleteBook(id);
}
@Override
@GET
@Path("/{id}/get")
@Produces("application/json")
public Book getBook(@PathParam("id") int id) {
dbAccess.connectToDb();
return dbAccess.getBook(id);
}
So in postman I have a request like http://localhost:8080/book-storage-REST/book/15/get to retrieve a record.
Eventually I will write a front end, maybe in angular, which allows users to do their searches and display the results back to them.
Now, in the real world, a user of course wouldn't use an ID to search for a record, but he/she will be more likely to use the author name or book title. So my question is:
do I need to rewrite the methods above and not use an id parameter or should the front end do the job of taking a search term (title or author), query the db to find the equivalent id, do the necessry filtering in case of more than 1 record and only then, call the relevant methods above wth only one ID?
Violet_82 89 Posting Whiz in Training
Which in turns means another Statement because I've just learned that you can have multiple ResultSets open with one Statement only (my sincere apologies for the long delay in updating the thread)
It works now with a local ResultSet in addition to the other one
Violet_82 89 Posting Whiz in Training
I don't think I have a good reason to be honest, I was just curious :-)
Violet_82 89 Posting Whiz in Training
Hi all, I have a question about updating two values in a sql table using a subquery. My database is called books and the below table books too:
id title author location_id
11 Tera Netrear 4
14 Disser Bubu 1
15 Tenno Puoio 4
16 Oban Tyrone 5
18 Sisnobi Teru 3
Location_id is indeed another table as it's a foreign key in this table but that shouldn't matter. So what I want to do is to change all the location_id currently at 4 to 2. I know i could do it like this
update books set location_id = 1 where location_id = 4
but I'd like to know if I could do it with a subquery, something like update (SELECT * from books where location_id = 4) as thebook set thebook.location_id = 2
but when I run this query it tells me that the target table thebook UPDATE is not updatable
. Why is that and can this be achieved - I don't really mind if it's not performant etc, I think it should be possible but I can't quite get the right way to do it...
So, the idea is, make a selection first then change a/more value/s on that selection, this is the concept I'm try to prove.
Any idea?
Violet_82 89 Posting Whiz in Training
Also, let me give you another example.
I changed the code slightly and still fails to go through each record, it only goes through 1 but this time no error is generated. Here is the code:
public Book[] getAllBooks()
{
HashMap<String, Book> booksRecords = new HashMap();
try
{
resultSet = statement.executeQuery(SqlStrings.SELECT_NUMBER_ALL_RECORDS);
resultSet.next();
int dbRecordNo = resultSet.getInt(1);//get number of record
resultSet = statement.executeQuery("SELECT books.id FROM books");
while(resultSet.next())
{
String id = resultSet.getString("id");
System.out.println("");
booksRecords.put(id, getBook(Integer.parseInt(id)));
}
return books;
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
This time I created a HashMap which would contain the id of the record and the actual Book object. Again, it only does one pass, then returns.
I'm starting to think that the problem might be inside the getBook method, so here it is, but it's all standard stuff:
public Book getBook(int id)
{
Book bookQueried = null;
try
{
resultSet = statement.executeQuery(String.format(SqlStrings.SELECT_RECORD, id));
bookQueried = constructObject(resultSet);
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return bookQueried;
}
private Book constructObject(ResultSet resultSet) {
Book bookToReturn = new Book();
try
{
while (resultSet.next())
{
bookToReturn.setId(Integer.valueOf(resultSet.getString("id")));
bookToReturn.setTitle(resultSet.getString("title"));
bookToReturn.setAuthor(resultSet.getString("author"));
bookToReturn.setLocation_id(Integer.valueOf(resultSet.getString("location_id")));
}
}
catch (SQLException e)
{
e.getMessage();
e.printStackTrace();
}
return bookToReturn;
}
Violet_82 89 Posting Whiz in Training
I am not sure what your object books is suppose to return as you already run all id's
Not sure what you mean. The method should essentially loop through all the ids, use each id to construct the Book object and insert that into a Book array. In here while((resultSet.next()) && (i < dbRecordNo))
both conditions should be true an equal amount of times because dbRecordNo
contains the number of records and resultSet.next() should go through all the records (ids).
The error refers to you trying to run two objects in one loop
Right, is that not allowed :-)? dbRecordNo is an int so it's not an object
Violet_82 89 Posting Whiz in Training
Hi there, I've come across an issue while trying to get all the records from a SQL database in java.
A bit of necessary introduction here.
I'm not using any framework just JDBC and the getAllBooks method is part of a REST call. The failing method is essentially doing this:
-getting the number of all records in the db;
-getting all the ids and use them to get the records, store them in an array and return that array;
Unfortunately the call generates the following error in the while loop:
SEVERE: Servlet.service() for servlet [Jersey Web Application] in context with path [/book-storage-REST] threw exception
java.lang.IllegalArgumentException: the object parameter to marshal() is not marshallable
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:280)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:163)
at com.sun.jersey.json.impl.provider.entity.JSONListElementProvider.writeList(JSONListElementProvider.java:145)
at com.sun.jersey.core.provider.jaxb.AbstractListElementProvider.writeTo(AbstractListElementProvider.java:264)
at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:302)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1510)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:558)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:733)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Ok, the code now:
public Book[] getAllBooks()
{
Book[] books = null;
try
{
resultSet = statement.executeQuery(SqlStrings.SELECT_NUMBER_ALL_RECORDS);//runs "SELECT COUNT(*) from books";
resultSet.next();
int dbRecordNo = resultSet.getInt(1);//get number of record
resultSet = statement.executeQuery("SELECT books.id FROM books");//get all ids
books = new Book[dbRecordNo];
int i = 0;
while(resultSet.next() && i < dbRecordNo)
{
int bookId = Integer.parseInt(resultSet.getString("id"));
books[i] = getBook(bookId);
System.out.println();
i++;
}
return books;
}
catch (SQLException e)
{
// …
Violet_82 89 Posting Whiz in Training
Well, maybe there isn't anything to test there....I thought I'd test a range of values, right ones and not right ones, but maybe I don't really need to do that, not sure to be honest
Violet_82 89 Posting Whiz in Training
Thanks. A few things from my side :-)
So String correctInput = "3\n" ; // this is what the user should have typed
would mean that I would have to do something like this though assertTrue(testVal.equals(String.valueOf(userChoice)));
and - I haven't tried yet, hopefully tomorrow - perhaps remove that \n from the string before doing the assertion.
Also, one thing occurred to me. If I then run another test, say something like this
@Test
public void test() {
String testVal = "99";
byte[] testVal_bytes = testVal.getBytes();
insertInStream(testVal_bytes);
userInput = new UserInput();
int userChoice = userInput.getUserInput();
}
99 isn't within the valid values but the getUserInput() method keeps asking for an input till the user uses a valid one. So, the above test will inject 99 in the stream, great, but the getUserInput() will correctly identify that as a non valid input and then will ask again for another input and the stram doesn't have anything else in it so it will fail miserably. So, how would I go about testing this then?
Violet_82 89 Posting Whiz in Training
Thanks for the feedback. I like your idea about the the enum but having the validation directly in it seems a bit odd as opposed to have it in a separate class?
Sorry I didn't share the full story here but this is essentially just a bit of a test console application. I'm planning to rewrite the whole thing and add some decent a front end to it too (maybe angular, I haven't decided yet and this will be, at some point soon, the subject of a different thread) so any tip for a rewrite is of course VERY welcome, like for the example the enumeration bit.
One thing that became obvious in here is the fact that the tests are now being added almost as an afterthought, so during the rewrite I will try to stick to a more TDD approach: granted it will take more time but the code should in theory be more robust, and it will be a good exercise anyway.
For now, I will finish off the tests, upload the SQL scripts and then move one :-)
With the refactoring, were you referring to the class that does the CRUD operations?
Violet_82 89 Posting Whiz in Training
I think I'll provide an example. This actually works OK
public void testCase1() {
String testVal = "3";
byte[] testVal_bytes = testVal.getBytes();
provideInput(testVal_bytes);
userInput = new UserInput();
int userInput2 = userInput.getUserInput();
assertEquals(Integer.parseInt(testVal), userInput2);
}
private void provideInput(byte[] int_bytes) {
testIn = new ByteArrayInputStream(int_bytes);
System.setIn(testIn);
}
Is this the kind of thing you had in mind? What doesn't really convince me is the fact that I had to use a String rather than an int
Violet_82 89 Posting Whiz in Training
I used ObjectOutStream mostly because the example I found did it, but it could easily be omitted from the code altogether.
The reason for using a byte[] is just so I could create a ByteArrayInputStream and inserting it in the stream to fake the input, or at least this is what I thought.
So, just to be sure I understand, you're saying have my testString (not a good name as it isn't a string anymore but an int now - I should've given it a better name) and then use a PrintStream to inject that test value in the stream (like use the PrintStream to create the byte[] and then create the ByteArrayInputStream)? I don't necessarily need a file, so probably a string would do.
Violet_82 89 Posting Whiz in Training
Hi guys, I was trying to test a method which takes a user input, does something and then returns that input but I'm having some issues with it. I've attempted a few solutions, mostly described in SE, I will leaave them out of this
post and discuss instead the one that seemed the most likely to work instead.
This is the method that I'm trying to test (other code omitted for brevity):
public int getUserInput() {
int choice = 0;
boolean isValidInput = false;
System.out.printf("Welcome. Select action: %d for READ, %d for CREATE, %d for UPDATE, %d for DELETE, %d for EXIT.", OperationOptions.READ.getValue(), OperationOptions.CREATE.getValue(), OperationOptions.UPDATE.getValue(), OperationOptions.DELETE.getValue(), OperationOptions.EXIT.getValue());
while(!isValidInput)
try {
choice = scanner.nextInt();
if(choice == OperationOptions.READ.getValue() || choice == OperationOptions.CREATE.getValue() || choice == OperationOptions.UPDATE.getValue() || choice == OperationOptions.DELETE.getValue() || choice == OperationOptions.EXIT.getValue())
{
isValidInput = true;
}
else
{
isValidInput = false;
System.out.println(String.format(Messages.NOT_ALLOWED_VALUES, OperationOptions.READ.getValue(), OperationOptions.CREATE.getValue(), OperationOptions.UPDATE.getValue(), OperationOptions.DELETE.getValue(), OperationOptions.EXIT.getValue()));
}
scanner.hasNextLine();
}
catch(InputMismatchException e) {
System.out.println(Messages.NOT_NUMERICAL_ERROR);
isValidInput = false;
scanner.nextLine();
}
return choice;
}
A word about the test. What I had in mind was to test the actual input, so have a test that fakes the input, then calls the method and check that the fake input is the same as the one returned from that method. That would be
the first test, then chose another input and make sure it generates an exception and so on.
So this is my test class
package com.test.userInteraction;
import static org.junit.Assert.assertEquals;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream; …
Violet_82 89 Posting Whiz in Training
thanks for taking the time to explain it. Yes, I got it now :-).
I went for solution 1 and it's all working now :-)
Violet_82 89 Posting Whiz in Training
that said, using onnly one scanner appears to be causing issues again as control doesn't stop at the second scanner input inside getUserInputForProcessing.
package com.test.userInteraction;
import java.util.InputMismatchException;
import java.util.Scanner;
import com.test.db.operations.BookManager;
public class UserInput {
private static final String READ = "READ";
private static final String CREATE = "CREATE";
private static final String UPDATE = "UPDATE";
private static final String DELETE = "DELETE";
private static final Scanner scanner = new Scanner(System.in);
public int getUserInput() {
//Scanner scanner = new Scanner(System.in);
int choice = 0;
boolean isValidInput = false;
System.out.printf("Welcome. Select action: %d for READ, %d for CREATE, %d for UPDATE, %d for DELETE.", OperationOptions.READ.getValue(), OperationOptions.CREATE.getValue(), OperationOptions.UPDATE.getValue(), OperationOptions.DELETE.getValue());
while(!isValidInput)
try {
choice = scanner.nextInt();
isValidInput = true;
}
catch(InputMismatchException e) {
System.out.println("value must contain only number");
isValidInput = false;
scanner.nextLine();
}
return choice;
}
public void processChoice(int userChoice, BookManager bookManager) {
switch(userChoice) {
case 0:
System.out.println("Goodbye.");
System.exit(0);
break;
case 1:
System.out.println("You chose READ");
getUserInputForProcessing(READ);
bookManager.read();
break;
case 2:
System.out.println("You chose CREATE");
bookManager.create();
break;
case 3:
System.out.println("You chose UPDATE");
bookManager.update();
break;
case 4:
System.out.println("You chose DELETE");
getUserInputForProcessing(DELETE);
bookManager.delete();
break;
}
}
public String getUserInputForProcessing(String action)
{
//Scanner scanner = new Scanner(System.in);
System.out.printf("You chose %s: ", action);
String nextLine = "";
if(action.equals(READ) || action.equals(DELETE) )
{
System.out.println("Enter the searching criterion");
}
try
{
nextLine = scanner.nextLine();
}
catch(Exception e)
{
e.printStackTrace();
}
return nextLine;
}
}
Violet_82 89 Posting Whiz in Training
thanks. I only closed the scanner as eclipse was somplaining about that. I remove the close statments and now everything work. However I will use only one scanner as you suggested.
Thanks
Violet_82 89 Posting Whiz in Training
yes they are constant declared at the beginning - sorry I didn't post the whole class. And yes, I should probably use them at line 9 and 2 as pointed out.
I will use only one scanner then - at the moment I'm using 2 in fact even though they're duly closed - and then post back the results and see how it goes.
Violet_82 89 Posting Whiz in Training
Hi all,
I'm having some issues with the Scanner not waiting for input from the keyboard.
The first method processChoice takes the userChoice and the idea is that in the switch statement there is a call to a method getUserInputForProcessing that takes the action and there scanner takes an input but it's not passing the control to the keyboard,
it goes directly to the Exception because nextLine is empty, not entirely sure why. Is that because it's inside a switch statement?
public void processChoice(int userChoice, BookManager bookManager) {
switch(userChoice) {
case 0:
System.out.println("Goodbye.");
System.exit(0);
break;
case 1:
System.out.println("You chose READ");
getUserInputForProcessing("READ");
bookManager.read();
break;
case 2:
System.out.println("You chose CREATE");
bookManager.create();
break;
case 3:
System.out.println("You chose UPDATE");
bookManager.update();
break;
case 4:
System.out.println("You chose DELETE");
getUserInputForProcessing("DELETE");
bookManager.delete();
break;
}
}
public String getUserInputForProcessing(String action)
{
Scanner scanner = new Scanner(System.in);
System.out.printf("You chose %s: ", action);
String nextLine = "";
if(action.equals(READ) || action.equals(DELETE) )
{
System.out.println("Enter the searching criterion");
}
try
{
nextLine = scanner.nextLine();
}
catch(Exception e)
{
e.printStackTrace();
}
scanner.close();
return nextLine;
}
Violet_82 89 Posting Whiz in Training
Hi all,
I have an application which saves data (books details like book title, author and location) to a mySql db and performs CRUD operations.
I thought I'd use hibernate to take care of the ORM side of things and everything was OK when it came to create a record, but for RUD operations I had an issue.
If I'm a user, I'm likely to be willing to find a book by its author, title or location, certainly not by its ID and yet as far as I can see, hibernate uses IDs for these kind of operations.
I dug a bit further and eventually resorted to Criteria objects which apparently allow you do perform operations creating your own criteria, see below for the update functionality:
public void update() {
// code to modify a book
Session session = sessionFactory.openSession();
String searchTerm = "Jack";
session.beginTransaction();
Criteria criteria = session.createCriteria(Book.class);
Book uniqueResult = (Book) criteria.add(Restrictions.eq("author", searchTerm)).uniqueResult();
uniqueResult.setTitle("Amended plus one");
session.update(uniqueResult);
session.getTransaction().commit();
session.close();
}
The problem is that criteria
have been deprecated and I can't find a good alternative to that, which now makes me question my choice to use hibernate in the first place. What do you guys think? I wouldn't mind using hibernate but I find hard to believe that to find a record I have to use an ID and not the author, title or whatever else, unless of course you can get the ID of the record from the searchTerm like author etc but I couldn't find …
Violet_82 89 Posting Whiz in Training
Great, thanks
Violet_82 89 Posting Whiz in Training
Cool, thanks
Violet_82 89 Posting Whiz in Training
That could be much easier amd faster of you sorted the List (or create a sorted copy) first. Then just store every record that's equal to the previous record.
Yes, that worked perfectly.
Here is the code.
I slightly changed the collection, adding 2 duplicates and not only one (Anna is there 3 times):
studentRecords.add(new Student("Jo", "Pip", "JP000", LocalDate.of(1999,9,23), Sex.FEMALE));
studentRecords.add(new Student("Sean", "Due", "SD230", LocalDate.of(1982,10,2), Sex.MALE));
studentRecords.add(new Student("Bart", "Dowes", "BD333", LocalDate.of(2000,11,23), Sex.MALE));
studentRecords.add(new Student("Tara", "Bot", "TB345", LocalDate.of(1991,9,1), Sex.FEMALE));
studentRecords.add(new Student("Mara", "Lewart", "ML456", LocalDate.of(1988,5,23), Sex.FEMALE));
studentRecords.add(new Student("Anna", "Clarke", "AC010", LocalDate.of(1999,1,1), Sex.FEMALE));
studentRecords.add(new Student("Ahmed", "Mohammed", "AM222", LocalDate.of(1990,2,23), Sex.MALE));
studentRecords.add(new Student("Dan", "Tissip", "DT599", LocalDate.of(1988,4,12), Sex.MALE));
studentRecords.add(new Student("Frank", "Boia", "FB300", LocalDate.of(2001,8,13), Sex.MALE));
studentRecords.add(new Student("Anna", "Clarke", "AC010", LocalDate.of(1999,1,1), Sex.FEMALE));
studentRecords.add(new Student("Darla", "Roberts", "DR432", LocalDate.of(1979,10,10), Sex.FEMALE));
studentRecords.add(new Student("Jo", "Pip", "JP000", LocalDate.of(1999,9,23), Sex.FEMALE));
studentRecords.add(new Student("Anna", "Clarke", "AC010", LocalDate.of(1999,1,1), Sex.FEMALE));
then the class Student extends Comparable and overrides compareTo
public class Student implements Comparable<Student> {
....
@Override
public int compareTo(Student o) {
return this.name.compareTo(o.name);
}
}
and the main class calls the findDuplicates method which has all the logic, sorting the elements and comparing each record to the previous one as you suggested
...
public void findDuplicates() {
duplicateRecords = new ArrayList<Student>();
Collections.sort(studentRecords);//sorting the collection
printDetails(studentRecords);
System.out.println("############");
for(int i = 1; i < studentRecords.size(); i++) {
Student currentStudent = studentRecords.get(i);
Student previousStudent = studentRecords.get(i - 1);
if(currentStudent.equals(previousStudent)) {
duplicateRecords.add(currentStudent);
}
}
printDetails(duplicateRecords);
}
...
This prints
Student [name=Anna, surname=Clarke, studentNumber=AC010, dob=1999-01-01, sex=FEMALE]
Student [name=Anna, surname=Clarke, studentNumber=AC010, dob=1999-01-01, sex=FEMALE]
Student [name=Jo, surname=Pip, studentNumber=JP000, dob=1999-09-23, sex=FEMALE]
Violet_82 89 Posting Whiz in Training
Hi guys,
I am having an issue finding duplicates in an arrayList.
Here is some code
studentRecords.add(new Student("Jo", "Pip", "JP000", LocalDate.of(1999,9,23), Sex.FEMALE));
studentRecords.add(new Student("Tara", "Bot", "TB345", LocalDate.of(1991,9,1), Sex.FEMALE));
studentRecords.add(new Student("Mara", "Lewart", "ML456", LocalDate.of(1988,5,23), Sex.FEMALE));
studentRecords.add(new Student("Anna", "Clarke", "AC010", LocalDate.of(1999,1,1), Sex.FEMALE));
studentRecords.add(new Student("Frank", "Boia", "FB300", LocalDate.of(2001,8,13), Sex.MALE));
studentRecords.add(new Student("Anna", "Clarke", "AC010", LocalDate.of(1999,1,1), Sex.FEMALE));
studentRecords.add(new Student("Jo", "Pip", "JP000", LocalDate.of(1999,9,23), Sex.FEMALE));
This is the list of student records stored in an ArrayList.
I have to make sure that I identify the duplicates and store the duplicates in another arrayList, duplicateRecords for the sake of argument.
Now, the first thing I tried was to do something like this
public void findDuplicates() {
if(!studentRecords.isEmpty()) {
duplicateRecords = new ArrayList<Student>();
for(int i = 0; i < studentRecords.size(); i++) {
for(int j = 1; j < studentRecords.size(); j++) {
if(studentRecords.get(i).equals(studentRecords.get(j)) && i != j) {
duplicateRecords.add(studentRecords.get(i));
}
}
}
}
printDetails(studentRecords);
System.out.println("############");
printDetails(duplicateRecords);
}
Which is buggy because, of course, it adds the record for Anna twice. I guess the complexity comes in if there are multiple duplicates, for example what happens if there are 3 records for Anna Clarke? In theory I'd want that to be twice in the duplicateRecords because it is a double duplicate
I can't really think of another approach though, any ideas?
Violet_82 89 Posting Whiz in Training
Right. Thanks. I've implemented the functionality based on your example and yes it works. Now I have to make sure I understand it though.
Let's look at the code (I will return it directly rather than returning the variable).
studentList here is a List type.
private List<Optional<Student>> getOverSixtyStudents() {
List<Optional<Student>> collect = studentList.stream().
filter(s -> s.getAge() > 60)
.map(s -> Optional.of(s))
.collect(Collectors.toList());
return collect;
}
First off turn studentList to a stream, then apply the filter as before to filter out students below 61. Now, after filter(s -> s.getAge() > 60)
, "s" contains student objects whose age is above 60, correct?
If that's so, then .map(s -> Optional.of(s))
is transforming those objects into Optionals, correct?
Then the terminal operation .toList() simply turn that into a list.
If all the above is correct, it was actually a lot easier than I thought...but I did't actually thought about using the map to transform the values, I was more hoping to find a way to replace .toList() with something else that would generate Optionals, which was clearly a mistake.
Violet_82 89 Posting Whiz in Training
Hi guys, I'm a little stuck with something here.
Basically I have a a method which, given a List of Students will have to filter out those who are younger than 60yrs old and should return a List of Students who are over 60yrs old but I want this to be a list of Optionals rather than just a List of Students. THe difficulty is that I can't find a way to return a list of optionals like List<Optional>. I can return an optional if I use things like findFirst() or findAny()
return studentList.stream().filter(x -> x.getAge() > 60).findAny();
or i could return a simple list of students
return studentList.stream().filter(x -> x.getAge() > 60).collect(Collectors.toList());
but I want all the Students over 60 in a list of Optional.
Any idea how that can be done if it can be done that is?
thanks
Violet_82 89 Posting Whiz in Training
Hi, sorry been caught into other things on the way.
I've tried to get my head around this again, I'm still in the splitting string phase and I can get this {"Product":{"id":2,"name":Product_2}}
to be split into this array of strings
{"Customer":{"id":1
"name":Customer_1}}
But I don't seem to be able to split it any further without getting into issues, ideally I'd like to get to this array of strings instead:
{"Customer":
{"id":1
"name":Customer_1}}
So the methods doing the splits are these
private void parseResponse(String generateJsonResponse) {
//System.out.println(generateJsonResponse);
String[] splitObjects = splitObject(generateJsonResponse, ",", 0);
for(int i = 0; i < splitObjects.length; i++) {
System.out.println(splitObjects[i]);
}
}
private String[] splitObject(String generateJsonResponse, String delimiter, int limit) {
String[] split = generateJsonResponse.split(delimiter, limit);
for(int i = 0; i < split.length; i++) {
//System.out.println(split[i]);
// if(split[i].indexOf(':') != -1) {
// split = splitObject(split[i], ":" , 0);
// }
//TODO turn into a collection and back into array
//System.out.println(split[i]);
}
TreeSet<String> splitString = new TreeSet<String>(Arrays.asList(split));
//HashSet<String> splitString = new HashSet<String>(Arrays.asList(split));
for(String currentString : splitString) {
//System.out.println(currentString);
}
return split;
}
(everything gets copied into TreeSet to avoid duplicates) but the moment is try to split the string even further using the ":" as delimiter and uncommenting this code in splitObject method
if(split[i].indexOf(':') != -1) {
split = splitObject(split[i], ":" , 0);
}
then I get only a partial string
{"Customer"
{"id"
1
SO essentially what I'm trying to do there is to split the string first at the "," delimiter, which works, but then …
Violet_82 89 Posting Whiz in Training
Eh eh, no I was wrong, I don't need to pass anything to the constructor, I can simply keep the constructors as they are and in the employee's one do this:
public Employee(String name, String surname, String dob, String type) {
this.name = name;
this.surname = surname;
this.dob = dob;
if(type != null) {
this.type = type;
}
else {
this.type = "unspecified";
}
employees.add(this);
}
Noted what you said about the readClass, you're right, I will amend. Also about the try/resource I've actually never tried it, I will have a look!
Violet_82 89 Posting Whiz in Training
Thanks.
The constructor for Employee can simply add this to the list.
But that means that the subclasses, PartimeEmployee and FUlltimeEmployee need to call the super constructor (EMployee's) with a parameter of this, something like
...
public class PartimeEmployee extends Employee {
public PartimeEmployee(String name, String surname, String dob, String type) {
super(name, surname, dob, type, this);
}
...
so that they can match the EMployee's constructor signature which now is
public Employee(String name, String surname, String dob, String type, Employee employee) {
...
But this won't work because I'm essentially passing an instance of the object - this - which at that time is probably still not fully constructed. How do I get around that?
Violet_82 89 Posting Whiz in Training
Hi guys, as part of an exercise I was involved with, I had to use the factory pattern to create a series of different objects, however I'd like to keep a list or map for what it matters of these objects and I'm getting a bit into troubles here.
So basically, I have a CSV file containing some data which I'd like to use to construct my objects: the data in the spreadsheet is arranged in the following cols:
name, surname, date_of_birth, type
and I'm essentially reading from the spreadsheet and using the factory pattern I'm building a series of Employee objects, either full time or part time (that's stored inside the type column in the spreadsheet).
This has been done but I'd like to keep a list of all the employees, regardless of whether they are full time or part time and I'm not sure how and where to populate that list, that's why I got a bit stack.
Here is what I've done (in a nutshell)
- I have an abstract Employee class which is my model
- a FulltimeEmployee and ParttimeEmployee concrete classes implementing the EMployee class
- an EmployeeFactory class which determines whether the employee is part time or full time and builds the objects accordingly
- a ReadData class which reads the spreadsheet and calls the factory class with the data needed to build the objects
- a EmployeeTestingMain with main which creates a ReadData object and kicks off everything.
Ideally I'd like to keep a list of …
Violet_82 89 Posting Whiz in Training
Hi sorry for the long delay, but sadly it might be like that for a little while.
So, I have a couple of questions following your last post.
OK about what you said, will try to do it just with plain java and no json object, that's fine and I will create the objects using the empty constructors and use the set methods, that's fine
However, in the client I have either the encoded request (restRequest) which looks like this:
Customer+1
Product+2
As they are a combination of the class name and the ID or the fully generated response (generateJsonResponse) which looks like this
{"Customer":{"id":1,"name":Customer_1}}
{"Product":{"id":2,"name":Product_2}}
The server is doing all the substringing, string splitting but it returns the fully generated response already.
Now, for me to create the Customer and Product object in the client I will need to know what type of object I need to create, the name and the id which I need to extract from either the encoded request or from the fully generated response, so does it mean that I will have to "parse" the server response string again and extract all the relevant info? The thing is, how do I split a resonse string like {"Product":{"id":2,"name":Product_2}}
to get the bits I need?
Violet_82 89 Posting Whiz in Training
I see you are using JSONObject with it's horrible unchecked casts etc. Have fun with that!
Sorry I wasn't sure what else to use to create the json object and then create the objects from there.
You assume that the classes involved have a constructor that takes all the values you need, but the best you can rely on is that they are javabeans with a no-args constructor and set methods for everything.
So you're saying I shouldn't be creating objects like for the Produc and the Customer at all instead? But to set its value I need an object first anyway, so if I don't create it how would i set its value?
Violet_82 89 Posting Whiz in Training
Right, so now the client has some extra methods:
parseJsonResponse which parses the json and createObjFromJson which creates either a Product object or a Customer one.
The output is
Customer{id=1, name=Customer_1}
Product{id=2, name=Product_2}
...
String requestType = "GET";
String typeAccepted = "Accept:application/json";
String fullRequest = requestType + " " + encodedRequest + " " + typeAccepted;
String generateJsonResponse = server.generateJsonResponse(fullRequest);
parseJsonResponse(generateJsonResponse);
return (T) class1;
}
private void parseJsonResponse(String generateJsonResponse) {
String json = "";
try {
JSONObject obj = new JSONObject(generateJsonResponse);
createObjFromJson(obj);
} catch (Exception e) {
e.printStackTrace();
}
}
private void createObjFromJson(JSONObject obj) throws JSONException {
Integer id = null;
String name = null;
if(obj.has("Customer")) {
id = (Integer)obj.getJSONObject("Customer").get("id");
name = (String)obj.getJSONObject("Customer").get("name");
customer = new Customer(id, name);
System.out.println(customer);
}
else if(obj.has("Product")) {
id = (Integer)obj.getJSONObject("Product").get("id");
name = (String)obj.getJSONObject("Product").get("name");
product = new Product(id, name);
System.out.println(product);
}
}
Next I will see if I can have the Customer to have a list of products as you've suggested.
Violet_82 89 Posting Whiz in Training
The client now needs to parse the returned JSON string and create the corresponding Customer or Product.
OK, but just to clarify, when the client parses the response - let's say that I leave the returned json as it is without getting the server to split the request because the client now has the json string as I needed it to be I think - to create the Customer and Product object do you mean using JSONParser and JSONObject (or any other library for that matter) because I remember that in a previous thread you said that it might have been better not to use those in this example?
In the event that you are finding out about REST messages
I guess not yet :-)
Violet_82 89 Posting Whiz in Training
Right, so here is what I came up with, following your suggestions
The test class
package testClass;
import Client.Client;
import Server.Server;
import resources.Customer;
import resources.Product;
public class TestClass {
private static Client client;
private static Server server;
private static Customer customer;
private static Product product;
public static void main(String[] args) {
server = new Server();
client = new Client(server);
test();
}
public static void test() {
client.get(Customer.class, 1);
client.get(Product.class, 2);
}
}
has the test method calling the client's get.
Client
package Client;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import Server.Server;
import resources.Customer;
import resources.Product;
public class Client {
private Server server;
private Customer customer;
private Product product;
public Client(Server server) {
this.server = server;
}
public <T> T get(Class<T> class1, int ID) {
String restRequest = class1.getSimpleName() + " " + ID;
String encodedRequest = "";
try {
encodedRequest = URLEncoder.encode(restRequest, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String requestType = "GET";
String typeAccepted = "Accept:application/json";
String fullRequest = requestType + " " + encodedRequest + " " + typeAccepted;
server.generateJsonResponse(fullRequest);
return (T) class1;
}
}
The client also encodes the request and sends it to the server, well, calls a method in the Server class I should say
Server
package Server;
import java.net.URLEncoder;
import resources.Customer;
import resources.Product;
public class Server {
private Customer customer;
private Product product;
private Object getResource(String type, int id) {
switch (type) {
case "Customer":
return new Customer(id, "Customer_" + id);
case "Product":
return new Product(id, "Product_" + id);
}
return …
Violet_82 89 Posting Whiz in Training
I see you have it commented out, but that is the version you should use.
I did yes, but I've uncommented that after and used it
public <T> T get(Class<T> t, int id)
Yep, I figured that out after my post, :-)e tell me what class of object, and what it's ID (primary key) is, and I'll return you the instance of that class
Presumably that method, once we know what class has been passed to it, will have to create an instance of that class based so that I can get the class name and id and construct the REST request to pass to the server. If that method returns the instance of the class that makes me think that it will be the responsibility of the TestMethod to create the request.
What I'm saying I suppose is the get(Class<T> t, int id)
will do this in its body
find out which class has been passed to it
create a class of that type and populate its fields
use its fields to construct the request
pass the request to the server
Violet_82 89 Posting Whiz in Training
Sorry, I'm misunderstanding you? All the way at the bottom of the page? Do you mean at the bottom of the post?
That's OK, yes I meant that it's all the way down to the last post as I expected an "edit" button to be wither fixed positioned or for each thread, but that's I guess personal preferences.
I don't know of any forums or Q&A sites that have full size editor mode as the default. Certainly DaniWeb has never been that way.
If I remember correctly in the old days there was just a textx area to reply to the thread which wasn't kind of floating as the current one is and, again I seem to remember, it spanned across the whole width of the page whereas the current window noe is only like half of the width of the page and you can't make it bigger other than making it full width - sorry maybe that wasn't clear
Violet_82 89 Posting Whiz in Training
Hi sorry, still setting things up as I wasn't around that much yesterday and today
I set up the Customer and Product as you said.
Here are the CLient, Server and testClass so far
Client
package Client;
import Server.Server;
import resources.Customer;
import resources.Product;
public class Client {
private Server server;
private Customer customer;
private Product product;
public Server getServer() {
return server;
}
public void setServer(Server server) {
this.server = server;
}
public Client(Server server) {
this.server = server;
}
}
Server
package Server;
import resources.Customer;
import resources.Product;
public class Server {
private Customer customer;
private Product product;
public Object getResource(String type, int id) {
switch (type) {
case "Customer":
return new Customer(id, "Customer_" + id);
case "Product":
return new Product(id, "Product_" + id);
}
return null;
}
}
TestClass
package testClass;
import Client.Client;
import Server.Server;
import resources.Customer;
import resources.Product;
public class TestClass {
private static Client client;
private static Server server;
private static Customer customer;
private static Product product;
public static void main(String[] args) {
server = new Server();
client = new Client(server);
customer = (Customer) server.getResource("Customer", 4);
product = (Product) server.getResource("Product", 24);
//test();
}
public static void test() {
// System.out.println(client.get(Customer.class, 1));
// System.out.println(client.get(Product.class, 2));
// should print the toString for Customer and Product
System.out.println(customer);
System.out.println(product);
}
}
Violet_82 89 Posting Whiz in Training
That's fine :-), I'll set up the enviroment. I'll have a server package with a Client class and a server package with the Server class, then a resource package with the Customer and the Product. Once question, what do you mean when you say
(you'll need those in both the client and the server.)
Do you mean that both CLient and Server classes will have to hold an object of type Product and Customer?
Violet_82 89 Posting Whiz in Training
Right, thanks, haven't we done all that already?
Client is passed a resource name and an ID (eg "Client:, 123).
That's already done as we're passing the full URL String searchId = "123"; String fullUrl = "CLIENT/" + searchId;
to the client;
It constructs a GET and passes it to the Server
That's done too as the client is encoding the URL and then concatenate the other bits to it;
Parsing and getting the request are essentially faked already, then the Json string is build and passed back to the main class which prints everything?
Parsing and
Violet_82 89 Posting Whiz in Training
Looking good!
Great :-). So where to now? I will be reviewing the logic just to make sure i understand the various pieces, what do we do now? We need to take it a notch up of course, so what do we do now? Should I close this thread as this is the basics of REST and open another one?
Violet_82 89 Posting Whiz in Training
OK, so I've done it as above but then I added some spaces here and there, so ended up with this in the Client class (the main class is as above):
public String constructGetRequest(String url) throws Exception {
String encodedRequest = URLEncoder.encode(url, "UTF-8");
String requestType = "GET";
String typeAccepted = "Accept:application/json";
String fullRequest = requestType + " " + encodedRequest + " " + typeAccepted;
return fullRequest;
}
which yields GET CLIENT%2F123 Accept:application/json
which I guess it means, as you said earlier, that I can - if needed - split the string at the space for example.
For the second point, the json response, I removed the json objects and done with strings as you mentioned, so in the Server the getResponse method looks like this now
public String getResponse() {
String response = "{\"" + getResourceName() + "\":{\"id\":" + getID() + ",\"" + "price\":" + getPrice() + "}}";
return response;
}
And thsi yields the json response {"Dummy":{"id":1,"price":9.99}}
JamesCherrill commented: Looking good! +15
Violet_82 89 Posting Whiz in Training
I suggest you pass the raw URL to the method that builds the command
Right so something like this then:
public class TestRest {
...
public static void main(String[] args) throws Exception {
...
String searchId = "123";
String fullUrl = "CLIENT/" + searchId;
String getRequest = client.constructGetRequest(fullUrl);
...
which then, in the client class
public String constructGetRequest(String url) throws Exception {
String encodedRequest = URLEncoder.encode(url, "UTF-8");
String requestType = "GET";
String typeAccepted = "Accept:application/json";
String fullRequest = requestType + encodedRequest + typeAccepted;
return fullRequest;
}
Like this you pass just the URL that then gets parsed first and then the other bits added to it - although I would have to include the white space manually, for example space between GET and CLIENT/ as this now yields GETCLIENT%2F123Accept:application/json
Violet_82 89 Posting Whiz in Training
Thanks for the feedback.
Right, so , wait a sec:
The request URL needs to be encoded (it may contain blanks etc), but not the whole request.
So you mean I have to split String request = "GET CLIENT/123 Accept:application/json";
and only encode GET CLIENT/123
? What happens to the other bit, it doesn't get encoded and concatenated to the encoded one and passed on as well? That's a bit confusing, surely the server needs the second part of the string but why encode only the first part and not the second one? That's providing I understood what you were saying.
JSONObjectBuilder and JSONObject are useful, but I would suggest getting this working with some simple string concatenation first
Ah, I thought that would be all right :-). No worries, will do that with string concatenation as you suggested then.
Violet_82 89 Posting Whiz in Training
You can;t just delete significant charaters like that!
No, I can't, sorry, that was stupid.
So, this is what I found and how I dealt with it in the Client:
public String constructGetRequest() throws Exception {
String request = "GET CLIENT/123 Accept:application/json";
byte[] ptext = request.getBytes(ISO_8859_1);
String utfEncoded = new String(ptext, UTF_8);
String encodedRequest = URLEncoder.encode(utfEncoded, "UTF-8");
return encodedRequest;
}
given this input GET CLIENT/123 Accept:application/json
I get back GET+CLIENT%2F123+Accept%3Aapplication%2Fjson
. Presumably there is a corresponding decode method or something along those lines to decode.
On the Server class this is how I created the json response:
public String getResponse() {
String formattedJsonResponse = null;
JSONObject parentJsonObj = new JSONObject();
JSONObject childJsonObj = new JSONObject();
try {
//this will produce this format {"Dummy":{"price":9.99,"id":1}}
childJsonObj.put("id", getID());//add object key val
childJsonObj.put("price", getPrice());//add object key val
parentJsonObj.put(getResourceName(), childJsonObj);
formattedJsonResponse = parentJsonObj.toString();
} catch (JSONException e) {
e.printStackTrace();
}
return formattedJsonResponse;
}
Is that what we expected? Below I include the full classes for completeness and clarity (feel free to remove them if you think they don't add any value to the post - the TestRest.java hasn't changed)
Client.java
import static java.nio.charset.StandardCharsets.ISO_8859_1;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.net.URLEncoder;
public class Client {
Server server;
public Client(Server server) {
this.server = server;
}
public Server getServer() {
return server;
}
public void setServer(Server server) {
this.server = server;
}
public String constructGetRequest() throws Exception {
String request = "GET CLIENT/123 Accept:application/json";
byte[] ptext = request.getBytes(ISO_8859_1);
String utfEncoded = new String(ptext, UTF_8);
String encodedRequest …
Violet_82 89 Posting Whiz in Training
Hi all,
I haven't used the forum for a while so lot of the functionalities are new to me and I have to admit I found some of them really counter intuitive/annoying.
For a start, the way to reply to threads, that "Reply to this topic" button at the bottom that then turns into a small window which is incredibly annoying. Yes you can press esc and that turns into a full window but there you lose all the formatting options (code, quote etc): why can't we hav a normal full size window with all the options to reply to a thread as a default?
Then I tried to edit a post after I replied: I couldn't find an edit button to do so, that's extremely annoying and frustrating, where, if any, is the edit button?!
EDIT: about the edit button: I discovered that it's there, only it only appears at the bottom of the page so to see it you have to scroll all the way to the bottom first before thrying to edit something, still annoying, but less than what I first believed...
There might be more things, I will add when I discover them.
cheers
Violet_82 89 Posting Whiz in Training
Your request string is incomplete - see the examples linked earlier.
The URL should be UTF-8 encoded and % encoding all non alphanumerics
Sure sorry. It should be:
public String constructGetRequest() throws Exception {
String request = "GET CLIENT/123 Accept:application/json";
byte[] ptext = request.getBytes(ISO_8859_1);
String utfEncoded = new String(ptext, UTF_8);
String utfAndNonAlphaCharEncoded = encodeNonAlphanumericalChars(utfEncoded);
return utfAndNonAlphaCharEncoded;
}
private String encodeNonAlphanumericalChars(String toEncode) {
String encodedString = toEncode.replaceAll("[^A-Za-z0-9]", "%");
return encodedString;
}
This gets the UTF encoded and replaces the non alphanumerical characters to %.
But I have an issue - I think - converting the response in the Server class to Json
private final String resourceName = "Dummy";
private final int ID = 1;
private final double price = 9.99;
...
public String getResponse() {
return new String(getResourceName() + getID() + String.valueOf(getPrice()));
}
So, I think I need to clarify a few things, sorry.
Rather than the Server class returning a string, can't I get it to return the formatted Json rather than format it in the main() class? The thing is, if Server returns a string, it will return something like Dummy19.99
with or without spaces, not sure yet, but then I have to find a mechanism to get it to look like this
{
"dummy": {
"id": 1,
"price": 9.99
}
}
in the TestRest (main) class. Isn't is better to convert it to Json inside the Server class before it gets returned to the TestRest class?
One more thing: if we want that …
Violet_82 89 Posting Whiz in Training
OK, so I refactored the code a little.
Testing the class
public class TestRest {
private static Server server;
private static Client client;
public static void main(String[] args) throws Exception {
//server = new Server("Dummy", 1, 9.99);
server = new Server();
client = new Client(server);
String getRequest = client.contructGetRequest();
System.out.println(getRequest);
String response = server.getResponse();
System.out.println(response);
}
}
CLient
import static java.nio.charset.StandardCharsets.*;
public class Client {
Server server;
public Client(Server server) {
this.server = server;
}
public Server getServer() {
return server;
}
public void setServer(Server server) {
this.server = server;
}
public String contructGetRequest() throws Exception {
String request = "GET CLIENT/123";
byte[] ptext = request.getBytes(ISO_8859_1);
return new String(ptext, UTF_8);
}
}
Server
public class Server {
private final String resourceName = "Dummy";
private final int ID = 1;
private final double price = 9.99;
public String getResourceName() {
return resourceName;
}
public int getID() {
return ID;
}
public double getPrice() {
return price;
}
public String getResponse() {
return new String(getResourceName() + getID() + String.valueOf(getPrice()));
}
}
So here I'm just passing the simple string to the server and then getting the response (hardcoded string) as a response. Now, we said I have to stract the values from the string, but if the string is a single string like this Dummy19.99 I'm not sure I will have much luck in separating it and extracting the values as I don't have any separator or anything I could use to split the string - had it been insteal a …