4 Replies - 470 Views - Last Post: 09 March 2013 - 03:57 AM Rate Topic: -----

#1 DJ2334  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 08-February 13

Help with Integer.parseInt error

Posted 09 March 2013 - 02:44 AM

The four lines of code that I highlighted in red are the ones giving me the most trouble at the moment and I know it has something to do with the Integer.parseInt code that I highlighted in orange below, but I don't know how to fix it. This is what I want my program to display:

Quote

Station ID: SAN
The Altitude Weather for 3000 feet is: "random number from getAltitudeWeather"
Wind Direction: "random number from getWindDir" degrees
Wind Speed: "random number from getWindSpeed" knots
Wind Temperature: "random number from getWindTemp"


What it's currently displaying:

Quote

Station ID: SAN
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
at a19005.A19005.main(A19005.java:18)
Java Result: 1


Basically it wants me to put a string in the parentheses after getAltitudeWeather, getWindDir, getWindSpeed, and getWindTemp, but the only string it'll allow me to put is strStationWeather and when I run that this is what I get:

Quote

Station ID: SAN
Exception in thread "main" java.lang.NumberFormatException: For input string: "SAN 1905 1808+24 1512+17 1209+10 1708-06 2016-16 211831 211941 192652 "
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at a19005.NWSFB.getPos(NWSFB.java:30)
at a19005.NWSFB.getAltitudeWeather(NWSFB.java:71)
at a19005.A19005.main(A19005.java:18)
Java Result: 1


The main error that I get in the quote above has something to do with the code that I have highlighted in orange below. I hope this helps.

My two .java files are below (part of the same package though).

package a19005;
public class A19005
{
    public static void main(String[] args) 
    {
       final String strStationWeather = "SAN 1905 1808+24 1512+17 1209+10 1708-06 2016-16 211831 211941 192652 ";

       NWSFB myWeather = new NWSFB(strStationWeather);
       
       System.out.println("Station ID: " + myWeather.getStationID());
[COLOR=Red]       System.out.println("The Altitude Weather for 3000 feet is:  " + myWeather.getAltitudeWeather());
       System.out.println("Wind Direction: " + myWeather.getWindDir() + " degrees");
       System.out.println("Wind Speed: " + myWeather.getWindSpeed() + " knots");
       System.out.println("Wind Temperature: " + myWeather.getWindTemp());[/COLOR]
    }
}



package a19005;
public class NWSFB 
{
    private String strWeather;
    
    public NWSFB(String strVar)
    {
        strWeather = strVar;
    }

    public String getStationID()
    {
        String strStationID = strWeather.substring(0,3);
        return strStationID;
    }
    
    private int getPos(String strAlt)
    {      
        int intAlt;
        int intRet =0;
        [COLOR=Orange]intAlt = Integer.parseInt(strAlt);[/COLOR]
        
        switch (intAlt)
        {
            case 3:
                intRet = 4;
                break;
            case 6:
                intRet = 9;
                break;
            case 9:
                intRet = 17;
                break;
            case 12:
                intRet = 25;
                break;
            case 18:
                intRet = 33;
                break;
            case 24:
                intRet = 41;
                break;
            case 30:
                intRet = 49;
                break;
            case 34:
                intRet = 56;
                break;
            case 39:
                intRet = 63;
                break;
        }
        return intRet;
        }
    
    public String getAltitudeWeather(String strAlt)
    {
        int intPos = getPos(strAlt);
        String strRet1 = strWeather.substring(intPos,intPos+7);
        
        return strRet1;
    }
    
    public String getWindDir(String strAlt)
    {
        String strRet2 = getAltitudeWeather(strAlt);
        return strRet2.substring(0,2) + "0";
    }
    
    public String getWindSpeed(String strAlt)
    {
        String strRet3 = getAltitudeWeather(strAlt);
        return strRet3.substring(2,4);
    }
    
    public String getWindTemp(String strAlt)
    {
        String strRet4 = getAltitudeWeather(strAlt);
        return strRet4.substring(4,7);
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Help with Integer.parseInt error

#2 DJ2334  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 08-February 13

Re: Help with Integer.parseInt error

Posted 09 March 2013 - 02:50 AM

Well obviously the colors didn't show up and I'm new to this forum, so I don't know how to edit my first post. I'm used to forums having "Edit" right on the posts you made, but I have yet to see that option anywhere.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3354
  • View blog
  • Posts: 11,353
  • Joined: 12-December 12

Re: Help with Integer.parseInt error

Posted 09 March 2013 - 03:00 AM

Quote

"SAN 1905 1808+24 1512+17 1209+10 1708-06 2016-16 211831 211941 192652 "

If this is your string then you cannot pass it to parseInt() and expect it to return a number. You need to use substr(), as you have done throughout your code, to pass only a part of your string to parseInt(). For example, parseInt("1905") would work.

Note that your code is hard to read as each function is calling another function, which is calling another function..
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3354
  • View blog
  • Posts: 11,353
  • Joined: 12-December 12

Re: Help with Integer.parseInt error

Posted 09 March 2013 - 03:08 AM

I would investigate String.split() to split the string at the spaces.
Was This Post Helpful? 0
  • +
  • -

#5 DJ2334  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 08-February 13

Re: Help with Integer.parseInt error

Posted 09 March 2013 - 03:57 AM

I figured it out, thanks for the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1