Fires of Heaven Guild Message Board  

Go Back   Fires of Heaven Guild Message Board > General forums > Development
User Name
Password
ForumSpy Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 09-05-2007, 12:28 AM   #16 (permalink)
slitz
euro scum
 
slitz's Avatar
 
Join Date: Aug 2002
Location: Sweden
Posts: 808
-9 Internets
Quote:
Originally Posted by Tenks View Post
Because you should really test your data and avoid try/catch via good programming ideas. I don't think I use try/catch outside of File IO and database programming.
Think what you mean is, even though you sometimes needs to call methods that throw different exceptions, it's your job as a programmer to make sure a exception is almost never caught (by making sure your data is valid). The try / catches shouldn't be avoided themselves, nor should you program without methods that throw exceptions.

Example:

public Vector getRoadQueue(Road road) throws NoVehicleException{

     if(!road.containVehicles()){
throw new NoVehicleException();
}else{
return road.getVehicleQueue();
}
}
}


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.
slitz is offline   Reply With Quote
Old 09-05-2007, 12:28 PM   #17 (permalink)
Kuriin
Registered User
 
Kuriin's Avatar
 
Join Date: Aug 2006
Location: Raleigh, NC
Posts: 1,865
Thanks guys.
Kuriin is offline   Reply With Quote
Old 09-05-2007, 02:02 PM   #18 (permalink)
Tenks
Registered User
 
Join Date: Nov 2003
Posts: 477
-16 Internets
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!
Tenks is offline   Reply With Quote
Old 09-05-2007, 10:57 PM   #19 (permalink)
slitz
euro scum
 
slitz's Avatar
 
Join Date: Aug 2002
Location: Sweden
Posts: 808
-9 Internets
Post

Quote:
Originally Posted by Tenks View Post
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!
True 8)
slitz is offline   Reply With Quote
Old 09-21-2007, 06:34 PM   #20 (permalink)
Kuriin
Registered User
 
Kuriin's Avatar
 
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.
Kuriin is offline   Reply With Quote
Old 09-21-2007, 06:54 PM   #21 (permalink)
Kuriin
Registered User
 
Kuriin's Avatar
 
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?
Kuriin is offline   Reply With Quote
Old 09-22-2007, 11:39 AM   #22 (permalink)
Tenks
Registered User
 
Join Date: Nov 2003
Posts: 477
-16 Internets
Quote:
Originally Posted by Kuriin View Post
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?
Its been a looooong time since I've done any Java console programming but I can answer your questions.

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."
Tenks is offline   Reply With Quote
Old 09-22-2007, 12:59 PM   #23 (permalink)
theawddone
Doooooooooooom...?
 
theawddone's Avatar
 
Join Date: Jan 2006
Posts: 927
+0 Internets
Quote:
Originally Posted by Kuriin View Post
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?
The import is for importing part of the Java API (I think that's the current word?) to utilize a certain feature that's builtin to Java but not always accessed. It's just a memory saving feature I guess, so you don't have to load all of the billions of things that are available to use with Java in every program. A scanner basically reads any form of input, whether it be in the command line, a file, or anything else. Maybe try messing around with parsing strings, that can be a great way to learn the scanner object.

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:
//class declarations etc Scanner sc = new Scanner(System.in) String[] array = new String[100] int i = 0; while sc.hasnext() //reads your input until it meets the end { array[i] = sc.next(); i++; }
You can also then utilize the value i to check how many different 'words' or strings you read in, etc. This isn't the most elegant way to do it, especially with using an array that's maxed out at a certain size, but at it's basic level it should teach you something you can mess around with further...
theawddone is offline   Reply With Quote
Old 09-27-2007, 02:35 PM   #24 (permalink)
slitz
euro scum
 
slitz's Avatar
 
Join Date: Aug 2002
Location: Sweden
Posts: 808
-9 Internets
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.
slitz is offline   Reply With Quote
Old 09-28-2007, 10:01 AM   #25 (permalink)
Kuriin
Registered User
 
Kuriin's Avatar
 
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.
Kuriin is offline   Reply With Quote
Old 09-28-2007, 10:53 AM   #26 (permalink)
slitz
euro scum
 
slitz's Avatar
 
Join Date: Aug 2002
Location: Sweden
Posts: 808
-9 Internets
Quote:
Originally Posted by Kuriin View Post
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
First of all, since there is two classes in the Java API that is named Date already, try to refrain from naming your class Date (this goes for naming all your classes actually. Just stay away from commonly used words)
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:
public class MyCustomDate { private Scanner scanner; private SimpleDateFormat format; private Date inputDate; private boolean failure =false; public MyCustomDate() { format = new SimpleDateFormat("MM/dd/yyyy"); } public void readMyConsole(){ scanner = new Scanner(System.in); String input = scanner.next(); inputDate = null; try { inputDate = format.parse(input); } catch (ParseException ex) { failure = true; } if (!failure){ System.out.println("You entered: " +inputDate.toString()); System.out.println("Current date is: " +new Date()); }else{ System.err.println("wrong format, try again!"); failure = false; readMyConsole(); } } public static void main(String[] args) { MyCustomDate customDate = new MyCustomDate(); customDate.readMyConsole(); } }
Check the API for SimpleFormatDate and Date so you understand what happens.
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.
slitz is offline   Reply With Quote
Old 09-28-2007, 11:08 AM   #27 (permalink)
Kuriin
Registered User
 
Kuriin's Avatar
 
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 !
Kuriin is offline   Reply With Quote
Old 09-28-2007, 11:29 AM   #28 (permalink)
slitz
euro scum
 
slitz's Avatar
 
Join Date: Aug 2002
Location: Sweden
Posts: 808
-9 Internets
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?
slitz is offline   Reply With Quote
Old 09-28-2007, 03:46 PM   #29 (permalink)
Kuriin
Registered User
 
Kuriin's Avatar
 
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 !
Kuriin is offline   Reply With Quote
Old 09-28-2007, 07:56 PM   #30 (permalink)
Kuriin
Registered User
 
Kuriin's Avatar
 
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.
Kuriin is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 On
Trackbacks are On
Pingbacks are On
Refbacks are On
uberguilds network



All times are GMT -7. The time now is 02:39 AM.


Powered by vBulletin® Version 3.6.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.0.0 RC6