Quantcast

Maximum PC

It is currently Fri Aug 29, 2014 2:58 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 58 posts ]  Go to page Previous  1, 2, 3  Next
Author Message
 Post subject:
PostPosted: Fri Feb 06, 2009 2:16 pm 
Little Foot
Little Foot
User avatar

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");
        }

   if (radix < Character.MIN_RADIX) {
       throw new NumberFormatException("radix " + radix +
                   " less than Character.MIN_RADIX");
   }

   if (radix > Character.MAX_RADIX) {
       throw new NumberFormatException("radix " + radix +
                   " greater than Character.MAX_RADIX");
   }

   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;
       }
       multmin = limit / radix;
       if (i < max) {
      digit = Character.digit(s.charAt(i++),radix);
      if (digit < 0) {
          throw NumberFormatException.forInputString(s);
      } else {
          result = -digit;
      }
       }
       while (i < max) {
      // Accumulating negatively avoids surprises near MAX_VALUE
      digit = Character.digit(s.charAt(i++),radix);
      if (digit < 0) {
          throw NumberFormatException.forInputString(s);
      }
      if (result < multmin) {
          throw NumberFormatException.forInputString(s);
      }
      result *= radix;
      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
  Profile  
 
 Post subject:
PostPosted: Tue Mar 03, 2009 1:24 pm 
Northwood
Northwood
User avatar

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
Location: No. 1 Thread Killer
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
  Profile  
 
 Post subject:
PostPosted: Tue Mar 03, 2009 2:42 pm 
Little Foot
Little Foot
User avatar

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


Top
  Profile  
 
 Post subject:
PostPosted: Tue Mar 03, 2009 2:52 pm 
Northwood
Northwood
User avatar

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
Location: No. 1 Thread Killer
Quertior wrote:
Do you know how to use Regex?


No I do not.


Top
  Profile  
 
 Post subject:
PostPosted: Tue Mar 03, 2009 3:49 pm 
Little Foot
Little Foot
User avatar

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


Top
  Profile  
 
 Post subject:
PostPosted: Fri Mar 06, 2009 9:28 pm 
Northwood
Northwood
User avatar

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
Location: No. 1 Thread Killer
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 :P


Top
  Profile  
 
 Post subject:
PostPosted: Sat Mar 07, 2009 12:09 pm 
Little Foot
Little Foot
User avatar

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


Top
  Profile  
 
 Post subject:
PostPosted: Sat Mar 07, 2009 3:27 pm 
Northwood
Northwood
User avatar

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
Location: No. 1 Thread Killer
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
  Profile  
 
 Post subject:
PostPosted: Wed Mar 11, 2009 2:51 pm 
Northwood
Northwood
User avatar

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
Location: No. 1 Thread Killer
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 :P

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
  Profile  
 
 Post subject:
PostPosted: Wed Mar 11, 2009 3:28 pm 
Little Foot
Little Foot
User avatar

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
  Profile  
 
 Post subject:
PostPosted: Wed Mar 11, 2009 3:32 pm 
Northwood
Northwood
User avatar

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
Location: No. 1 Thread Killer
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
  Profile  
 
 Post subject:
PostPosted: Wed Mar 11, 2009 4:01 pm 
Little Foot
Little Foot
User avatar

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
  Profile  
 
 Post subject:
PostPosted: Thu Mar 12, 2009 4:01 am 
SON OF A GUN
SON OF A GUN
User avatar

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. :P


Top
  Profile  
 
 Post subject:
PostPosted: Thu Mar 12, 2009 10:44 am 
Northwood
Northwood
User avatar

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
Location: No. 1 Thread Killer
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. :P


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
  Profile  
 
 Post subject:
PostPosted: Thu Mar 12, 2009 3:09 pm 
Little Foot
Little Foot
User avatar

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
  Profile  
 
 Post subject:
PostPosted: Sat Mar 14, 2009 4:51 am 
Northwood
Northwood
User avatar

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
Location: No. 1 Thread Killer
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
  Profile  
 
 Post subject:
PostPosted: Sun Mar 15, 2009 9:40 pm 
Little Foot
Little Foot
User avatar

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
  Profile  
 
 Post subject:
PostPosted: Mon Mar 16, 2009 12:20 pm 
Northwood
Northwood
User avatar

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
Location: No. 1 Thread Killer
I like the Scala and Groovy version the best. :P 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
  Profile  
 
 Post subject:
PostPosted: Mon Mar 16, 2009 1:40 pm 
Little Foot
Little Foot
User avatar

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
  Profile  
 
 Post subject:
PostPosted: Mon Mar 16, 2009 5:38 pm 
Northwood
Northwood
User avatar

Joined: Sun Jul 15, 2007 6:37 pm
Posts: 2261
Location: No. 1 Thread Killer
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.


:P


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
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 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 3 guests


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

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group