2015-10-06 35 views
0

在我的程序中,用户用TEMP输入一个float数字(例如TEMP 10.05)。 该程序应该只采取float部分,并将其转换为fareheit。最后将结果打印出来。 我该怎么做?如何分隔用字符串输入的浮点数?

public static void main(String[] args) { 
     Scanner s = new Scanner(System.in); 
     System.out.println("The following program takes a float value with the word 'TEMP'in celcius, and converts into farenheit"); 
     System.out.println("Enter the temperature with TEMP: "); 

     while (true) { 
      String input = s.next(); 
      //converting into farenheit 
      if (input != null && input.startsWith("TEMP")) { 
       float celsius = Float.parseFloat(input.substring(input.indexOf(' ') + 1)); 
float tempFaren=celcius+32.8; 
       // float=result 
       System.out.println("Temperature in farehheit is : "+tempFaren+ " F."); 

      } 
     } 

    } 

该方案显示此错误:

enter image description here

+0

你是什么意思“只采取浮动部分”?你的意思是“一切都在”TEMP“之后?如果是这样,子串可能是你的朋友... –

+0

这是一个字符串,找出数字开始的位置,提取那部分字符串,然后http://stackoverflow.com/questions/7552660/java-convert-float-to-string-and-string-to-float –

+0

作为建议,您希望使用'Scanner#nextLine'而不是'Scanner#next',因为后者会读取'' TEMP“'而不是float部分 –

回答

0

可以使用的indexOf找到第一空间,子串的空间位置后得到的字符串测试和parseFloat从字符串解析数转换为一个float:

float celsius = Float.parseFloat(input.substring(input.indexOf(' ') + 1)); 

同样的事情分解成以下步骤:

int spacePos = input.indexOf(' '); 
String celsiusStr = input.substring(spacePos + 1); 
float celsius = Float.parseFloat(celsiusStr); 

UPDATE

您的修改代码不会编译(您在“celcius”和其他问题中输入错误)。

这将编译,并正确地解析浮点部分:

String input = "TEMP 10.5"; 
float celsius = Float.parseFloat(input.substring(input.indexOf(' ') + 1)); 
float tempFaren = celsius + 32.8f; 
System.out.println("Temperature in farehheit is : " + tempFaren + " F."); 

最后, 另一种方式来提取在所述字符串的末尾浮点值是从开始剥离非数字值,用于例如:

float celsius = Float.parseFloat(input.replaceAll("^\\D+", "")); 

免责声明:无我上面给会为所有可能的输入工作的例子,它们都适合你给的例子输入。必要时可以使它们更健壮。

+0

janos,谢谢你的好答案,但它没有帮助。请参阅我已更新我的帖子。我已发布我的错误。 – Esha

+0

janos,my不要成为一个ngry。当然,你回答的帮助。没有它,我无法做到。然而,非常感谢gre_gor,因为他指出了重要的事情。 – Esha

1

你可以使用Float.parseFloat(yourString);

例子:

String x = "TEMP 10.5"; 
    float y = Float.parseFloat(x.substring(5,x.length())); 
    System.out.println(y); 
+0

它不会工作,因为字符串x = TEMP 10.5,那么它如何忽略TEMP和考虑只有10.5? – Esha

+0

@Esha再次检查答案。 – ninesalt

0

尝试这样的事情

while (true) { 
      String input = s.nextLine(); 
      //converting into farenheit 

     if (input != null && input.startsWith("TEMP")) { 
      try{ 
       double faren=Double.parseDouble(input.substring(input.lastIndexOf(' ')+1))+32.8; 
       //float tempIntoFarenheit= input+32.8 
       System.out.println("Temperature in farenheit: "+faren); 

      }catch(Exception e){ 
       System.out.println("Something was wrong with the temperature, make sure the input has something like 'TEMP 50.0' "); 
       System.out.println(e.toString()); 
      } 


     } else { 
      System.out.println("Wrong input. Try again: "); 
     } 
    } 
1

与您的代码的问题是,你使用

String input = s.next(); 

这只回报TEMP。您需要使用

String input = s.nextLine(); 

这应该返回完整的字符串。

和你无关的问题,你也是converting the temperatures错了。它应该是

float tempFaren = celcius*1.8f + 32.0f; 
+0

谢谢,它帮助我肯定! – Esha

0

您可以使用下面的方法:

static float extractFloatWithDefault(String s, float def) { 
    Pattern p = Pattern.compile("\\d+(\\.\\d+)?"); 
    Matcher m = p.matcher(s); 
    if (!m.find()) return def; 
    return Float.parseFloat(m.group()); 
} 

像这样:

 while (true) { 
     String input = s.next(); 
     //converting into farenheit 
     if (input != null && input.startsWith("TEMP")) { 
      float celsius = extractFloatWithDefault(input, -999); 
      if (celsius > -999) { 
       float tempFaren=celcius+32.8; 
       System.out.println("Temperature in farehheit is : "+tempFaren+ " F."); 
      } 
      else System.out.println("Please include a number"); 
     } 
    } 

此方法将提取它发现在字符串和使用它的第一个数字或如果没有有效的浮点数或整数,则返回默认值。

相关问题