I am working on an assignment with an online Udacity course, where we have been asked to develop a method that recognizes the last letter of the noun and then based on the letter, out put a "La", "el", or "?" + the noun.
The complete instructions are You are to complete the method fixNoun in the SpanishWord class so that it returns the noun preceded by:
// "la " if the noun ends in "a",
// "el " if it ends in "o"
// "? " if it ends in some other letter.
I seem to keep getting all failures when compiling. Can someone please help me understand what I am doing wrong?
public String fixNoun(String noun) {
String determinenet= noun.substring(noun.length() - 1);
if(determinenet.equals("a")) {
System.out.println("la" + " "+noun );
}
else if ( determinenet.equals("o")) {
System.out.println( "el"+ " "+noun);
}
else {
System.out.println("?"+ " "+noun);
}
return noun;
}
you don't return the altered noun but the old one. In your code you have to change the
System.out.println("la" + " "+noun );
to
return("la " + noun);
and so on.
The fixed code looks like this:
public String fixNoun(String noun) {
String determinenet= noun.substring(noun.length() - 1);
if(determinenet.equals("a")) {
return("la" + " "+noun );
}
else if ( determinenet.equals("o")) {
return( "el"+ " "+noun);
}
else {
return("?"+ " "+noun);
}
}
}
P.S.: The String class has a method endsWith() that gives you the last character of the string.
So you also could write your code like this:
public String fixNoun(String noun) {
if(noun.endsWith("a")) {
return("la " + noun);
else if(noun.endsWith("o")) {
return("el " + noun);
else {
return("? " + noun);
}
}
Good luck with your assignment :)
I don't know maybe i'm wrong. how about this?
String de=noun.charAt(noun.length()-1)
if(de.equalsIgnoreCase("a"))
return "la" + " "+noun ;
Related
I am trying to read the data from CSV file. everything works fine but when i try to read the data for longitude and latitude. it gives me an below error message.
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.yogi.guestlogix/com.example.yogi.guestlogix.MainActivity}: java.lang.NumberFormatException: Invalid double: "EVE"
Caused by: java.lang.NumberFormatException: Invalid double: "EVE"**
MainActivity.java
//Airports data begin
InputStream isss = getResources().openRawResource(R.raw.airports);
BufferedReader readerss = new BufferedReader(
new InputStreamReader(isss, Charset.forName("UTF-8"))
);
String liness = "";
//for airports.csv
try {
readerss.readLine();
while ((liness = readerss.readLine()) != null) {
//split by ',' first
String[] airport = liness.split(",");
//Read the data
AirportsData airdata = new AirportsData();
airdata.setAirportname(airport[0]);
airdata.setAirportcity(airport[1]);
airdata.setAirportcountry(airport[2]);
airdata.setAirportIATAcode(airport[3]);
//airdata.setAirportlang(Double.parseDouble(airport[4]));
//airdata.setAirportlat(Double.parseDouble(airport[5]));
AirportsDatas.add(airdata);
}
} catch (IOException e) {
Log.wtf("My Activity", "Reading data file error " + liness, e);
e.printStackTrace();
}
Log.d("Airline", "name is " + AirportsDatas);
AirportsData.java
public double getAirportlang(double airportlang) {
return airportlang;
}
public void setAirportlang(double airportlang) {
this.airportlang = airportlang;
}
public double getAirportlat( ) {
return airportlat;
}
public void setAirportlat(double airportlat) {
this.airportlat = airportlat;
}
#Override
public String toString() {
return "AirportsData{" +
"airportname='" + airportname + '\'' +
", airportcity='" + airportcity + '\'' +
", airportcountry='" + airportcountry + '\'' +
", airportIATAcode='" + airportIATAcode + '\'' +
", airportlang=" + airportlang +
", airportlat=" + airportlat +
'}';
}
**Any solution for this problem would be greatly appreciated....
The problem with CSV files is, that users (and other programmers) can pretty much write into the file what they want.
In your case, somebody entered a non-number into a CSV cell where you were expecting a double floating point number. Java correctly complains that this cannot be parsed to double. You have two approaches, to treat such situations:
A) Design by Contract
First, you could resolve that by relying on design by contract. You could have a parseLat method:
public Double parseLat(String[] csvRow) {
final String lat= csvRow[4];
assertIsNumeric(lat);
}
private void assertIsNumeric(String lat) {
if(!isNumeric(lat)) {
throw new IllegalArgumentException("Lattitude '" + lat + "' is not numeric");
}
}
There are countless options to implement an isNumeric method, some are discussed here. This will still stop the execution, but it will give a clearer message to users.
B) Defensive Design
A second option, is to provide a parseLAttitude method, that replaces non numeric value with null:
public Double parseLattitude(String[] csvRow) {
final String lattitude = csvRow[4];
if(!isNumeric(lattitude)) {
System.out.println("Could not parse lattitude '" + lat + "'");
return null;
}
return Double.parseDouble(lat);
}
I think the problem is that airport[4] & airport[5] might not be the lon and lat of the airport. If these field contain alpha characters [a-z] you will get an numberFormatException. Can you add the the output when you print airport like:
System.out.println(Arrays.asList(airport));
to the question? Than we will be better able to help you
I have a big text files and I want to remove everything that is between
double curly brackets.
So given the text below:
String text = "This is {{\n" +
"{{the multiline\n" +
"text}} file }}\n" +
"what I\n" +
"{{ to {{be\n" +
"changed}}\n" +
"}} want.";
String cleanedText = Pattern.compile("(?<=\\{\\{).*?\\}\\}", Pattern.DOTALL).matcher(text).replaceAll("");
System.out.println(cleanedText);
I want the output to be:
This is what I want.
I have googled around and tried many different things but I couldn't find anything close to my case and as soon as I change it a little bit everything gets worse.
Thanks in advance
You can use this :
public static void main(String[] args) {
String text = "This is {{\n" +
"{{the multiline\n" +
"text}} file }}\n" +
"what I\n" +
"{{ to {{be\n" +
"changed}}\n" +
"}} want.";
String cleanedText = text.replaceAll("\\n", "");
while (cleanedText.contains("{{") && cleanedText.contains("}}")) {
cleanedText = cleanedText.replaceAll("\\{\\{[a-zA-Z\\s]*\\}\\}", "");
}
System.out.println(cleanedText);
}
A regular expression cannot express arbitrarily nested structures; i.e. any syntax that requires a recursive grammar to describe.
If you want to solve this using Java Pattern, you need to do it by repeated pattern matching. Here is one solution:
String res = input;
while (true) {
String tmp = res.replaceAll("\\{\\{[^}]*\\}\\}", "");
if (tmp.equals(res)) {
break;
}
res = tmp;
}
This is not very efficient ...
That can be transformed into an equivalent, but more concise form:
String res = input;
String tmp;
while (!(tmp = res.replaceAll("\\{\\{[^}]*\\}\\}", "")).equals(res)) {
res = tmp;
}
... but I prefer the first version because it is (IMO) a lot more readable.
I am not an expert in regular expression, so I just write a loop which does this for you. If you don't have/want to use a regEx, then it could be helpful for you;)
public static void main(String args[]) {
String text = "This is {{\n" +
"{{the multiline\n" +
"text}} file }}\n" +
"what I\n" +
"{{ to {{be\n" +
"changed}}\n" +
"}} want.";
int openBrackets = 0;
String output = "";
char[] input = text.toCharArray();
for(int i=0;i<input.length;i++){
if(input[i] == '{'){
openBrackets++;
continue;
}
if(input[i] == '}'){
openBrackets--;
continue;
}
if(openBrackets==0){
output += input[i];
}
}
System.out.println(output);
}
My suggestion is to remove anything between curly brackets, starting at the innermost pair:
String text = "This is {{\n" +
"{{the multiline\n" +
"text}} file }}\n" +
"what I\n" +
"{{ to {{be\n" +
"changed}}\n" +
"}} want.";
Pattern p = Pattern.compile("\\{\\{[^{}]+?}}", Pattern.MULTILINE);
while (p.matcher(text).find()) {
text = p.matcher(text).replaceAll("");
}
resulting in the output
This is
what I
want.
This might fail when having single curly brackets or unpaired pair of brackets, but could be good enough for your case.
My user inputs work fine, my problem is I want an if statement that will say are both inputs equal?, but I get the attached error.
I need the code to be: if cork is entered in batman and robin?. This is what I tried:
System.out.println("From " + batman);
System.out.println("To " + Robin);
if(batman.equals("Cork") + Robin.equals("Cork") {
} else {
System.err.println("");
}
that here:
if(batman.equals("Cork") + Robin.equals("Cork") {
makes no sense because you are doing something like concatenating true with true or similar...
you have to do instead:
if(batman.equals("Cork") && Robin.equals("Cork") {
Your code should look like this :
System.out.println("From " + batman);
System.out.println("To " + Robin);
if(batman.equals("Cork") && Robin.equals("Cork")) {
// Statements
} else {
System.err.println("");
}
You should simply use && when requiring more then one test in an if statement.
+ doesn't work in such cases it simply concatenates values. Hope it helps :)
I have a string, let's call it output, that's equals the following:
ltm data-group internal str_testclass {
records {
baz {
data "value 1"
}
foobar {
data "value 2"
}
topaz {}
}
type string
}
And I'm trying to extract the substring between the quotes for a given "record" name. So given foobar I want to extract value 2. The substring I want to extract will always come in the form I have prescribed above, after the "record" name, a whitespace, an open bracket, a new line, whitespace, the string data, and then the substring I want to capture is between the quotes from there. The one exception is when there is no value, which will always happen like I have prescribed above with topaz, in which case after the "record" name there will just be an open and closed bracket and I'd just like to get an empty string for this. How could I write a line of Java to capture this? So far I have ......
String myValue = output.replaceAll("(?:foobar\\s{\n\\s*data "([^\"]*)|()})","$1 $2");
But I'm not sure where to go from here.
Let's start extracting "records" structure with following regex ltm\s+data-group\s+internal\s+str_testclass\s*\{\s*records\s*\{\s*(?<records>([^\s}]+\s*\{\s*(data\s*"[^"]*")?\s*\}\s*)*)\}\s*type\s*string\s*\}
Then from "records" group, just find for sucessive match against [^\s}]+\s*\{\s*(?:data\s*"(?<data>[^"]*)")?\s*\}\s*. The "data" group contains what's you're looking for and will be null in "topaz" case.
Java strings:
"ltm\\s+data-group\\s+internal\\s+str_testclass\\s*\\{\\s*records\\s*\\{\\s*(?<records>([^\\s}]+\\s*\\{\\s*(data\\s*\"[^\"]*\")?\\s*\\}\\s*)*)\\}\\s*type\\s*string\\s*\\}"
"[^\\s}]+\\s*\\{\\s*(?:data\\s*\"(?<data>[^\"]*)\")?\\s*\\}\\s*"
Demo:
String input =
"ltm data-group internal str_testclass {\n" +
" records {\n" +
" baz {\n" +
" data \"value 1\"\n" +
" }\n" +
" foobar {\n" +
" data \"value 2\"\n" +
" }\n" +
" topaz {}\n" +
" empty { data \"\"}\n" +
" }\n" +
" type string\n" +
"}";
Pattern language = Pattern.compile("ltm\\s+data-group\\s+internal\\s+str_testclass\\s*\\{\\s*records\\s*\\{\\s*(?<records>([^\\s}]+\\s*\\{\\s*(data\\s*\"[^\"]*\")?\\s*\\}\\s*)*)\\}\\s*type\\s*string\\s*\\}");
Pattern record = Pattern.compile("(?<name>[^\\s}]+)\\s*\\{\\s*(?:data\\s*\"(?<data>[^\"]*)\")?\\s*\\}\\s*");
Matcher lgMatcher = language.matcher(input);
if (lgMatcher.matches()) {
String records = lgMatcher.group();
Matcher rdMatcher = record.matcher(records);
while (rdMatcher.find()) {
System.out.printf("%s:%s%n", rdMatcher.group("name"), rdMatcher.group("data"));
}
} else {
System.err.println("Language not recognized");
}
Output:
baz:value 1
foobar:value 2
topaz:null
empty:
Alernatives: As your parsing a custom language, you can give a try to write an ANTLR grammar or create Groovy DSL.
Your regex shouldn't even compile, because you are not escaping the " inside your regex String, so it is ending your String at the first " inside your regex.
Instead, try this regex:
String regex = key + "\\s\\{\\s*\\n\\s*data\\s*\"([^\"]*)\"";
You can check out how it works here on regex101.
Try something like this getRecord() method where key is the record 'name' you're searching for, e.g. foobar, and the input is the string you want to search through.
public static void main(String[] args) {
String input = "ltm data-group internal str_testclass { \n" +
" records { \n" +
" baz { \n" +
" data \"value 1\" \n" +
" } \n" +
" foobar { \n" +
" data \"value 2\" \n" +
" }\n" +
" topaz {}\n" +
" } \n" +
" type string \n" +
"}";
String bazValue = getRecord("baz", input);
String foobarValue = getRecord("foobar", input);
String topazValue = getRecord("topaz", input);
System.out.println("Record data value for 'baz' is '" + bazValue + "'");
System.out.println("Record data value for 'foobar' is '" + foobarValue + "'");
System.out.println("Record data value for 'topaz' is '" + topazValue + "'");
}
private static String getRecord(String key, String input) {
String regex = key + "\\s\\{\\s*\\n\\s*data\\s*\"([^\"]*)\"";
final Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
//if we find a record with data return it
return matcher.group(1);
} else {
//else see if the key exists with empty {}
final Pattern keyPattern = Pattern.compile(key);
Matcher keyMatcher = keyPattern.matcher(input);
if (keyMatcher.find()) {
//return empty string if key exists with empty {}
return "";
} else {
//else handle error, throw exception, etc.
System.err.println("Record not found for key: " + key);
throw new RuntimeException("Record not found for key: " + key);
}
}
}
Output:
Record data value for 'baz' is 'value 1'
Record data value for 'foobar' is 'value 2'
Record data value for 'topaz' is ''
You could try
(?:foobar\s{\s*data "(.*)")
I think the replaceAll() isn't necessary here. Would something like this work:
String var1 = "foobar";
String regex = '(?:' + var1 + '\s{\n\s*data "([^"]*)")';
You can then use this as your regex to pass into your pattern and matcher to find the substring.
You can simple transform this into a function so that you can pass variables into it for your search string:
public static void SearchString(String str)
{
String regex = '(?:' + str + '\s{\n\s*data "([^"]*)")';
}
I need some help with arrays for admins and stuff. I'm using this API to make a server bot. You can find all the classes, constructors, and methods there.
Here's my current code:
import org.jibble.pircbot.*;
public class MyBot extends PircBot {
public MyBot() {
this.setName("DevilBot");
}
String owner = "Evan";
public void onMessage(String channel, String sender,
String login, String hostname, String message) {
if (message.equalsIgnoreCase("!time")) {
String time = new java.util.Date().toString();
sendMessage(channel, sender + ": The time is now " + time);
}
if (message.equalsIgnoreCase("!owner")) {
if(sender.equals(owner))
{
sendMessage(channel, Colors.NORMAL + "You're my owner silly!");
}
if (!sender.equals(owner))
{
sendMessage(channel, Colors.NORMAL + sender + ": " + owner + " is my owner!");
}
}
if (message.equalsIgnoreCase("!ban")) {
if(sender.equals(owner))
{
ban(channel, message);
sendMessage(channel, "Banned " + message);
}
else
{
kick(channel, sender);
sendMessage(channel, "You aren't my mother!");
}
}
if (message.equalsIgnoreCase("!version")) {
sendMessage(channel, "Version 0.1");
sendMessage(channel, "PircBot API v1.5.0");
}
if (message.equalsIgnoreCase("!aelux")) {
sendMessage(channel, "ALL HAIL AELUX!");
}
if (message.equalsIgnoreCase("!hates")){
sendMessage(channel, message + ", " + sender + " hates you!");
}
if (message.equalsIgnoreCase("!op")){
if(sender.equals(owner))
{
sendMessage(channel, "Opping " + message);
}
else
{
ban(channel, sender);
kick(channel, sender);
sendMessage(channel, "GTFO! Banned.");
}
}
}
}
It compiles and runs fine. And it's still in Alpha Stages, but for some reason it won't read my command. Like:
!kick user
Yields no response.
The API is quick and easy to understand. If you CAN help me, that would be awesome!
Right now in your code you are only using equalsIgnoreCase
This method requires both strings to match perfectly apart from the case.
So the reason "!ban user" would not work is because of the following.
"!ban".equalsIgnoreCase("!bAn") == true
"!ban".equalsIgnoreCase("!BaN") == true
but
"!ban".equalsIgnoreCase("!ban username") == false
"!ban".equalsIgnoreCase("!ban ") == false
This following piece of code would result in you being able to ban people by typing "!ban username". However it will be case sensitive.
if (message.startsWith("!ban")) {
if(sender.equals(owner))
{
String userToBan = message.split(" ")[1];
ban(channel, userToBan);
sendMessage(channel, "Banned " + userToBan);
}
else
{
kick(channel, sender);
sendMessage(channel, "You aren't my mother!");
}
}
If you don't want it to be just split the incoming command on a separator (usually space), and convert the first string and match them with equalsIgnoreCase.
String[] messageParts = string.split();
String command = messageParts[0];
if("!ban".equalsIgnoreCase(command){
ban(channel,messageParts[1])
if(messageParts[2].isEmpty()){
sendMessage(channel, "Banned " + messageParts[1]);
}else{
sendMessage(channel, "Banned " + messageParts[1] + " Reason: " + messageParts[2]);
}
}
Hope this helps