I'm very new to Java and I was looking for some help on this particular problem.
I need to create a method that takes a String as a parameter and returns "true" if the parameter is the letter Y or the letter N (in either upper or lower case), or false otherwise.
Most of the methods we've been working on are numbers (integers mostly) not strings, so this one confuses me quite a bit. How do you make the computer look for a certain character like y, Y, n, N. I don't want to declare them as variables, I just want Java to see them as letters.
I have:
public static boolean isYorN(String str)
{
boolean character;
switch (character)
{
case 'y':
character = true;
return true;
break;
case 'Y':
character = true;
return true;
break;
case 'n':
character = true;
return true;
break;
case 'N':
character = true;
return true;
break;
default:
character = false;
return false;
}
}
}
I think a switch case is the best method, but I'm missing something about naming because all my compiling errors are telling me that I have incompatible types (i.e. found: boolean, required: int).
Thank you in advance for any help.
11 3236
I believe the errors are coming from the fact that you are using 'Y', 'y', 'N', and 'n' as the cases, when you tell the switch statement (swtich (character)) that it should expect a boolean - either a true or a false.
I would recommend using the .equals() function in the String class. That returns a boolean, so you can resolve it as being either true or false (use the fact that it will return as either true or false).
(And in future posts, please use [code ] and [/code ] around your code - only without the spaces at the end, it really helps with readability. Thanks!)
I believe the errors are coming from the fact that you are using 'Y', 'y', 'N', and 'n' as the cases, when you tell the switch statement (swtich (character)) that it should expect a boolean - either a true or a false.
I would recommend using the .equals() function in the String class. That returns a boolean, so you can resolve it as being either true or false (use the fact that it will return as either true or false).
(And in future posts, please use [code ] and [/code ] around your code - only without the spaces at the end, it really helps with readability. Thanks!)
Can PvtBillPilgrim use scanner and do this
if(kbd.equalsIgnoreCase("Y"));
then use the switch or if statement
...
I am a beginner programmer in Java. Just started learning the past few weeks.
I'm taking a class and need to create a method that finds the greatest common divisor of two integers. I can assume that both are positive, but I cannot use the Euclidean Algorithm.
I'm sort of lost, but I think I'm on the right track, although this may look confusing: -
public static int gcd(int a, int c)
-
{
-
int gcd;
-
int attempt;
-
if (a > c)
-
{
-
if (a % c == 0)
-
{
-
gcd = c;
-
return gcd;
-
}
-
else
-
{
-
for (attempt = c; attempt = 1; attempt--)
-
do
-
{
-
if (c % attempt == 0 && a % attempt == 0)
-
{
-
gcd = attempt;
-
return attempt;
-
}
-
}
-
while (c % attempt != 0 || a % attempt == 0);
-
}
-
}
-
And then I basically copied the code with an else statement if c > a.
I'm sure there are a lot of mistakes. Could someone just point them out and offer a simpler way of doing it (possibly without using the Euclidean Algorithm)?
Thanks.
Michael
Hint: for any two (positive) numbers a and c the gcd is the largest number in the
range [1 ... min(a, c) ]. Only a single for loop could do the job. There's no need
to duplicate code for a < c or c < a and there's also no need for two nested loops.
kind regards,
Jos
....I think a switch case is the best method...
I'm afraid you're wrong there and that's where you missed it. Just an if else will do it.
big hint: - if(str.equalsIgnoreCase("y")) ....
I am a beginner programmer in Java. Just started learning the past few weeks.
I'm taking a class and need to create a method that finds the greatest common divisor of two integers. I can assume that both are positive, but I cannot use the Euclidean Algorithm.
I'm sort of lost, but I think I'm on the right track, although this may look confusing: -
public static int gcd(int a, int c)
-
{
-
int gcd;
-
int attempt;
-
if (a > c)
-
{
-
if (a % c == 0)
-
{
-
gcd = c;
-
return gcd;
-
}
-
else
-
{
-
for (attempt = c; attempt = 1; attempt--)
-
do
-
{
-
if (c % attempt == 0 && a % attempt == 0)
-
{
-
gcd = attempt;
-
return attempt;
-
}
-
}
-
while (c % attempt != 0 || a % attempt == 0);
-
}
-
}
-
And then I basically copied the code with an else statement if c > a.
I'm sure there are a lot of mistakes. Could someone just point them out and offer a simpler way of doing it (possibly without using the Euclidean Algorithm)?
Thanks.
Michael
Hi,
I modified ur function to find GCD of two numbers.
Try this function repl ur feedback
[edit] Don't supply full spoonfed code; it's not allowed in this forum. Thanks;
Thanks,
Srinivas r.
OK. I think this is a lot better. It compiles and makes sense to me at least. However, it doesn't work properly for more difficult integers like 20, 30. Can anyone spot the problem (as I said nothing wrong with compiling, just doesn't work logically). As I said before, I can't use the Euclidean Algorithm, so I basically had to write the simplest program to find gcd. -
public static int gcd(int a, int b)
-
{
-
int gcd;
-
if (a > b && a % b == 0)
-
{
-
gcd = b;
-
return gcd;
-
}
-
else if (b > a && b % a == 0)
-
{
-
gcd = a;
-
return gcd;
-
}
-
else if (a > b && a % b != 0)
-
{
-
int count = b;
-
do
-
{
-
count = count - 1;
-
}
-
while (b % count != 0 && a % count != 0);
-
gcd = count;
-
return gcd;
-
}
-
else if (b > a && b % a != 0)
-
{
-
int count = a;
-
do
-
{
-
count = count - 1;
-
}
-
while (b % count != 0 && a % count != 0);
-
gcd = count;
-
return gcd;
-
}
-
else
-
{
-
gcd = a;
-
return gcd;
-
}
-
}
-
I'm not 100% sure about the forum guidelines, but I'm pretty sure you should post this as a new thread/topic because it is indeed a ... new... topic. Anyways, may I ask why you're using subtraction and not mod? What do you mean you can't use the euclidean algorithm?
Ok, you're subtracting by one each time... why? Why not subtract by intervals of the smaller number (original euclidean algorithm) or use the mod method?
You realize there's a Math.max(int a, int b) method?
I don't know what is logically wrong in your code, but you don't have a good base of an idea, try rewriting and also tell us what exactly outputs when you have higher integers.
-blazed
I'm afraid you're wrong there and that's where you missed it. Just an if else will do it.
big hint: - if(str.equalsIgnoreCase("y")) ....
perhaps simply
return str.equalsIgnoreCase("y") || str.equalsIgnoreCase("n");
OK. I think this is a lot better. It compiles and makes sense to me at least. However, it doesn't work properly for more difficult integers like 20, 30. Can anyone spot the problem (as I said nothing wrong with compiling, just doesn't work logically). As I said before, I can't use the Euclidean Algorithm, so I basically had to write the simplest program to find gcd.
Oh man, why so complicated? Here's some pseudo code to do it (non-Euclidean-style, why not Euclidean, btw?): - public static int gcd(int a, int b) {
-
-
counter <- the minimum value between 'a' and 'b'
-
-
WHILE counter is more than 0
-
IF 'counter' divides 'a' AND 'counter' divides 'b'
-
break out of this loop
-
END IF
-
decrease 'counter'
-
END WHILE
-
-
return 'counter'
-
}
Good luck.
Oh man, why so complicated? Here's some pseudo code to do it (non-Euclidean-style, why not Euclidean, btw?):
Yup, exactly what I suggest in the thread where the sequel of this
thread belongs ;-)
kind regards,
Jos
ps. I just merged the two threads.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Dave Rahardja |
last post by:
OK, so I've gotten into a philosophical disagreement with my colleague at
work. He is a proponent of the Template Method pattern, i.e.:
class foo
{
public:
void bar() { do_bar(); }
protected:...
|
by: Chris |
last post by:
Hi
I have a scenario where I've created another AppDomain to dynamically load a DLL(s) into. In this newly loaded DLL I want to call a static method on a class. The problem arise is that I have...
|
by: daniel.w.gelder |
last post by:
I wrote a template class that takes a function prototype and lets you
store and call a C-level function, like this:
inline string SampleFunction(int, bool) {..}
functor<string (int, bool)>...
|
by: greenflame |
last post by:
I am trying to make a matrix object. I have given it some properites. I
am trying to add a method. When I call the method by Test.showDims(...)
I want to only enter one input, that is the method by...
|
by: Nick Flandry |
last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my
development environment (Win2K server running IIS 5) and a test environment (also Win2K server
running IIS...
|
by: JohnR |
last post by:
From reading the documentation, this should be a relatively easy thing. I
have an arraylist of custom class instances which I want to search with
an"indexof" where I'm passing an instance if the...
|
by: Mihai Osian |
last post by:
Hi everyone,
Given the code below, can anyone tell me:
a) Is this normal behaviour ?
b) If it is, what is the reason behind it ? I would expect the
A::method(int) to be inherited by B.
...
|
by: Steve Richter |
last post by:
in a generic class, can I code the class so that I can call a static
method of the generic class T?
In the ConvertFrom method of the generic TypeConvert class I want to
write, I have a call to...
|
by: allendowney |
last post by:
Hi All.
In a complex inheritance hierarchy, it is sometimes difficult to find
where a
method is defined. I thought it might be possible to get this info
from the
method object itself, but it...
|
by: VK |
last post by:
<OT>I am finishing TransModal 0.1 so planning to move it from alpha to
beta stage.<OT>
Besides that I am planning to write an introductory to inheritance
schema currently used in Javascript...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |