Creating a Class from a text document - c#-4.0

I am very, very new to programming and I'm currently trying to learn it as fast as I can as a requirement of my research. I am trying to create a class from a text document. I have produced the code below, however it is returning errors in when I debug which I am unsure of how to fix.
List<Partitions> ListofPartitions = new List<Partitions>();
System.IO.StreamReader sr = new System.IO.StreamReader("C:\\Partitions.csv")
{
string line = sr.ReadToEnd();
Partitions Pname = new Partitions(line);
Partitions Plength = new Partitions(line);
Partitions Pdepth = new Partitions(line);
Partitions Pheight = new Partitions(line);
}
}
public class Partitions
{
string parameters;
public int Pname;
public double Plength;
public double Pdepth;
public double Pheight;
public Partitions(string inputLine)
{
parameters = inputLine;
string[]split = parameters.Split(',');
Pname = Convert.ToInt16(split[0]);
Plength = Convert.ToDouble(split[1]);
Pdepth = Convert.ToDouble(split[2]);
Pheight = Convert.ToDouble(split[3]);
}
Returning errors:
GA.xaml.cs(39,24): error CS1525: Invalid expression term 'string'
GA.xaml.cs(39,45): error CS1003: Syntax error, ',' expected
Any help would be much appreciated.

I would bet it is
string[]split = ...
there should be space between [] and split like this
string[] split = ...

Related

Passing an Int and String argument to an arrayList index and String element

import java.util.ArrayList;
public class DataBase {
public ArrayList<String> nameList = new ArrayList<>();
public void nameAdd(String s, int i) {
if (i < nameList.size()){
nameList.add(i, s);
//string not adding to arrayList, ive probably done it wrong.
}
System.out.println(s);
System.out.println("nameList.size = " + nameList.size());
System.out.println("arrayList number = " + i);
}
}
I've been coding for about a month now so please excuse my noobieness but i'm trying to basically take a string from a GUI i've built, cut it up to get a name, and add that name to a "database" when the user clicks another button to add it to it. Ive already been able to increment the i which is supposed to set an index for the string within the arrayList but the arrayList isnt increasing in size and I get a whole bunch of errors thus the if statement ive added to stop it from trying to add to an index that isnt yet in the arrayList.
In your code I see your array nameList always empty.
Because you have this line:
public ArrayList<String> nameList = new ArrayList<>();
In this moment you create your array (empty), but in your nameAdd method if you pass this at the first time:
nameAdd('ADDFIRSTELEMENT', 0)
Your if statemnt is always false
if (i < nameList.size()){
nameList.add(i, s);
//string not adding to arrayList, ive probably done it wrong.
}
You must to change your check as follow:
if (i <= nameList.size())
Because if your index is the same of the size, you can add your element in the array.
If you want you can use the contains method of your nameList ArrayList object so you can see if your string is present in your array

Scanning HBase based on two cells

Let's say I have two HBase cells:
x:y
x:z
How do I do the equivalent of this SQL:
SELECT * FROM some_table WHERE x_y = ? AND x_z = ?
This is the (Groovy) code I have for generating the basic filters:
static SingleColumnValueFilter makeColumnFilter(String family, String qualifier, String expectedValue) {
new SingleColumnValueFilter (
Bytes.toBytes(family),
Bytes.toBytes(qualifier),
CompareFilter.CompareOp.valueOf('EQUAL'),
new SubstringComparator(expectedValue))
}
def filterz = filters.collect {
makeColumnFilter(it.family, it.qualifier, it.expectedValue)
}
def fl = new FilterList(filterz)
def scan = new Scan()
scan.setFilter(fl)
def family = 'x'.bytes
t.getScanner(scan).each {
println "${Bytes.toString(it.getValue(family, 'y'.bytes))}"
count++
}
The print statement shows nothing but nulls even though I'm passing in x for the family value and y/z for the qualifiers. It appears to not be filtering the values. What am I doing wrong?
You need to filter the rows if the column is not found using setFilterIfMissing.
Change the makeColumnFilter to :
static SingleColumnValueFilter makeColumnFilter(String family, String qualifier, String expectedValue) {
def colFilter = new SingleColumnValueFilter (
Bytes.toBytes(family),
Bytes.toBytes(qualifier),
CompareFilter.CompareOp.valueOf('EQUAL'),
new SubstringComparator(expectedValue))
colFilter.setFilterIfMissing(true)
colFilter
}
I think SingleColumnValueExcludeFilter should do the job.
Scan scan = new Scan();
SingleColumnValueExcludeFilter singleColumnValueFilterY = new SingleColumnValueExcludeFilter("x".getBytes(),
"y".getBytes(), CompareOp.EQUAL, new BinaryComparator("valueY".getBytes()), true, true);
SingleColumnValueExcludeFilter singleColumnValueFilterZ = new SingleColumnValueExcludeFilter("x".getBytes(),
"z".getBytes(), CompareOp.EQUAL, new BinaryComparator("valueZ".getBytes()), true, true);
FilterList filterList = new FilterList(Operator.MUST_PASS_ALL);
filterList.addFilter(singleColumnValueFilterX);
filterList.addFilter(singleColumnValueFilterY);
scan.setFilter(filterList);
More detailed documentation on HBase filters could be found here.
Hope this will help.

Saving a string found with .contains as a String variable?

First of all, I don't have any code to show yet, but the idea is that there is a text document that contains information, say... 'John Fitzgerald New York' on a single line, and I want to find that via .contains(), for example:
Scanner newScanner = new Scanner(inputFile);
String name = "Fitzgerald";
while(!newScanner.nextLine().contains(name)){
}
The idea being that I can then save the entire line as a variable. A search for Fitzgerald should allow me to save John Fitzgerald New York as a variable, in other words. Any ideas?
Scanner sc = new Scanner(new File("input.txt")); // Read from file
final String pattern = "Fitzgerald"
while( sc.hasNext() ){ // While you still have more data
String l = sc.next(); // Get the next token
if( l.contains(pattern) ){ // Check if it matches your pattern
System.out.println("Match Found");
}
}
This is how you would do it if you want to loop over the tokens. Alternatively, you can use the next(Pattern) method if you want to find more complex patterns.
For a text document, consider using a FileReader.
final String pattern = "Fitzgerald"
FileReader f = new FileReader(new File("input.txt"));
BufferedReader b = new BufferedReader(f);
String line;
while( (line=b.readLine()) != null ){
if(line.contains(pattern)){
doSomething(line);
}
}

JPA string comparison

I've written a simple login system using a JPQL query, which always returns no result:
public boolean check(String name, String password) {
final String qstring="SELECT e FROM Muser e WHERE e.name = '"+name+"'";
Muser user;
try{
user = em.createQuery(qstring, Muser.class).getSingleResult();
}
catch(NoResultException e){
return false;
}
return password.equals(user.getPassword());
}
When I changed it to a native query:
user = (Muser) em.createNativeQuery(qstring, Muser.class).getSingleResult();
or an int expression:
final String qstring="SELECT e FROM Muser e WHERE e.id = "+id;
It goes all right. What's the problem? Thanks a million!
It might be a problem with string comparison in your JPA provider. Do you test it on the case-sensitive data?
You could also try (and it's the preferred way) using parameters instead of crafting your statement by hand. It's not only safer (prevents SQL injection) but also faster: not only for Java (you don't concatenate Strings) but also for the DB (the query can be prepared once for all executions). It might be something like this:
final String qstring = "SELECT e FROM Muser e WHERE e.name = :name";
TypedQuery<Muser> query = em.createQuery(qstring, Muser.class);
query.setParameter("name", name);
user = query.getSingleResult();
I think problem is because of String comparison.My solution to this problem is:
using lowercase for comparison.
final String qstring = "SELECT e FROM Muser e WHERE LOWER(e.name) = :name";
TypedQuery<Muser> query = em.createQuery(qstring, Muser.class);
query.setParameter("name", name.toLowerCase());
user = query.getSingleResult();

Subsonic 3 - Sequence contains no matching element

I need help creating a LINQ SQL with subsonic. First the basics, this works fine:
var query = (from o in bd.concelhos
orderby o.descricao
select o);
var results = query.ToList<concelhos>();
However, I want to filter out some columns and I have created the following code:
var query = (from o in bd.concelhos
orderby o.descricao
select new FilteredConcelhos { id = o.idDistrito + "/" + o.idConcelho, descricao = o.descricao });
var results = query.ToList<FilteredConcelhos>();
which errors out in the ToList method with the description "Sequence contains no matching element"
Any help would be great with this...
update:
Turns out I was missing get set attributes in the newly declared class...
Like so
public class FilteredConcelhos
{
public string id { get; set; }
public string descricao { get; set; }
}
This clears the exception, but the resulting List is still all wrong (FilteredConcelhos.id contains nothing and FilteredConcelhos.descricao contains numbers)
Can you try to first execute the ToList and the select afterwards - then the select is performed via linq 2 objects!
Have you tried to work with an anonymous type?
var query = (from o in bd.concelhos
orderby o.descricao
select new { id = o.idDistrito + "/" + o.idConcelho,
descricao = o.descricao });
var results = query.ToList();
Unfortunately, this happened to me a lot. I'm not sure about the details of how Linq 2 Object works, but if you'll call ToList on the original object, like this:
from o in bd.concelhos.ToList()
...
It should do the trick.

Resources