Best Way to Only Search In Partial Sections of An Array - java

Please do not provide actual code, just give me items to research on how to approach this, or try and steer me in the right direction.
I currently have an assignment where I'm to read a text file into a String array, then use a product number (pid) entered by the user to configure a car by putting each option into another array.
Here is the text file information that I've entered into a String array named anyCarConfig[]. I've included the index from the array to help illustrate this.
[0] - 0001 model1 10000.00
[1] - 0002 model2 20000.00
[2] -
[3] - Options
[4] - Exterior Color
[5] - 1 Blue 3000.00
[6] - 2 Red 3000.00
[7] - 3 Green 3000.00
[8] - 4 White 3000.00
[9] - 5 Black 3000.00
[10] - 6 Yellow 3000.00
[11] - 7 Orange 3000.00
[12] - 8 Gold 3000.00
[13] - 9 Purple 3000.00
[14] - Interior Color
[15] - 1 Leather-Tan 5000.00
[16] - 2 Leather-Black 5000.00
[17] - 3 Leather-Red 5000.00
[18] - 4 Vinyl-Tan 3000.00
[19] - 5 Vinyl-Black 3000.00
[20] - 6 Vinyl-Red 3000.00
[21] - 7 Cloth-Tan 1000.00
[22] - 8 Cloth-Black 1000.00
[23] - 9 Cloth-Red 1000.00
[24] - Powertrain
[25] - 1 Automatic 7500.00
[26] - 2 Manual 5000.00
[27] - Seat
[28] - 1 Multi-Point 800.00
[29] - 2 Heated 1200.00
[30] - 3 Air 2000.00
[31] - Radio
[32] - 1 AM/FM 500.00
[33] - 2 CD 750.00
[34] - 3 MP3 850.00
[35] - 4 Sirius/XM 900.00
[36] - 5 NAV 3000.00
[37] - Tire
[38] - 1 All-Weather 500.00
[39] - 2 Sports 1100.00
[40] - 3 Premium 1500.00
[41] - Rim
[42] - 1 18 1100.00
[43] - 2 19 1200.00
[44] - 3 20 1300.00
[45] - 4 22 1400.00
[46] - 5 26 2000.00
[47] - Miscellaneous
[48] - 1 MoonRoof 3000.00
[49] - 2 SunRoof 5000.00
[50] - 3 Panaramic 8000.00
Then here is the description on how I'm to come up with the different options.
getOptions
Type: Public Static
Input: String pid - 12-digit product ID number
Return: returns a String Array of options
Description: Reads the last 8 numbers of pid and identifies the options within anyCarConfig as follows:
Index 1. Exterior Color
Index 2. Interior Color
Index 3. Powertrain
Index 4. Seat Type
Index 5. Radio Type
Index 6. Tire Size
Index 7. Rim Size
Index 8. Miscellaneous
If the number is zero then the option is None. Each option is appended to an array with the option name and type as string example: “Exterior Color: White” once all options are defined then return the array of options.
Once again don't provide me full code, because I want to learn through some trial and error. Thank you. This is my first post so I hope I did this correctly.
Here is my current thought process. Just need to some guidance.
STEP ONE: Create loop to go through pid String starting at the 5th character and store each character as I go.
STEP TWO: Start at anyCarConfig[4] and add the value from the stored character of the pid String loop to get to the correct index of anyCarConfig[].
STEP THREE: Add that anyCarConfig[] index to the getOptions() String array that I will be returning.
STEP FOUR: Start at the anyCarConfig[] that got added to getOptions() String array and loop until I find the the next entry in anyCarConfig[] that starts with a character instead of a digit.
STEP FIVE: Go through the next iteration of the pid String loop
STEP SIX: Use the index found from STEP FOUR to be the start loop through like STEP TWO.
Repeat that whole process until I've reached the end of the pid String and my getOptions() String array is created.
I know I'm missing things in there like parsing, removing characters I don't need from the anyCarConfig[] values, etc, but I just wanted to give a general idea.

I personally would parse the available options first, and create several objects as categories. An object has a name (like exterior color) and a set of options. Optimally, you'd save these in a map: the key is the index of the digit which stands for the option, and the value, of course, is the option object itself.
Like:
Map<String, ConfigurationOption> options = new HashMap<>();
options.put(1, new ConfigurationOption("Exterior Color", "Blue", "Red", "Green", "White", "Black", "Yellow", "Orange", "Gold", "Purple");
And ConfigurationOption has a constructor like
public ConfigurationOption(String name, String ... options) {
...
}
Of course, you'd have to replace these hard coded strings with the data you parsed, but once you saved it this way, it's easy to access the data, because you just have to use options.get(index), while index stands for the index of the current digit of your product number.

Related

Remove an entry from a List<int[]> based on the value of of a specific index [duplicate]

This question already has answers here:
Using the non final loop variable inside a lambda expression
(3 answers)
Closed 3 years ago.
I have a list of ints like the following one:
1318 1065 0
1392 1109 0
1522 1114 2
1764 1134 0
1643 1172 0
1611 1141 0
1608 1142 4
1689 1180 0
1546 1144 0
1811 1121 1
1682 1144 0
1687 1203 0
1751 1138 0
1702 1227 0
My goal is to keep only entries where the third element is a 0.
I've tried a few things like:
for( int i=0; i<data_fin.size();i++) {
data_fin.removeIf(s -> !((data_fin.get(i)[2]) == 0));
}
I get the error 'Local variable i defined in an enclosing scope must be final or effectively final'.
Can anyone help me understand what I'm doing wrong? I'm quite new to Java and I'm probably using removeif wrong, so I would appreciate the help!
My goal is to keep only entries where the third element is a 0
You don't need a loop in this, all you need is :
data_fin.removeIf(a -> a[2] != 0);

regx matching from a file java

I am new to regx, but seems to be the only way I can solve the problem i have.
I was able to extract a text file from a CISCO switch using show vlan. The file looks like below. I just want to extact the vlan number and Name on array of strings. Where to begin.
VLAN Name Status Ports
---- -------------------------------- --------- -------------------------------
1 default active Gi0/1, Gi0/2, Gi0/4, Gi0/5
Gi0/6, Gi0/7, Gi0/8, Gi0/9
Gi0/10, Gi0/11, Gi0/12, Gi0/13
Gi0/14, Gi0/15, Gi0/16, Gi0/17
Gi0/18, Gi0/19, Gi0/20, Gi0/21
Gi0/22, Gi0/23, Gi0/24, Gi0/25
Gi0/26, Gi0/27, Gi0/28, Gi0/29
Gi0/30, Gi0/31, Gi0/32, Gi0/33
Gi0/34, Gi0/35, Gi0/37, Gi0/38
Gi0/39, Gi0/40, Gi0/41, Gi0/42
Gi0/43, Gi0/44, Gi0/45, Gi0/46
Gi0/47, Gi0/48, Gi0/49, Gi0/50
Gi0/51, Gi0/52
2 Test active Gi0/3
3 Internet active Gi0/36
1002 fddi-default act/unsup
1003 token-ring-default act/unsup
1004 fddinet-default act/unsup
1005 trnet-default act/unsup
VLAN Type SAID MTU Parent RingNo BridgeNo Stp BrdgMode Trans1 Trans2
---- ----- ---------- ----- ------ ------ -------- ---- -------- ------ ------
1 enet 100001 1500 - - - - - 0 0
2 enet 100002 1500 - - - - - 0 0
3 enet 100003 1500 - - - - - 0 0
1002 fddi 101002 1500 - - - - - 0 0
1003 tr 101003 1500 - - - - - 0 0
1004 fdnet 101004 1500 - - - ieee - 0 0
1005 trnet 101005 1500 - - - ibm - 0 0
Remote SPAN VLANs
------------------------------------------------------------------------------
Primary Secondary Type Ports
------- --------- ----------------- ------------------------------------------
^(\d+)\s+([^ ]+)
And then you will match VLAN number and type OR VLAN numer and name.
The explanation is in the first comment below this question.
The only thing I've changed is that the name or type may contain any characters except a blank space.

Predict function R returns 0.0 [duplicate]

I posted earlier today about an error I was getting with using the predict function. I was able to get that corrected, and thought I was on the right path.
I have a number of observations (actuals) and I have a few data points that I want to extrapolate or predict. I used lm to create a model, then I tried to use predict with the actual value that will serve as the predictor input.
This code is all repeated from my previous post, but here it is:
df <- read.table(text = '
Quarter Coupon Total
1 "Dec 06" 25027.072 132450574
2 "Dec 07" 76386.820 194154767
3 "Dec 08" 79622.147 221571135
4 "Dec 09" 74114.416 205880072
5 "Dec 10" 70993.058 188666980
6 "Jun 06" 12048.162 139137919
7 "Jun 07" 46889.369 165276325
8 "Jun 08" 84732.537 207074374
9 "Jun 09" 83240.084 221945162
10 "Jun 10" 81970.143 236954249
11 "Mar 06" 3451.248 116811392
12 "Mar 07" 34201.197 155190418
13 "Mar 08" 73232.900 212492488
14 "Mar 09" 70644.948 203663201
15 "Mar 10" 72314.945 203427892
16 "Mar 11" 88708.663 214061240
17 "Sep 06" 15027.252 121285335
18 "Sep 07" 60228.793 195428991
19 "Sep 08" 85507.062 257651399
20 "Sep 09" 77763.365 215048147
21 "Sep 10" 62259.691 168862119', header=TRUE)
str(df)
'data.frame': 21 obs. of 3 variables:
$ Quarter : Factor w/ 24 levels "Dec 06","Dec 07",..: 1 2 3 4 5 7 8 9 10 11 ...
$ Coupon: num 25027 76387 79622 74114 70993 ...
$ Total: num 132450574 194154767 221571135 205880072 188666980 ...
Code:
model <- lm(df$Total ~ df$Coupon, data=df)
> model
Call:
lm(formula = df$Total ~ df$Coupon)
Coefficients:
(Intercept) df$Coupon
107286259 1349
Predict code (based on previous help):
(These are the predictor values I want to use to get the predicted value)
Quarter = c("Jun 11", "Sep 11", "Dec 11")
Total = c(79037022, 83100656, 104299800)
Coupon = data.frame(Quarter, Total)
Coupon$estimate <- predict(model, newdate = Coupon$Total)
Now, when I run that, I get this error message:
Error in `$<-.data.frame`(`*tmp*`, "estimate", value = c(60980.3823396919, :
replacement has 21 rows, data has 3
My original data frame that I used to build the model had 21 observations in it. I am now trying to predict 3 values based on the model.
I either don't truly understand this function, or have an error in my code.
Help would be appreciated.
Thanks
First, you want to use
model <- lm(Total ~ Coupon, data=df)
not model <-lm(df$Total ~ df$Coupon, data=df).
Second, by saying lm(Total ~ Coupon), you are fitting a model that uses Total as the response variable, with Coupon as the predictor. That is, your model is of the form Total = a + b*Coupon, with a and b the coefficients to be estimated. Note that the response goes on the left side of the ~, and the predictor(s) on the right.
Because of this, when you ask R to give you predicted values for the model, you have to provide a set of new predictor values, ie new values of Coupon, not Total.
Third, judging by your specification of newdata, it looks like you're actually after a model to fit Coupon as a function of Total, not the other way around. To do this:
model <- lm(Coupon ~ Total, data=df)
new.df <- data.frame(Total=c(79037022, 83100656, 104299800))
predict(model, new.df)
Thanks Hong, that was exactly the problem I was running into. The error you get suggests that the number of rows is wrong, but the problem is actually that the model has been trained using a command that ends up with the wrong names for parameters.
This is really a critical detail that is entirely non-obvious for lm and so on. Some of the tutorial make reference to doing lines like lm(olive$Area#olive$Palmitic) - ending up with variable names of olive$Area NOT Area, so creating an entry using anewdata<-data.frame(Palmitic=2) can't then be used. If you use lm(Area#Palmitic,data=olive) then the variable names are right and prediction works.
The real problem is that the error message does not indicate the problem at all:
Warning message: 'anewdata' had 1 rows but variable(s) found to have X
rows
instead of newdata you are using newdate in your predict code, verify once. and just use Coupon$estimate <- predict(model, Coupon)
It will work.
To avoid error, an important point about the new dataset is the name of independent variable. It must be the same as reported in the model. Another way is to nest the two function without creating a new dataset
model <- lm(Coupon ~ Total, data=df)
predict(model, data.frame(Total=c(79037022, 83100656, 104299800)))
Pay attention on the model. The next two commands are similar, but for predict function, the first work the second don't work.
model <- lm(Coupon ~ Total, data=df) #Ok
model <- lm(df$Coupon ~ df$Total) #Ko

org.postgresql.util.PSQLException: No value specified for parameter 11

I have 10 parameters set in the statement with the question marks and provided 10 associated values. At execution, it’s throwing this error “org.postgresql.util.PSQLException: No value specified for parameter 11.” I’m having a similar issue with another table where it’s also asking for an out of range parameter. I can manually run the query against PostGres without any problem. Please see log below, thanks in advance.
[DBOperations]:INSERT INTO product.SHIB_RP_MD_PROVIDER(RP_MD_PROVIDER_ID,MD_PROVIDER_TYPE_ID,MD_ID,SRC_ORG_ID,MD_NAME,MD_DESC,PARENT_RP_MD_PROVIDER_ID,LAST_UPDATE_USER,LAST_UPDATE_DATE,SYSTEM_IND,ORG_ID) VALUES (?,?,?,?,?,?,?,?,date_trunc('second' , now()),?,?)
[DBOperations]:No value specified for parameter 11. org.postgresql.util.PSQLException: No value specified for parameter 11.
[DBOperations] - Index 1 - Value 10042
[DBOperations] - Index 2 - Value 4
[DBOperations] - Index 3 - Value aa
[DBOperations] - Index 4 - Value 2
[DBOperations] - Index 5 - Value aa
[DBOperations] - Index 6 - Value null
[DBOperations] - Index 7 - Value 0
[DBOperations] - Index 8 - Value 1234
[DBOperations] - Index 9 - Value 0
[DBOperations] - Index 10 - Value 2
INSERT INTO dah53idm.SHIB_RP_MD_PROVIDER(RP_MD_PROVIDER_ID,MD_PROVIDER_TYPE_ID,
MD_ID,SRC_ORG_ID,MD_NAME,
MD_DESC,PARENT_RP_MD_PROVIDER_ID,
LAST_UPDATE_USERID,
LAST_UPDATE_DATE,
SYSTEM_IND,ORG_ID) VALUES (?,?,?,?,?,?,?,?,date_trunc('second' , now()),?,?)
well answer is pretty obvious . but i'll point it out anyway, you have 11 question marks which means you are supposed to add 11 values to execute the query but in your code you are adding only 10 , hence the exception "no value specified for 11"
assign all the parameters to the values properly
for (User user : users) {
System.out.println("Inserting Data for Userr name" + user.getName());
jdbcTemplate.update("insert into USERR(Id,Name,Dept,Salary)
values(?,?,?,?)",
preparedStatement -> {
preparedStatement.setLong(1,user.getId());
preparedStatement.setString(2, user.getName());
preparedStatement.setString(3, user.getDept());
preparedStatement.setLong(4, user.getSalary());
});

SQL Pivot table for monthly aggregate amount

I've not much experience in sql.In my little java program using ucanaccess library I was able to perform some simple queries after creating a simple table (named ReportSales). The table is:
ID DATE PRODUCT SALES FEES
1 2014-10-02 productA 10.000 100
2 2014-09-02 productC 12.000 240
3 2014-09-02 productA 8.000 80
4 2014-11-02 productB 7.000 105
5 2014-08-02 productB 6.000 90
.. .......... ........ ...... ....
.. .......... ........ ...... ....
The last task is to create a pivot table in which I would insert the monthly sales per product. Something like:
PRODUCT AUG SEP OCT NOV
productA 0 8.000 10.000 0
productB 6000 0 0 7000
productC 0 12.000 0 0
And another pivot for monthly fees
My (wrong) attempt is:
SELECT [8] as AUG, [9] as SEP, [10] as OCT, [11] as NOV
FROM
(SELECT SALES,MONTH(DATE)
FROM ReportSales) AS tmp
PIVOT
(
SUM(SALES)
FOR MONTH(DATE) IN ([8], [9], [10], [11])
) AS PivotTable
Does anyone can help me? Thanks in advance
UCanAccess supports Pivot queries, only if they are built in Access, so you have firstly to create a crosstab query using the Access IDE.
Or you may simply create a new query in Access entering directly the following SQL:
TRANSFORM sum(ReportSales.sales) AS SalesCount
SELECT ReportSales.product
FROM ReportSales
GROUP BY ReportSales.product
PIVOT Format([date],"mmm");
Then assuming that you saved the query as query1, you have just to execute, with UCanAccess:
Select * from query1;

Categories

Resources