Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 10-09-2008, 11:19 AM   #1
Golden Master
 
dude_se's Avatar
 
Join Date: Nov 2004
Posts: 8,632
Send a message via AIM to dude_se Send a message via MSN to dude_se
Default i need some java help

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
__________________

__________________
Laptop spec: ASUS X53E, i5 2430m 2.4ghz, 3gb ram, 320gb hdd, intel hd graphics, usb 3.0
dude_se is offline   Reply With Quote
Old 10-09-2008, 12:11 PM   #2
Daemon Poster
 
xtremezx2's Avatar
 
Join Date: Sep 2006
Posts: 915
Default Re: i need some java help

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"
__________________

xtremezx2 is offline   Reply With Quote
Old 10-09-2008, 12:33 PM   #3
Golden Master
 
dude_se's Avatar
 
Join Date: Nov 2004
Posts: 8,632
Send a message via AIM to dude_se Send a message via MSN to dude_se
Default Re: i need some java help

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");
	}
    }
}
__________________
Laptop spec: ASUS X53E, i5 2430m 2.4ghz, 3gb ram, 320gb hdd, intel hd graphics, usb 3.0
dude_se is offline   Reply With Quote
Old 10-09-2008, 02:30 PM   #4
Daemon Poster
 
xtremezx2's Avatar
 
Join Date: Sep 2006
Posts: 915
Default Re: i need some java help

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.
xtremezx2 is offline   Reply With Quote
Old 10-09-2008, 02:51 PM   #5
Golden Master
 
dude_se's Avatar
 
Join Date: Nov 2004
Posts: 8,632
Send a message via AIM to dude_se Send a message via MSN to dude_se
Default Re: i need some java help

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?
__________________
Laptop spec: ASUS X53E, i5 2430m 2.4ghz, 3gb ram, 320gb hdd, intel hd graphics, usb 3.0
dude_se is offline   Reply With Quote
Old 10-09-2008, 03:39 PM   #6
muz
Golden Master
 
muz's Avatar
 
Join Date: Oct 2006
Posts: 6,928
Default Re: i need some java help

describe how it dosent work , what happens ?
__________________
Desktop-AMD Athlon 64 X2 6000+-2GB Elixer DDR2 800 250gb+500gb+500gb+120gb
Laptop-Apple Macbook Pro 13" Intel core i5(2.3ghz) 4gb Ram 320gb hard drive
muz is offline   Reply With Quote
Old 10-09-2008, 03:43 PM   #7
Daemon Poster
 
xtremezx2's Avatar
 
Join Date: Sep 2006
Posts: 915
Default Re: i need some java help

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.
xtremezx2 is offline   Reply With Quote
Old 10-09-2008, 04:04 PM   #8
Golden Master
 
dude_se's Avatar
 
Join Date: Nov 2004
Posts: 8,632
Send a message via AIM to dude_se Send a message via MSN to dude_se
Default Re: i need some java help

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:

__________________
Laptop spec: ASUS X53E, i5 2430m 2.4ghz, 3gb ram, 320gb hdd, intel hd graphics, usb 3.0
dude_se is offline   Reply With Quote
Old 10-09-2008, 04:28 PM   #9
Daemon Poster
 
xtremezx2's Avatar
 
Join Date: Sep 2006
Posts: 915
Default Re: i need some java help

Well, I looked quick for you error. It's not your code itself that is the problem, it's not able to find your Class Definition.

when you execute the program, type.
Code:
java -classpath . test
Source:
http://www.tech-recipes.com/rx/826/j...deffounderror/
xtremezx2 is offline   Reply With Quote
Old 10-09-2008, 04:42 PM   #10
Golden Master
 
dude_se's Avatar
 
Join Date: Nov 2004
Posts: 8,632
Send a message via AIM to dude_se Send a message via MSN to dude_se
Default Re: i need some java help

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
__________________

__________________
Laptop spec: ASUS X53E, i5 2430m 2.4ghz, 3gb ram, 320gb hdd, intel hd graphics, usb 3.0
dude_se is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 02:59 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0