the same code when solving problems on codingbat works , but not in my IDE
public class Bark {
public static void main(String[] args) {
String lo = "hi there";
String shank = lo.substring(0 , 2);
if (shank.equals("hi")) return true;
else return false;
}}
My guess here is that your expectations have to with an artifact of some sort. Certainly, the check on shank should reveal that it is equal to hi. You are returning a boolean value from a method which is void, and has no return type. Consider the following version, which works as expected:
public static void main(String[] args) {
String lo = "hi there";
String shank = lo.substring(0 , 2);
if ("hi".equals(shank)) {
System.out.println("EQUAL");
}
else {
System.out.println("NOT EQUAL");
}
}
Perhaps CodingBat has some hooks into your code which aren't so obvious, resulting in the code "working" there, but not in your IDE.
Related
I have a program that takes letters for input and then sums the numeric value of each letter.
I have it so that if I input "abc", my output is "6".
I ignore uppercase letters, so if I input "abC", my output is "3".
What I want to do now, is in a separate class, make a method, which if set to true will run my main program as is, but when it is set to false, it will treat uppercase letters as lowercase, giving an input of "abC", an output of "6".
I hope this makes sense, I've tried a few different things but they all run the programm as is, ignoring uppercase.
Here is my code, I appreciate any constructive feedback.
Thanks
EDIT: I would also appreciate if you didn't downvote me for asking a question, if you don't want to help dont', seems every question I asked gets downvoted for no obvious or fair reason. I didn't want to ask for help since I knew this would happen. We all start have to somewhere!
Main method:
Scanner scan = new Scanner(System.in);
System.out.println("\nPlease enter the letters you would like to use:");
String s, t = "";
scan.next();
s = scan.next();
boolean b = Converter.caseSensitive(false, s);
scan.close();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (!t.isEmpty()) {
t += ",";
}
if (ch >= 'a' && ch <= 'z') {
int n = ch - 'a' + 1;
t += String.valueOf(n);
}
}
Second method in separate class:
public class Converter {
public static boolean caseSensitive(Boolean b, String s) {
for (char c : s.toCharArray()) {
if (Character.isLetter(c) && Character.isLowerCase(c)) {
b = s.equalsIgnoreCase(s);
return false;
}
}
s = s.toLowerCase();
return true;
}
}
I believe your question is "how do I record a static boolean value in a class and then request it from another class?"
public class Configuration {
private static boolean convertToUppercase = true;
public static void setConvertToUppercase(boolean convert) {
convertToUppercase = convert;
}
public static boolean getConvertToUppercase() {
return convertToUppercase;
}
}
This can be used as:
StringConverter.caseSensitive(Configuration.getConvertToUppercase(), input);
Note that most coders (me included) would consider this poor design but explaining why is outside the scope of your question.
There are a lot of other issues with your code. For example your method call above will leave the input string unchanged. But I suggest you ask another question with just the relevant code when you get stuck.
String is immutable in Java. Please read following stackoverflow question for more information about this topic:
String is immutable. What exactly is the meaning?
public static void main(String[] args) throws Exception
{
String test = "abc";
toUpperCase(test);
System.out.println(test);
}
private static void toUpperCase(String test)
{
test = test.toUpperCase();
}
Please note that above code will output:
abc
In order to have "ABC" as result you need to use following code:
public static void main(String[] args) throws Exception
{
String test = "abc";
test = toUpperCase(test);
System.out.println(test);
}
private static String toUpperCase(String test)
{
return test.toUpperCase();
}
This one outputs:
ABC
So your Converter.caseSensitive method should return String.
I don't think you really need the Converter class. You can delete class and replace the line:
boolean b = Converter.caseSensitive(false, s);
with this
boolean shouldCountUppercaseLetters = false;
if (shouldCountUppercaseLetters) {
s = s.toLowerCase();
}
import java.util.Scanner ;
public class ProcessNumbers
{
public static void main( String[] args )
{
Scanner in = new Scanner(System.in) ;
System.out.print("Please enter an integer between 6 and 12, inclusive: ") ;
int num = in.nextInt() ;
boolean result = shouldProcess(num);
String result1 = String.valueOf(result) ;
}
public static boolean shouldProcess(int n)
{
if (n>=6 && n<12)
{
return true;
}
else
{
return false;
}
}
public static boolean processInput(boolean result2)
{
if (result2 == true)
{
System.out.println("Yes") ;
}
else
{
System.out.println("No") ;
}
return result2 ;
}
}
now I am getting the output which is partially right but has forgot the yes or no output in the second method
Please enter an integer between 6 and 12, inclusive:
when it should also include the yes or not output
You are sending in a boolean value in the method parameter of processInput but you are catching it as a String. You need to change it to boolean. Further, you want to check if its value is true with equal signs like below:
public static void processInput(boolean result2)
{
if (result2 == true)
{
System.out.println("Yes") ;
}
else
{
System.out.println("No") ;
}
}
EDIT 2:
Also, you need to change String result1 = String.valueOf(result); to processInput(result);
EDIT 3:
If you want the number printed too that you just entered and then you want a "yes" or "no", then between int num = in.nextInt(); and boolean result = shouldProcess(num);, add this line: System.out.println(num);
There's apparently some code missing, so I'm guessing this is just part of the full thing. So I will only tackle your output issue.
I won't talk about the code in: public static boolean processInput(boolean result2), because you're not running it anywhere in your main method public static void main( String[] args ) anyway.
Now, in your code at:
public static boolean shouldProcess(int n)
if you look at your code, you are assigning the value of the boolean to the new String result1, so result1 now has the new value, but you are not running its output anywhere, so there's no way the program can guess you want to output that value. You need to assign the output:
System.out.print(result1);
However, if you only want to output the boolean, there's no need to assign that boolean value to a new String and then output the new String, you could just:
System.out.print(result);
Unless you're going to use that value somewhere else where creating a new variable would arguably be a good choice.
Also, it seems you want to return either a "Yes" or "No" on your class: public static boolean processInput(boolean result2).
Remember a class that does not return a value, but rather executes a code, has to be written as void. In other words, your:
public static boolean processInput(boolean result2)
should really be:
public static void processInput(boolean result2)
Because if not, you are just making your program return result2;, which in this case can only be either true or false. By adding void to the class, makes the class understand it will be executing your System.out.print code, rather than returning a value for you to use. But also, depends on what you want to afterwards.
I'm new to Java and i don't have much experience programming. I've been working with this code for a while now, and I'm not really sure how to make it work.
public class Bases {
public static void main(String[] args) {
String base = args[0];
char valid = args[0].charAt(0);
char[] newvalid = { 'A', 'G', 'C', 'T'};
if (valid == newvalid)
return valid;
else
System.out.println("Not a valid base");
}}
So here are my questions:
1. Is it possible to mix char[] and char?
2. And can someone explain why you "cannot return a value from method whose result type is void"?
Any help would be appreciate.
Mixing types is not a java concept however you can compare, which is what you are looking for. Since newvalid is an array lets loop it and see if valid is inside.
boolean contains = false;
for (char c : newvalid) {
if (c == valid) {
contains = true;
break;
}
}
if (contains) {
// do your stuff
}
cannot return a value from method whose result type is void
Means that in method with return declaration void you can not return a value, hmm maybe that's exactly what is in the message...
I will highlight your code so that you can understand
public static void main(String[] args) //This is your method, see the void as return type
.....
return valid; //Here you try to return a char and this is not allowed, since it is declared void
To solve compilation problem, change return valid; to return;
This question already has answers here:
How do I determine whether an array contains a particular value in Java?
(30 answers)
Closed 7 years ago.
I want to compare from a void main method where there is a array and a string. so what I want to do is to compare with if name is in the array. return true else return false. But I cant get it to work. It gives me false on both. And I don't know why?
public boolean isActor(String name) {
if(name.equals(actors)) {
return true;
}else {
return false;
}
}
public static void main(String[] args) {
String[] actors = {"Ulla Skoog", "Suzanne Reuter", "Peter Dalle"} ;
Dvd d1 = new Dvd(10327, "Yrrol", "Peter Dalle", actors, 88);
System.out.println("Medverkar Kalle Kula i Lorry: " + d1.isActor("Kalle kula"));
System.out.println("Medverkar Ulla Skoog i Lorry: " + d1.isActor("Ulla Skoog"));
}
Any help would be appreciated !
EDIT: So I tried to do if(Arrays.asList(actors).contains(name)) and yes it worked. Problem is I might now be allowed to do it under a test which can make me losing points and I tried to make a for loop and by that make a equals statement but still getting the same result which is both false.
EDIT2: Also tried to do this
public boolean isActor(String name) {
for(String s: actors){
if(s.equals(actors))
return true;
}
return false;
}
but still got the same result of both false
EDIT3:
What I want to do is to make e method which is ( public boolean isActor(String name) { ) and with that I want to make a if-statement which should make a algorithm by saying "If this name is on this array. make it say true. If its not in the array. Say its false." Thats what im trying to do.
You have to test each item in array:
public boolean isActor(String name) {
for (String actor : actors) {
if (name.equals(actor) {
return true;
}
}
return false;
}
public boolean isActor(String name, List<String> actorList) {
if(actorList.contains(name)) {
return true;
}else {
return false;
}
}
public static void main(String[] args) {
String[] actors = {"Ulla Skoog", "Suzanne Reuter", "Peter Dalle"} ;
List<String> actorList = Arrays.asList ( actors);
Dvd d1 = new Dvd(10327, "Yrrol", "Peter Dalle", actors, 88);
System.out.println("Medverkar Kalle Kula i Lorry: " + d1.isActor("Kalle kula",actors));
System.out.println("Medverkar Ulla Skoog i Lorry: " + d1.isActor("Ulla Skoog",actors));
}
I am interested in a very simple string verification problem to see if the starting character in a string starts with an upper case letter and then have the console to display true or false. From my understanding you wouldn't have to invoke something like System.console().printf("true", s) in order to make this happen. I could swear I've seen similar elementary implementations achieved using the following sample code:
public class Verify {
public static boolean checkStartChar(String s) {
if (s.startsWith("[A-Z]")) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
String str = "abCD";
checkStartChar(str);
}
}
but when I run this, nothing displays. If I make a slight modification by adding in conditional printouts before returning T/F, e.g.
public class Verify2 {
public static boolean checkStartChar(String s) {
if (s.startsWith("[A-Z]")) {
System.out.println("yep");
return true;
}
else {
System.out.println("nope");
return false;
}
}
public static void main(String[] args) {
String str = "abCD";
checkStartChar(str);
}
}
the issue is somewhat resolved, as the console displays either "yep" or "nope", yet unresolved because I just want the console to display true or false. That's it. Advice?
As the question has already been answered, I'd like to point out there is no need for RegExes to solve this (and they are expensive operations). You could do it simply like this:
static boolean startsWithUpperCase(String toCheck)
{
if(toCheck != null && !toCheck.isEmpty())
{
return Character.isUpperCase(toCheck.charAt(0));
}
return false;
}
yet unresolved because I just want the console to display true or false
Calling checkStartChar method will return value, that doesn't mean it will print value to console. You need to code how you would like to handle return value. If you want to print return value, then you should do:
System.out.println(checkStartChar(str));
Will print what ever the return of checkStartChar method
if(s.startsWith("[A-Z]")){
String.startsWith(prefix) doesn't take regex as a parameter, you should be using regex APi instead.
Pattern p = Pattern.compile("[A-Z]");
Matcher m = p.matcher(new Character(s.charAt(0)).toString());
if(m.find()){
return true;
}else{
return false;
}
String str = "AbCD";
System.out.println(checkStartChar(str));
Output:
true
In your code checkStartChar(str); is returning a boolean value which is not being used in your program.Then if you want to display true or false then you can use.
System.out.println(checkStartChar(str));