Go to Post "We're too close for missiles, switching to Poof balls!" "Just one more day on the driving code, then I'll switch to autonomous." - Rick TYler [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 31-10-2016, 08:19
Tem1514 Mentor's Avatar
Tem1514 Mentor Tem1514 Mentor is offline
Registered User
FRC #1514
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2010
Location: Toronto
Posts: 238
Tem1514 Mentor is a splendid one to beholdTem1514 Mentor is a splendid one to beholdTem1514 Mentor is a splendid one to beholdTem1514 Mentor is a splendid one to beholdTem1514 Mentor is a splendid one to beholdTem1514 Mentor is a splendid one to beholdTem1514 Mentor is a splendid one to beholdTem1514 Mentor is a splendid one to behold
JAVA 101 help

As a complete novice with JAVA I am trying some sample tests to get some sort of handle on how to program in this language.

Please see the below code
#################################

import java.util.Scanner;

public class makeitwork {

static Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {


Boolean alarm = false;
System.out.println("What time 0-24");
Integer clock = Integer.parseInt(scanner.nextLine());

System.out.println("Door open 'T' or 'F'");
String door = (scanner.nextLine());

System.out.println("Test combo 0000-9999");
String combo = (scanner.nextLine());

System.out.println("#1-t-"+clock+"-d-"+door+"-c-"+combo+"-a-"+alarm);

if (clock < 5){
System.out.println("clock<5");
}
if (clock == 5){
System.out.println("clock = 5");
}
if (clock >5 && clock <24){
System.out.println("clock > 5 and < 24");
}
if (clock >5 || clock <24){ // watch out for this one it only fails with 24
System.out.println("clock > 5 or < 24");
}
if (clock >=23 && clock <=24 || clock>= 0 && clock <= 3){
System.out.println("clock 11pm to 3 am");
}
// all those integer functions seem to work on clock.

// lets see what work for strings
if (door == "t"){
System.out.println("It's a t");
}
else{
System.out.println("Don't know-"+door+"-");
}


} //end of main

}// end of public

##############################################
The integer functions seem to work just as expected but when I try the same thing with what I think is a string it does not work at all. The print will always be "Don't know-"+door+"-" result.

So my question(s) are;
What am I doing wrong?

Where in the "eclipse" enviroment can I find example help on functions?
When using the 'Help' function in eclipse what does one do to get a list functions?

Currently I am looking for way of finding things out about a string such as;
1)it's length
2)how to strip a string apart one charater at a time and search for delimiters etc.
for example;
assume test[] are strings

test = "334,5,t,678,c,9"
from the 'test' I would like to extract the string parts separted by a ','
to get
test1 = "334"
test2 = "5"
test3 = "t"
test4 = "678"
test5 = "c"
test6 = "9"

Many thanks
Reply With Quote
  #2   Spotlight this post!  
Unread 31-10-2016, 08:47
Ethan_P Ethan_P is offline
Registered User
FRC #1895 (Lambda Corps)
Team Role: Leadership
 
Join Date: Jan 2014
Rookie Year: 2013
Location: Virginia
Posts: 3
Ethan_P is an unknown quantity at this point
Re: JAVA 101 help

You have (door == "t"), this is not recommended.

Strings are objects, unlike ints and other primitive types. And because Strings are objects, you can't use '==' to compare two Strings. You would need to use
string1.equals(string2);
Reply With Quote
  #3   Spotlight this post!  
Unread 31-10-2016, 09:11
GeeTwo's Avatar
GeeTwo GeeTwo is online now
Technical Director
AKA: Gus Michel II
FRC #3946 (Tiger Robotics)
Team Role: Mentor
 
Join Date: Jan 2014
Rookie Year: 2013
Location: Slidell, LA
Posts: 3,591
GeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond repute
Re: JAVA 101 help

The java string class has methods to do each of the things you're asking about.

.equals() and .length() for the first two.

While you can manually tear the string apart, there is a method .split() which will parse out comma separated (or any regular expression separated) substrings.
__________________

If you can't find time to do it right, how are you going to find time to do it over?
If you don't pass it on, it never happened.
Robots are great, but inspiration is the reason we're here.
Friends don't let friends use master links.
Reply With Quote
  #4   Spotlight this post!  
Unread 31-10-2016, 10:29
gblake's Avatar
gblake gblake is offline
6th Gear Developer; Mentor
AKA: Blake Ross
no team (6th Gear)
Team Role: Mentor
 
Join Date: May 2006
Rookie Year: 2006
Location: Virginia
Posts: 1,935
gblake has a reputation beyond reputegblake has a reputation beyond reputegblake has a reputation beyond reputegblake has a reputation beyond reputegblake has a reputation beyond reputegblake has a reputation beyond reputegblake has a reputation beyond reputegblake has a reputation beyond reputegblake has a reputation beyond reputegblake has a reputation beyond reputegblake has a reputation beyond repute
Re: JAVA 101 help

The Java tutorial maintained by Oracle is here:
Tutorial Big Index
You can perhaps find better ones (better for you), if you look around. However, this one is the safe, common denominator for me to recommend. It should include a section or subsection about String objects.

The Java version 8, Standard Edition, API reference, maintained by Oracle, is here:
Java 8 SE API Reference
Looking up the String class will show you String's "equals" method, along with all the others available for what you want to do. This is the same info GeeTwo's link will take you to, except that I gave the URL for the entire Java SE version 8 API doc. His link goes straight to the Java SE version 7 String class.

Blake
PS: I dunno how to configure Eclipse to help you. However, the "StackOverflow" Q&A site often has answers for questions like these, because someone else already already asked the same question. I gravitate toward their links when I use Google to search for software answers.
__________________
Blake Ross, For emailing me, in the verizon.net domain, I am blake
VRC Team Mentor, FTC volunteer, 5th Gear Developer, Husband, Father, Triangle Fraternity Alumnus (ky 76), U Ky BSEE, Tau Beta Pi, Eta Kappa Nu, Kentucky Colonel
Words/phrases I avoid: basis, mitigate, leveraging, transitioning, impact (instead of affect/effect), facilitate, programmatic, problematic, issue (instead of problem), latency (instead of delay), dependency (instead of prerequisite), connectivity, usage & utilize (instead of use), downed, functionality, functional, power on, descore, alumni (instead of alumnus/alumna), the enterprise, methodology, nomenclature, form factor (instead of size or shape), competency, modality, provided(with), provision(ing), irregardless/irrespective, signage, colorized, pulsating, ideate

Last edited by gblake : 31-10-2016 at 15:18.
Reply With Quote
  #5   Spotlight this post!  
Unread 31-10-2016, 21:35
mathking's Avatar
mathking mathking is offline
Coach/Faculty Advisor
AKA: Greg King
FRC #1014 (Dublin Robotics aka "Bad Robots")
Team Role: Teacher
 
Join Date: Jan 2005
Rookie Year: 1999
Location: Columbus, OH
Posts: 635
mathking has a reputation beyond reputemathking has a reputation beyond reputemathking has a reputation beyond reputemathking has a reputation beyond reputemathking has a reputation beyond reputemathking has a reputation beyond reputemathking has a reputation beyond reputemathking has a reputation beyond reputemathking has a reputation beyond reputemathking has a reputation beyond reputemathking has a reputation beyond repute
Re: JAVA 101 help

When you evaluate door == "t" it is not checking to see if door has that value. It is checking to see if the bits are the same. In the case of a String object, that means "are they pointing at the same place in memory?", not "are they equal in value?" Because the bits are the location where the object's data is stored. With an integer you are dealing with a primitive so the bits are a value and not a memory location. As GeeTwo said, if you use if(door.equals("t")) you will get the comparison you want.
__________________
Thank you Bad Robots for giving me the chance to coach this team.
Rookie All-Star Award: 2003 Buckeye
Engineering Inspiration Award: 2004 Pittsburgh, 2014 Crossroads
Chairman's Award: 2005 Pittsburgh, 2009 Buckeye, 2012 Queen City
Team Spirit Award: 2007 Buckeye, 2015 Queen City
Woodie Flowers Award: 2009 Buckeye
Dean's List Finalists: Phil Aufdencamp (2010), Lindsey Fox (2011), Kyle Torrico (2011), Alix Bernier (2013), Deepthi Thumuluri (2015)
Gracious Professionalism Award: 2013 Buckeye
Innovation in Controls Award: 2015 Pittsburgh
Event Finalists: 2012 CORI, 2016 Buckeye
Reply With Quote
  #6   Spotlight this post!  
Unread 18-11-2016, 13:11
Nirnaeth Nirnaeth is offline
Coach/Mentor
AKA: Michael Liang
FRC #6367 (The Brooke High ElectroLights)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2001
Location: Boston, MA
Posts: 44
Nirnaeth has much to be proud ofNirnaeth has much to be proud ofNirnaeth has much to be proud ofNirnaeth has much to be proud ofNirnaeth has much to be proud ofNirnaeth has much to be proud ofNirnaeth has much to be proud ofNirnaeth has much to be proud of
Re: JAVA 101 help

Quote:
Originally Posted by Ethan_P View Post
You have (door == "t"), this is not recommended.

Strings are objects, unlike ints and other primitive types. And because Strings are objects, you can't use '==' to compare two Strings. You would need to use
string1.equals(string2);
It might be useful to talk about the reason for this.

Let's say I have two Strings:

String a = "blah";
String b = "blah";

When I instantiate these Strings, Java allocates two different memory locations for them. And 'a' points to the first one that has the "blah" data in it, and 'b" points to another location with the "blah" data in it.

That's why a==b returns false, because 'a' and 'b' are pointing to two different objects, even if they have the same value.

The '==' operator in this case checks to see if they are the same object, not if the have 'equal' values.
__________________
Lead Mentor - Team 6367 - The Brooke High ElectroLights
Former Mentor - Team 5969 - The English Skunkworks
Former Mentor - Team 1754 - The Excel Nitro Knights
Reply With Quote
  #7   Spotlight this post!  
Unread 18-11-2016, 13:32
GeeTwo's Avatar
GeeTwo GeeTwo is online now
Technical Director
AKA: Gus Michel II
FRC #3946 (Tiger Robotics)
Team Role: Mentor
 
Join Date: Jan 2014
Rookie Year: 2013
Location: Slidell, LA
Posts: 3,591
GeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond repute
Re: JAVA 101 help

Added a word for clarity:
Quote:
Originally Posted by Nirnaeth View Post
The '==' operator in this case checks to see if they are the same object, not if the objects have 'equal' values.
__________________

If you can't find time to do it right, how are you going to find time to do it over?
If you don't pass it on, it never happened.
Robots are great, but inspiration is the reason we're here.
Friends don't let friends use master links.
Reply With Quote
  #8   Spotlight this post!  
Unread 21-11-2016, 21:01
DaveFrederick's Avatar
DaveFrederick DaveFrederick is offline
Registered User
FRC #1895
 
Join Date: Jan 2009
Location: Manassas,VA
Posts: 37
DaveFrederick is a jewel in the roughDaveFrederick is a jewel in the roughDaveFrederick is a jewel in the rough
Re: JAVA 101 help

One great approach to learning JAVA is to use GreenFoot. This is a set of 10 minute videos and a special build JAVA environment. Easy and fun to use.
Dave Frederick

http://www.greenfoot.org/doc/joy-of-code
Reply With Quote
  #9   Spotlight this post!  
Unread 26-11-2016, 12:08
Tem1514 Mentor's Avatar
Tem1514 Mentor Tem1514 Mentor is offline
Registered User
FRC #1514
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2010
Location: Toronto
Posts: 238
Tem1514 Mentor is a splendid one to beholdTem1514 Mentor is a splendid one to beholdTem1514 Mentor is a splendid one to beholdTem1514 Mentor is a splendid one to beholdTem1514 Mentor is a splendid one to beholdTem1514 Mentor is a splendid one to beholdTem1514 Mentor is a splendid one to beholdTem1514 Mentor is a splendid one to behold
Re: JAVA 101 help

Many thanks to all those that have responded with all the get help and links.
Reply With Quote
  #10   Spotlight this post!  
Unread 26-11-2016, 23:31
tcjinaz tcjinaz is offline
Tim
FRC #3853
Team Role: Mentor
 
Join Date: May 2011
Rookie Year: 2011
Location: Arizona
Posts: 206
tcjinaz has a spectacular aura abouttcjinaz has a spectacular aura about
Re: JAVA 101 help

Quote:
Originally Posted by Nirnaeth View Post
It might be useful to talk about the reason for this.

Let's say I have two Strings:

String a = "blah";
String b = "blah";

When I instantiate these Strings, Java allocates two different memory locations for them. And 'a' points to the first one that has the "blah" data in it, and 'b" points to another location with the "blah" data in it.

That's why a==b returns false, because 'a' and 'b' are pointing to two different objects, even if they have the same value.

The '==' operator in this case checks to see if they are the same object, not if the have 'equal' values.
That's barbaric. At least C++ allows overloading operators.
All my fears of Java are coming true.

Tim
__________________
Software Mentor
3853 Pridetronics[

Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 08:16.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi