i need some java help

dude_se

Proper Legend
Messages
8,634
Location
UK
well im at uni and am learning java. still a beginner :p
basically one of the tasks is to write some code that determines:
"whether or not an item of data entered at the keyboard is valid or not for the following input specifications"
one of them is: "a string that has the value of Cathy or Kelvin"

some of the other ones involve entering numbers and the output tells you if they are positive, negative, etc. i just cant seem to work with characters instead of numbers... help?

sorry if this seems noobish to all the programmers out there, but we all have to start somewhere :)
 
Easiest way to check if a string has those in it would be something like this.

Code:
function (strInput) {
    if (strInput.toUpperCase().contains("CATHY") || strInput.toUpperCase().contains("KELVIN")) {
        return true;   //return true if string contains the format.
    } else {
        return false;
    }
}

That should work, check the syntax, I have been doing a lot of javascript recently and haven't actually coded in java for about 4 years.

Oops, I read a bit closer. Above would check if the string contains the sequence, looks like your teacher wants it to be equal. If this is the case it's pretty much the same, just change
Code:
strInput.toUpperCase().contains("CATHY")
to
Code:
strInput.toUpperCase() == "CATHY"
 
yea it must only say that its valid when the exact names are entered. i tried that code but got a few errrors. guess i need to check over the rest of the code too. the stuff we have learnt about so far is different, and is more like:

Code:
import java.util.*;    // the package containing Scanner
public class Integer02
{
	 public static void main(String[] args) 
     {
 // build an instance of Scanner, that is an object
 Scanner kybd = new Scanner(System.in);
        // Input the data
	    int number ;
        System.out.println("Enter a number:  ");
 	    number = kybd.nextInt();
//check the range
	if (( number >= 3 )  && (number <= 45) || ( number < -10 ))

	{
		System.out.println("The number is valid");
	}
	else 
        {
		System.out.println("number is invalid");
	}
    }
}
 
Yeah, there is probably a lot more javascriptish stuff in there than java as that's kind of what I've been doing lately, but you can get the basic flow of what it does. Basically the function I gave you, you'd pass the string you got from the keyboard into that function, it would test it for you and give you a true or false based on what it found. You've pretty much got your stuff done to do it that you put in your last post, you just need to change from int to strings.
 
yea thats what i thought. i just couldnt get it working. ill give it another shot and let you know how i get on.
+1
edit: enjoy the 3 bars i just bumped you to :)

edit:
Code:
 import java.util.*;    // the package containing Scanner
public class test
{
	 public static void main(String[] args) 
     {
 // build an instance of Scanner, that is an object
 Scanner kybd = new Scanner(System.in);
        // Input the data
	    String phrase ;
        System.out.println("Enter a name:  ");
 	    phrase = kybd.nextString();
//check the phrase
	if (( phrase == Cathy ) || ( phrase == Kelvin ))

	{
		System.out.println("The phrase is valid");
	}
	else 
        {
		System.out.println("The phrase is invalid");
	}
    }
}

that doesnt work, why?
 
This line has an error:
Code:
if (( phrase == Cathy ) || ( phrase == Kelvin ))
You need to have quotes around them so that the code knows it's a string value and not a variable. Like this:
Code:
if (( phrase == "Cathy" ) || ( phrase == "Kelvin" ))
You may also want to do it like this so it doesn't matter what case it is:
Code:
if (( phrase.toUpperCase() == "CATHY" ) || ( phrase.toUpperCase() == "KELVIN" ))

Oh yeah, thanks for the rep.
 
i did what you said and also changed the nextstring to just next
and javac gives me no errors, but when i try and run it i get:

21oautk.jpg
 
i never used to get this problem. how come i do now?
and yea it works now, but even if i enter the name correctly it says its invalid :confused:
 
Back
Top Bottom