尝试将字符串解析为整数时的数字格式异常

时间:2016-05-27 09:53:17

标签: java swing

我正在尝试将用户选择的JTable列转换为ArrayList。所有列都是字符串(来自数据库),因此在选择列时应该进行转换。当我使用下面的代码时,我得到一个NumberformatException并且我不确定为什么。

<DataGrid x:Name="RecodersSummary" Grid.Row="0" HorizontalAlignment="Left" VerticalAlignment="Top" BorderThickness ="1" IsReadOnly ="True" Loaded="RecordersSummary_Loaded" GridLinesVisibility="All" AutoGenerateColumns="False">
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}">
    <Setter Property="BorderBrush" Value="Black"/>
    <Setter Property="BorderThickness" Value="1,1,1,1"/>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
  <DataGridTextColumn Header="{DynamicResource Summary_FirstColumnHeader}"  Width="Auto" IsReadOnly="True"   Binding="{Binding Path=Summary}"/>
  <DataGridTextColumn Header="{DynamicResource Summary_SecondColumnHeader}" Width="Auto" IsReadOnly="True"  Binding="{Binding Path=Totals}"/>
  <DataGridTextColumn Header="{DynamicResource Summary_ThirdColumnHeader}" Width="Auto" IsReadOnly="True"  Binding="{Binding Path=Centralized}"/>
  <DataGridTextColumn Header="{DynamicResource Summary_ForthColumnHeader}" Width="Auto" IsReadOnly="True"  Binding="{Binding Path=Standalone}"/>
  <DataGridTextColumn Header="{DynamicResource Summary_FifthColumnHeader}" Width="Auto" IsReadOnly="True"  Binding="{Binding Path=Temporary}"/>
  <DataGridTextColumn Header="{DynamicResource Summary_SixthColumnHeader}" Width="Auto" IsReadOnly="True"  Binding="{Binding Path=Other}"/>
</DataGrid.Columns>

1 个答案:

答案 0 :(得分:4)

if (data[i] != null) {
    System.out.println(Integer.parseInt((data[i]).toString().trim()));
    descstats.add(Float.parseFloat((data[i]).toString().trim()));
}

您执行Integer.parseInt,然后使用Float.parseFloat保存该值。如果该数字为float,则Integer.parseInt将失败,不会强制转换为int(即Integer.parseInt("6.2")将引发NumberFormatException 1}},它不会返回6