# Maximum PC

 It is currently Wed Sep 17, 2014 5:33 am

 All times are UTC - 8 hours

 Page 2 of 3 [ 58 posts ] Go to page Previous  1, 2, 3  Next
 Print view Previous topic | Next topic
Author Message
 Post subject: Posted: Fri Feb 06, 2009 2:16 pm
 Little Foot

Joined: Mon Jul 28, 2008 1:39 pm
Posts: 123
Well, if you're really interested, here's the code from Integer.parseInt().
(FYI, you probably won't understand all of it, I certainly don't )
Code:
public static int parseInt(String s, int radix)
throws NumberFormatException
{
if (s == null) {
throw new NumberFormatException("null");
}

}

}

int result = 0;
boolean negative = false;
int i = 0, max = s.length();
int limit;
int multmin;
int digit;

if (max > 0) {
if (s.charAt(0) == '-') {
negative = true;
limit = Integer.MIN_VALUE;
i++;
} else {
limit = -Integer.MAX_VALUE;
}
if (i < max) {
if (digit < 0) {
throw NumberFormatException.forInputString(s);
} else {
result = -digit;
}
}
while (i < max) {
// Accumulating negatively avoids surprises near MAX_VALUE
if (digit < 0) {
throw NumberFormatException.forInputString(s);
}
if (result < multmin) {
throw NumberFormatException.forInputString(s);
}
if (result < limit + digit) {
throw NumberFormatException.forInputString(s);
}
result -= digit;
}
} else {
throw NumberFormatException.forInputString(s);
}
if (negative) {
if (i > 1) {
return result;
} else {   /* Only got "-" */
throw NumberFormatException.forInputString(s);
}
} else {
return -result;
}
}

Top

 Post subject: Posted: Tue Mar 03, 2009 1:24 pm
 Northwood

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
So next I received an assignment that I am to parse an entire text file for any combination of the two letters G and B right next to each other.

This is going to be tough (For me anyways)

There are several ways I can do this I think. I could run the whole loops as follows

Code:
Loop Until bottom line has been reached{

{     If GG = token(?)
G += 2

Elseif GB = token(?)
G++
B++
}

sample++

}

and so on and so forth. There is probably a better method but I think I'll go with that one

Top

 Post subject: Posted: Tue Mar 03, 2009 2:42 pm
 Little Foot

Joined: Mon Jul 28, 2008 1:39 pm
Posts: 123
Do you know how to use Regex?

Top

 Post subject: Posted: Tue Mar 03, 2009 2:52 pm
 Northwood

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
Quertior wrote:
Do you know how to use Regex?

No I do not.

Top

 Post subject: Posted: Tue Mar 03, 2009 3:49 pm
 Little Foot

Joined: Mon Jul 28, 2008 1:39 pm
Posts: 123
OK, then your method is probably the best.

Top

 Post subject: Posted: Fri Mar 06, 2009 9:28 pm
 Northwood

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
Fixed those issues. Took me until 12:30 A.M. here but yeah. I was doing it completely wrong and slow.

The program is barely recognizable from what it used to be

Top

 Post subject: Posted: Sat Mar 07, 2009 12:09 pm
 Little Foot

Joined: Mon Jul 28, 2008 1:39 pm
Posts: 123
LOL good job man.

Top

 Post subject: Posted: Sat Mar 07, 2009 3:27 pm
 Northwood

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
Quertior wrote:
LOL good job man.

it was a new assignment. I just edited my posts and stuff... (Habit of deleting posts when I previously double posted)

File I/O, nested loops, etc.

Top

 Post subject: Posted: Wed Mar 11, 2009 2:51 pm
 Northwood

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
Well here's a new problem:

Code:
int index = 0;

double [] temps = new double[11];
File fileName = new File("westpalmbeach.txt");
Scanner inFile = new Scanner(fileName);

while (inFile.hasNextDouble())
{
temps[index] = inFile.nextDouble();
index++;
}

for(int n = 0; n <= index; n++)
{
System.out.println("index position " + n + " " + temps[n]);

}

*sigh* Errors...

So what I have is a file and it has 11 doubles, all right? (westpb.text)

When i set my array to 11 it gives me an error with no chance for display but when I change it to 12 I get a display all the way down to index 11 but I get an error though :/

Assume any other code that you don't see works

Edit: Well I change it to 13 and I get display to a twelfth (but nonexistant double) Which output is not desired.

This is simplified code for the assignment I am doing to make it easier for me to debug.

Top

 Post subject: Posted: Wed Mar 11, 2009 3:28 pm
 Little Foot

Joined: Mon Jul 28, 2008 1:39 pm
Posts: 123
Your problem is that array indices for an array of length n go from 0 to n - 1. You are going from 0 to n. Since temps[n] does not exist, you get an error.

Top

 Post subject: Posted: Wed Mar 11, 2009 3:32 pm
 Northwood

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
Quertior wrote:
Your problem is that array indices for an array of length n go from 0 to n - 1. You are going from 0 to n. Since temps[n] does not exist, you get an error.

Actually... I just figured it out.... It's one of the most simple and retarded mistakes I have ever made

Code:
for(int n = 0; n [b]<=[/b] index; n++)

The for stops AFTER n is = index.

My solution?

Code:
for(int n = 0; n [b]<[/b] index; n++)

Silly me D:

Top

 Post subject: Posted: Wed Mar 11, 2009 4:01 pm
 Little Foot

Joined: Mon Jul 28, 2008 1:39 pm
Posts: 123
Don't worry...
it's not retarded at all. ArrayIndexOutOfBoundsException is one of the most common errors that everyone makes.

Top

 Post subject: Posted: Thu Mar 12, 2009 4:01 am
 SON OF A GUN

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
Quertior wrote:
Don't worry...
it's not retarded at all. ArrayIndexOutOfBoundsException is one of the most common errors that everyone makes.
No they don't! pffftt... ::looks away::

Uh... crap.

Top

 Post subject: Posted: Thu Mar 12, 2009 10:44 am
 Northwood

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
CrashTECH wrote:
Quertior wrote:
Don't worry...
it's not retarded at all. ArrayIndexOutOfBoundsException is one of the most common errors that everyone makes.
No they don't! pffftt... ::looks away::

Uh... crap.

What makes it worse is that looking in what seems to be a perfectly fine for statement is one of the last places programmers look while debugging!

Top

 Post subject: Posted: Thu Mar 12, 2009 3:09 pm
 Little Foot

Joined: Mon Jul 28, 2008 1:39 pm
Posts: 123
Yeah, no one likes to admit it, but they make small errors all the time. That's why Java started throwing exceptions for index out of bounds, unlike C and C++ which would just keep operating and your only clue would be when the value at the "final" index in the array was something like 145998!

Top

 Post subject: Posted: Sat Mar 14, 2009 4:51 am
 Northwood

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
Code:
Code Removed for grading purposes :D

Currently I will be debugging this.

I fixed that problem, now to use Printf properly to format the output.
___

Edit:

Done with that assignment code is no longer required. Printf & arrays.

Top

 Post subject: Posted: Sun Mar 15, 2009 9:40 pm
 Little Foot

Joined: Mon Jul 28, 2008 1:39 pm
Posts: 123
eek!! printf() is a relic from C and C++. i still cant believe that there is no better way yet.

The March of Progress

1980: C
Code:
printf("%10.2f", x);

1988: C++
Code:
cout << setw(10) << setprecision(2) << showpoint << x;

1996: Java
Code:
java.text.NumberFormat formatter = java.text.NumberFormat.getNumberInstance();
formatter.setMinimumFractionDigits(2);
formatter.setMaximumFractionDigits(2); String s = formatter.format(x);
for (int i = s.length(); i < 10; i++) System.out.print(' ');
System.out.print(s);

2004: Java
Code:
System.out.printf("%10.2f", x);

2008: Scala and Groovy
Code:
printf("%10.2f", x)

Top

 Post subject: Posted: Mon Mar 16, 2009 12:20 pm
 Northwood

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
I like the Scala and Groovy version the best. We also need IDEs which detect how often a person uses a certain command.

Try that on for size. Notice how there are no errors and I still get ArrayIndexOoB (figured out the problem, silly me)

Code:
removed

Last edited by Dwood15 on Mon May 11, 2009 3:22 pm, edited 1 time in total.

Top

 Post subject: Posted: Mon Mar 16, 2009 1:40 pm
 Little Foot

Joined: Mon Jul 28, 2008 1:39 pm
Posts: 123
note the lack of semicolon in scala and groovy. improvement!

and for sure, runtime exceptions are 20x harder to debug than compile time errors.

Top

 Post subject: Posted: Mon Mar 16, 2009 5:38 pm
 Northwood

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
Quertior wrote:
note the lack of semicolon in scala and groovy. improvement!

and for sure, runtime exceptions are 20x harder to debug than compile time errors.

Code:
removed

I feel so dirty.... Like something in my head just.. broke D:

Last edited by Dwood15 on Mon May 11, 2009 3:22 pm, edited 1 time in total.

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 2 of 3 [ 58 posts ] Go to page Previous  1, 2, 3  Next

 All times are UTC - 8 hours

#### Who is online

Users browsing this forum: No registered users and 2 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ Maximum FAQs    Forum Rules, Posting Guidelines & Announcements    The Good, The Bad & The Banned    FAQs Help/Do It Yourself    PC Building Lab    The Help Desk    PC Modding    Education & Certification Hardware    Nuts & Bolts    Portable Talk    Appraisals, Deals & Bargains (oh my!) OS/Software/Programming    Windows Parlor    Alt.OS.Abode    Games Arena    Programmers' Paradise Networking/Internet    Internet Truckstop    Network Nook In/Out    Magazine and Book Feedback    Forum & Website Feedback    Dog Pound Team Maximum PC Folding at Home    Team Maximum PC - Folding at Home - FIND CURES TO DISEASES    Team MPC - Folding Gauntlets