String incomingNumbers[ ] = writtenNumber.split("\\-");
Run Code Online (Sandbox Code Playgroud)
该程序接受自然语言编号,如三十二或五.
那么如果输入五个,那我的incomingNumbers数组中有什么东西?
在Java中,我如何在数组中存储数字,我的意思是真正的长数字,例如高达1万亿,所以我可以访问它们并用文字打印它们是什么?
import java.io.BufferedReader;
import java.util.Collections;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import com.javaranch.common.TextFileIn;
public class SortNames
{
public static class CelebrityNamesFile
{
public String firstName;
public String lastName;
public static class CompareLastName implements Comparator< CelebrityNamesFile >
{
@Override
public int compare( CelebrityNamesFile o1, CelebrityNamesFile o2 )
{
return o2.lastName.compareTo( o1.lastName );
}
}
public static void main( String[ ] args ) throws IOException
{
ArrayList< CelebrityNamesFile > myCelebrityList;
myCelebrityList = new …
Run Code Online (Sandbox Code Playgroud) Java,我如何在main之外创建一个哈希映射但在main或其他方法中引用它引入java.util.*;
import java.util.Map.Entry;
// Create a hash map
HashMap<String, Double> hm = new HashMap<String, Double>();
// Put elements into the map
hm.put("John Doe", new Double(3434.34));
hm.put("Tom Smith", new Double(123.22));
hm.put("Jane Baker", new Double(1378.00));
hm.put("Todd Hall", new Double(99.22));
hm.put("Ralph Smith", new Double(-19.08));
class HashMapDemo
{
public static void main(String args[])
{
// Get a set of the entries
Set<Entry< String, Double> > set = hm.entrySet();
// Get an iterator
Iterator<Entry< String, Double> > i = set.iterator();
// Display elements
while(i.hasNext())
{ …
Run Code Online (Sandbox Code Playgroud) 这是代码
private static HashMap naturalNumbers = new HashMap();
static
{
naturalNumbers.put("zero", new Integer( 0 ) );
naturalNumbers.put("one", new Integer( 1 ) );
naturalNumbers.put("two", new Integer( 2 ) );
naturalNumbers.put("three", new Integer( 3 ) );
}
private static int findANumber( String partOfaNumber ) throws Exception
{
int multiplicand = 0;
multiplicand += (Integer)naturalNumbers.get( partOfaNumber );
Run Code Online (Sandbox Code Playgroud)
如果"get"返回null,我该如何检查?
我试过了:
if ( (Integer)naturalNumbers == null )
{
throw new Exception( "Number not found" );
}
return multiplicand;
}
Run Code Online (Sandbox Code Playgroud)
但IDE甚至不接受它:无法从HashMap转换为Integer.
Eclipse抱怨我的catch
声明如下
public class NaturalLanguageMultiply
{
public class WrongMultiplierException extends Exception
{
}
private static int toInt( String number ) throws WrongMultiplierException
{
// removed for clarity
try
{
String numberKey = scanner.next();
if ( numberMap.containsKey( numberKey ) )
{
multiplier += ( Integer ) numberMap.get( numberKey );
}
else
{
throw new WrongMultiplierException();
}
}
Run Code Online (Sandbox Code Playgroud)
它抱怨以下捕获线:
Syntax error on tokens
catch ( WrongMultiplierException );
{
}
}
Run Code Online (Sandbox Code Playgroud)
另外,StackOverflow为什么一直在问:你的帖子没有太多的上下文来解释代码部分; 请更清楚地解释您的情景.我在FAQ或帮助中找不到答案.
我有一个大于2,147,483,647的字符串,我需要解析它.Integer.parseInt不会执行它们,它会抛出异常.
java ×7
arrays ×1
collections ×1
comparator ×1
hashmap ×1
parsing ×1
sorting ×1
split ×1
string ×1