|
| | #16 (permalink) | |
| euro scum Join Date: Aug 2002 Location: Sweden
Posts: 808
| Quote:
Example: public Vector if(!road.containVehicles()){Without the Exception here you would have to return null, which in a lot of cases is bad since that could also mean the vector isn't initialized. You can't know the difference unless you throw a exception instead (or rather your program can't). And that is bad for debugging your program. Last edited by slitz : 09-05-2007 at 06:49 AM. | |
| | |
| | #18 (permalink) |
| Registered User Join Date: Nov 2003
Posts: 477
| I'm not sure I agree with you Slitz since I find try/catch/throwing as messy as goto/label statements ![]() But the great thing about programming is there are many ways to solve the same problem, thats what I love about it! |
| | |
| | #20 (permalink) |
| Registered User Join Date: Aug 2006 Location: Raleigh, NC
Posts: 1,865
| I hope people are still here because I am in need of some serious fucking help. So, my teacher is a grad student who's probably socially inept and has no speech skills and gave us homework. I've finished the homework, but I want to take it further... Here is what the homework was: public class MyName { public static void main(String [] args) { System.out.println("My name is Stephen"); } } and this is what I WANT to do: public class MyName { public static void main(String [] args) { String name; System.out.println("What is your name?"); g.drawString("Hello, " + name + } } I am specifically trying to get the program to ask "What is your name?" , you respond, and then it will say, "Hello, name". What I'm confused is, what am I doing wrong? If you can, finish the program and tell me why. My teacher /barely/ gave us the syllabus (and refused to give us his e-mail address) and taught us NOTHING and we were pushed right into figuring out how to see your name on the computer. |
| | |
| | #21 (permalink) |
| Registered User Join Date: Aug 2006 Location: Raleigh, NC
Posts: 1,865
| import java.util.Scanner; public class MyName { public static void main(String [] args) { String name; System.out.println("What is your name?"); Scanner sc = new Scanner(System.in); name = sc.nextLine(); System.out.println("Hello," + name); } } So it seems that works, but I don't understand the import and the scanners. Can someone please tell me why I need them? |
| | |
| | #22 (permalink) | |
| Registered User Join Date: Nov 2003
Posts: 477
| Quote:
The scanner will read the input line on the console. Although I've never personally used the scanner class I assume the Scanner.nextLine() will read the input string. Scanner is a class and because of that you need to import it into your program. Not all classes come loaded into the program, it would be a burden on your computer to load all these needless classes. So you need to tell your program "I am going to use the scanner class, so please load it." | |
| | |
| | #23 (permalink) | |
| Doooooooooooom...? Join Date: Jan 2006
Posts: 927
| Quote:
For example: try inputting a string with a lot of numbers and saving them each to different values. Scanner has built-in methods to read the next integer (ScannerObject.nextInt()), or just next string (ScannerObject.next()). I know a bunch of my programs in my first Java class we did string parsing as a significant portion of it. If you want to read a string of any length, try reading it into an array (this is another good thing to practice). ie: Code:
| |
| | |
| | #24 (permalink) |
| euro scum Join Date: Aug 2002 Location: Sweden
Posts: 808
| One thing to note here is that you don't actually need to use the "import" feature, it just saves you a lot of typing. It becomes rather clear in this example: import java.util.Date public Date getNormalDate(){ return new Date(); } public Date getSQLDate(){ return new Date(); } Since you've imported java.util.Date your program will use that Date for both of your methods, but since I actually named one of my methods "getSQLDate()" I most likely don't want the normal Date on that method, but rather the sql date. To avoid the error here you'd have to do the following: import java.util.Date public Date getNormalDate(){ return new Date(); } public java.sql.Date getSQLDate(){ return new java.sql.Date(); } And thus you probably will be able to see that you don't need the import at all, it's just a short for typing "new Date()" instead of "new java.util.Date();" So it would be perfectly ok to do the same on the getNormalDate() and thus skipping the import: public java.util.Date getNormalDate(){ return new java.util.Date(); } You'll most likely run into the problem of using two different classes with the same name, and that is exactly how you solve that issue. |
| | |
| | #25 (permalink) |
| Registered User Join Date: Aug 2006 Location: Raleigh, NC
Posts: 1,865
| Okay, I've run into a problem because I think the teacher went warp speed near the end of the class. Note: DON'T help me wit the first part, I'm going to try that out when I get to a computer with a compiler. ***Test String Operations*** Enter a string of characters: The Lord of the Rings The length of string "The Lord of the Rings" is 21 Enter an integer between 0 and 20: 10 The character at index 10 of string "The Lord of the Rings" is 'f' Enter anoher string of characters: Lord The first occurrence of string "Lord" in string "The Lord of the Rings" is at position 4. That is CONFUSING me! Here's what I have thus far (remember, if what I have isn't rigt, don't tell me please. Gnna test it out later). import java.util.Scanner; public class Arithmetic { public static void main(String [] args) int Fi, Si double Fd, Sd That is what I have...CURRENTLY. I'm going to ask the user to input a number and I'll basically have to use all the mathematical operations (including modulus) using integer and double. The last part of the program is supposed to be the string and index shit which I have NO idea what to do. The lecture notes don't really tell me much. Also, how would I go about putting in a date and such? My homework is: Write a java program that asks the user for a date and reads th date entered by the user and finally prints a friendly message. Would this be...? import java.util.Scanner; public class Date { public static void main(String [] args); Scanner keyboard = new Scanner(System.in); string = Date System.out.println("Please enter today's date."); Date = keyboard.nextString(); System.out.println("Today's date is:" + mm/dd/yyyy); System.out.println("Have a nice day."); } } Okay, now that I look at it...I'm doing something wrong, but I can't put my finger on it. Please help. ![]() edit: Just to clarify...what I'm wanting help with is the date and the string index and all that shit. :P
__________________ VOCA ME BENEDICTUM ! SANA MEAM ANIMAM ! Last edited by Kuriin : 09-28-2007 at 10:03 AM. |
| | |
| | #26 (permalink) | |
| euro scum Join Date: Aug 2002 Location: Sweden
Posts: 808
| Quote:
You should also stay away from writing code in your main method, except for initialization, because it's there for just that, initialization of your program, nothing else (guess you'll understand that when covering static methods/variables). Anyhoo: Code:
Perhaps it's a bit of a overkill for your program however, I really don't know, but when using dates one should use the class Date and not a String representation of it and thus this will be a more correct program. Last edited by slitz : 09-28-2007 at 11:07 AM. | |
| | |
| | #27 (permalink) |
| Registered User Join Date: Aug 2006 Location: Raleigh, NC
Posts: 1,865
| Thing is, what you put is pretty much advanced for us. We haven't gone over boolean yet (that's next week) and most of the code is greek to me. lol. Remember it's elementary. We just went over methods and variables.
__________________ VOCA ME BENEDICTUM ! SANA MEAM ANIMAM ! |
| | |
| | #28 (permalink) |
| euro scum Join Date: Aug 2002 Location: Sweden
Posts: 808
| Are you supposed to just enter a date as a String and then just print it out then? If so, you solved it before didn't you? String date = scanner.next(); System.out.println("You entered date: " +date); Or did I missunderstand your question? |
| | |
| | #29 (permalink) |
| Registered User Join Date: Aug 2006 Location: Raleigh, NC
Posts: 1,865
| You didn't misunderstand me. I just don't want to go completely advanced where it makes me confused. I'm just trying to get it to ask the user for the date and the output is the date + a nice message at the end. I dunno if what I put was correct (other than the overusage of Date).
__________________ VOCA ME BENEDICTUM ! SANA MEAM ANIMAM ! |
| | |
| | #30 (permalink) |
| Registered User Join Date: Aug 2006 Location: Raleigh, NC
Posts: 1,865
| Here's my lab thus far: import java.util.Scanner; public class Arithmetic { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int Fi, Si, Ti, Ff, Ss, Sss, Ei; double Fd, Sd, Td, Ffd, Ffdd, Ssd, Ssdd; System.out.println("Enter the first integer."); Fi = keyboard.nextInt(); System.out.println("Enter the second integer."); Si = keyboard.nextInt(); Ti = Fi + Si; Ff = Fi - Si; Ss = Fi * Si; Sss = Fi / Si; Ei = Fi % Si; System.out.println("When added, these two integers equal" + Ti); System.out.println("When subtracted, these two integers equal" + Ff); System.out.println("When multiplied, these two integers equal" + Ss); System.out.println("When divided, these two integers equal" + Sss); System.out.println("When modulated, these two integers equal" + Ei); System.out.println("Now, enter a real number."); Fd = keyboard.nextDouble(); System.out.println("Now, enter another real number."); Sd = keyboard.nextDouble(); Td = Fd + Sd; Ffd = Fd - Sd; Ffdd = Fd * Sd; Ssd = Fd / Sd; Ssdd = Fd % Sd; System.out.println("When added, these two real numbers equal" + Td); System.out.println("When subtracted, these two real numbers equal" + Ffd) System.out.println("Multiplied, these two real numbers equal" + Ffdd); System.out.println("Divided, these two real numbers equal" + Ssd); System.out.println("Modulated, these two real numbers equal" + Ssdd); this is the part that I am stuck on: # Input a text string, output its length; input an index within the string, and output the character at that position in the string; input another text string, and output the location of the first occurrence of the second string in the first one (if such location exists, otherwise output -1); # Finally, input a date (a text string) in the format mm/dd/yyyy (i.e., two digits for the month, followed by a '/', followed by two digits for the day, followed by a '/', followed by four digits for the year), and output it in the common European format dd-mm-yyyy (i.e., two digits for the day, followed by a '-', followed by two digits for the month, followed by a '-', followed by four digits for the year. edit: I realize this might look like a lot more work, but hey, it's a start. I'll learn how to make it smaller eventually. ![]() edit 2: Okay, I just ran this program and it works. But, something is wrong. The spacing is a bit iffy at the end of my sentences. There is no spacing in between equal and the final result of that operation. I'm not sure what's going on or how to fix it. Do I need to start a new line?
__________________ VOCA ME BENEDICTUM ! SANA MEAM ANIMAM ! Last edited by Kuriin : 09-28-2007 at 08:02 PM. |
| | |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
| |