illegal escape character from String.format()

huangapple go评论66阅读模式
英文:

illegal escape character from String.format()

问题

Edit:

我在尝试编译我的Java程序时遇到了"error: illegal escape character"错误。

最小可重现示例:

public static void main(String... args) {
    String output = String.format("%7d%7d%5d\t%%5s\t%s", 1299.97, 465.64, 1963, "00694", "ATLAS CENTAUR 2");
}

期望输出:

 1299.97      465.64    1963      00694        ATLAS CENTAUR 2         

javac的输出:

Main.java:6: error: illegal escape character
        String output = String.format("%7d%7d%5d\t%%5s\t%s", 1299.97, 465.64, 1963, "00694", "ATLAS CENTAUR 2");
                                           ^
Main.java:6: error: illegal escape character
        String output = String.format("%7d%7d%5d\t%%5s\t%s", 1299.97, 465.64, 1963, "00694", "ATLAS CENTAUR 2");
                                               ^
2 errors

我该如何修复这个错误?

英文:

Edit:

I am getting "error: illegal escape character" when I try to compile my Java program.

Minimal reproducible example:

public static void main(String... args) {
    String output = String.format("%7d\%7d\%5d\t%%5s\t%s", 1299.97, 465.64, 1963, "00694", "ATLAS CENTAUR 2");
}

Expected output:

 1299.97      465.64    1963      00694        ATLAS CENTAUR 2         

Output from javac:

Main.java:6: error: illegal escape character
        String output = String.format("%7d\%7d\%5d\t%%5s\t%s", 1299.97, 465.64, 1963, "00694", "ATLAS CENTAUR 2");
                                           ^
Main.java:6: error: illegal escape character
        String output = String.format("%7d\%7d\%5d\t%%5s\t%s", 1299.97, 465.64, 1963, "00694", "ATLAS CENTAUR 2");
                                               ^
2 errors

How do I fix this?

Original question, can be ignored:

error at line 44 and I don't know how to fix it


Hi I am new to java programming. I want to write a java program to print out the satilites from a website. Error at line 44 and I don't know how to fix it. I'm not sure if I'm using the .format right. Can I please have some help?

This is the assignment:

Assignment 3 is:

Write 3 functions:

apogee which takes 2 parameters, mean motion and eccentricity and returns the apogee in kilometers.

perigee which takes 2 parameters, mean motion and eccentricity and returns the perigee in kilometers.

launch year yyyy which takes an integer parameter, the 2 digit launch year, and returns an integer, the 4 digit launch year.

    hint: use the date for the 1st ever artificial satellite launch to write a function that will work until 2057

Don't forget about the "assumed decimal point" for the eccentricity. (Use String concatenation to pre-pend a leading "0." to the extracted substring before converting it to double).

It should look like this :

 1299.97      465.64    1963      00694        ATLAS CENTAUR 2         
  813.27      765.76    1964      00733        THOR AGENA D R/B        
  753.32      648.28    1964      00877        SL-3 R/B                
  799.01      704.45    1967      02802        SL-8 R/B                
  616.16      574.02    1968      03230        SL-8 R/B                
  .....................................................

答案1

得分: 1

以下是翻译好的部分:

您可以在Formatter类的JavaDoc上查看_format specifiers_。

实数值使用_%f_。
整数值使用_%d_。

您可以使用以下内容来获得类似所需的输出。

"%7f %7f %5d %5s %s"
1299.970000 465.640000  1963 00694 ATLAS CENTAUR 2

您可以使用_%.2f_来指定一个标度,其中_.2_是标度。并且,您可以通过在宽度前面添加一个_0_来添加前导零。

"%7.2f %7.2f %5d %5s %s"
1299.97  465.64  1963 00694 ATLAS CENTAUR 2
英文:

You can review the format specifiers on the Formatter class JavaDoc.

Real number values use %f.
And, integer number values use %d.

You can use the following, to obtain a similar output as required.

"%7f %7f %5d %5s %s"
1299.970000 465.640000  1963 00694 ATLAS CENTAUR 2

You can specify a scale using %.2f, where .2 is the scale.
And, you can add leading zeros by adding a 0 before the width.

"%7.2f %7.2f %5d %5s %s"
1299.97  465.64  1963 00694 ATLAS CENTAUR 2

huangapple
  • 本文由 发表于 2023年6月6日 10:43:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76411129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定