调试器在学习任务中从不在断点处停止。

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

The debugger never stops at breakpoints in a learning task

问题

Here are the translations of your code and questions:

    import java.time.LocalDate;
    import java.util.Comparator;
    import java.util.List;
    import java.util.Objects;
    import java.util.Scanner;
    import java.util.stream.Collectors;
    import java.util.stream.Stream;
    
    class ProcessDeliveryOrders {
    
        public static DeliveryOrder findFirstOrder(List<DeliveryOrder> orders) {
            return new DeliveryOrder();  // Breakpoint.
            // write your code here
        }
    
        public static void printAddressesToDeliver(List<DeliveryOrder> orders) {
            System.out.println(); // Breakpoint.
            // write your code here
        }
    
        public static void main(String[] args) {
            final Scanner scanner = new Scanner(System.in);
    
            List<DeliveryOrder> orders = Stream.iterate(1, i -> scanner.hasNextLine(), i -> i + 1)
                    .map(i -> scanner.nextLine().split("\\|"))
                    .map(parts -> new DeliveryOrder(
                            Long.parseLong(parts[0]), parts[2], LocalDate.parse(parts[1])))
                    .collect(Collectors.toList());
    
            System.out.println(findFirstOrder(orders));  // Breakpoint.
    
            printAddressesToDeliver(orders);
        }
    }
    
    class DeliveryOrder {
        private final long orderId;
        private final String address;
        private final LocalDate deliveryDate;
        // there are even more fields: customer name, phone, products info, etc
    
        public DeliveryOrder() {
            this.orderId = -1;
            this.address = "No address";
            this.deliveryDate = LocalDate.MIN;
        }
    
        public DeliveryOrder(long orderId, String address, LocalDate deliveryDate) {
            this.orderId = orderId;
            this.address = address;
            this.deliveryDate = deliveryDate;
        }
    
        public static Comparator<DeliveryOrder> getComparatorByDeliveryDate() {
            return Comparator.comparing(DeliveryOrder::getDeliveryDate);
        }
    
        public long getOrderId() {
            return orderId;
        }
    
        public String getAddress() {
            return address;
        }
    
        public LocalDate getDeliveryDate() {
            return deliveryDate;
        }
    
        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            DeliveryOrder that = (DeliveryOrder) o;
            return address.equals(that.address) &&
                    deliveryDate.equals(that.deliveryDate);
        }
    
        @Override
        public int hashCode() {
            return Objects.hash(address, deliveryDate);
        }
    
        @Override
        public String toString() {
            return orderId + "|" + deliveryDate + "|" + address;
        }
    }

As for your questions:

  1. 为什么调试器从未到达代码中的这三个断点?
  2. 如何处理这个问题?

To address your first question, the debugger not reaching breakpoints could be due to several reasons:

  • The breakpoints may not be correctly set in your development environment.
  • The code might not be executed because of issues like incorrect input or an error in your setup.
  • The debugger settings or configurations might not be correctly configured.

To cope with this problem, you can try the following steps:

  • Double-check your debugger setup and ensure that breakpoints are set in the correct locations.
  • Verify that your input data matches the expected format and that it is being read correctly.
  • Review your debugger's configurations and settings to make sure they are appropriate for your project.

Debugging can be complex, and the specific solution may depend on your development environment and tools. If you continue to face issues, consider seeking assistance from a mentor or colleague with debugging experience, or consult the documentation for your development environment.

英文:
import java.time.LocalDate;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class ProcessDeliveryOrders {
public static DeliveryOrder findFirstOrder(List&lt;DeliveryOrder&gt; orders) {
return new DeliveryOrder();  // Breakpoint.
// write your code here
}
public static void printAddressesToDeliver(List&lt;DeliveryOrder&gt; orders) {
System.out.println(); // Breakpoint.
// write your code here
}
public static void main(String[] args) {
final Scanner scanner = new Scanner(System.in);
List&lt;DeliveryOrder&gt; orders = Stream.iterate(1, i -&gt; scanner.hasNextLine(), i -&gt; i + 1)
.map(i -&gt; scanner.nextLine().split(&quot;\\|&quot;))
.map(parts -&gt; new DeliveryOrder(
Long.parseLong(parts[0]), parts[2], LocalDate.parse(parts[1])))
.collect(Collectors.toList());
System.out.println(findFirstOrder(orders));  // Breakpoint.
printAddressesToDeliver(orders);
}
}
class DeliveryOrder {
private final long orderId;
private final String address;
private final LocalDate deliveryDate;
// there are even more fields: customer name, phone, products info, etc
public DeliveryOrder() {
this.orderId = -1;
this.address = &quot;No address&quot;;
this.deliveryDate = LocalDate.MIN;
}
public DeliveryOrder(long orderId, String address, LocalDate deliveryDate) {
this.orderId = orderId;
this.address = address;
this.deliveryDate = deliveryDate;
}
public static Comparator&lt;DeliveryOrder&gt; getComparatorByDeliveryDate() {
return Comparator.comparing(DeliveryOrder::getDeliveryDate);
}
public long getOrderId() {
return orderId;
}
public String getAddress() {
return address;
}
public LocalDate getDeliveryDate() {
return deliveryDate;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
DeliveryOrder that = (DeliveryOrder) o;
return address.equals(that.address) &amp;&amp;
deliveryDate.equals(that.deliveryDate);
}
@Override
public int hashCode() {
return Objects.hash(address, deliveryDate);
}
@Override
public String toString() {
return orderId + &quot;|&quot; + deliveryDate + &quot;|&quot; + address;
}
}

This is a learning task.

To run it I enter this into the console:

Sample Input:

1|2021-09-03|112 Mammoth Street, Colorado Springs, CO 80911
2|2021-09-05|369 Woodside Court, Troy, NY 12180
3|2021-09-02|837 Bowman Street, Helena, MT 59601
4|2021-09-03|112 Mammoth Street, Colorado Springs, CO 80911

Problem

The debugger never reaches any of the three breakpoints marked in the code.
Without stopping on each of these breakpoints I can't understand the task and proceed.

Could you help me understand:

  1. Why the debugger never reaches any of these breakpoints?
  2. How to cope with this problem?

答案1

得分: 2

这不是一个答案,但是:

(我打算撤回“不是一个答案”的部分,这可能是一个答案。重复我的评论:)

(我在这里使用的StringReader用于调试您的代码,关闭了Scanner的输入。System.in实际上从未关闭。因此,您实际上从未到达读取输入后面的代码部分。您可以尝试这个版本,它将帮助您达到这些语句。)

可能会有所帮助的是,您的输入可能存在问题。键入所有您那里拥有的内容相当复杂,可能容易出错。

如果您在程序内部使用字符串作为输入,那对您和我们来说都是可重现的。然后,我们都可以确保我们看到的代码问题是相同的。

例如,将一些测试向量添加到您的代码中:

class ProcessDeliveryOrders {

   static String testVectors = "1|2021-09-03|112 Mammoth Street, Colorado Springs, CO 80911\n" +
"2|2021-09-05|369 Woodside Court, Troy, NY 12180\n" +
"3|2021-09-02|837 Bowman Street, Helena, MT 59601\n" +
"4|2021-09-03|112 Mammoth Street, Colorado Springs, CO 80911";

为此,我只需复制您在问题中拥有的字符串,并将它们粘贴在字符串文字""的引号之间。

然后,只需将该字符串用作程序中的“输入”:

public static void main(String[] args) {
    final Scanner scanner = new Scanner( new StringReader( testVectors ) );

    List<DeliveryOrder> orders = Stream.iterate(1, i -> scanner.hasNextLine(), //... 等等。

现在,您的代码对每个人都完全相同,我们都可以调试完全相同的问题。

英文:

This is not an answer, but:

(I'm going to take back the "not an answer" part, this might be an answer. Repeating my comment:)

(The StringReader that I used here to debug your code closes the input to Scanner. System.in never actually closes. Thus you never actually reach the code that is after the part that reads input. You might try this version, it'll help you reach those statements.)

Something that might help is your input might be in question. Typing all of what you have there is pretty complicated and might be error prone.

If you use a string inside the program as your input, it's reproducible for you and for us also. Then we can all be assured that we are seeing the same issues with the code.

For example, add some test vectors to your code:

class ProcessDeliveryOrders {
static String testVectors = &quot;1|2021-09-03|112 Mammoth Street, Colorado Springs, CO 80911\n&quot; +
&quot;2|2021-09-05|369 Woodside Court, Troy, NY 12180\n&quot; +
&quot;3|2021-09-02|837 Bowman Street, Helena, MT 59601\n&quot; +
&quot;4|2021-09-03|112 Mammoth Street, Colorado Springs, CO 80911&quot;;

To do this I literally just copied the strings you have in your question and pasted them in between the quotes of a string literal &quot;&quot;.

Then just use that string as "input" in your program:

public static void main(String[] args) {
final Scanner scanner = new Scanner( new StringReader( testVectors ) );
List&lt;DeliveryOrder&gt; orders = Stream.iterate(1, i -&gt; scanner.hasNextLine(), //... etc.

Now your code works exactly the same way for everyone, and we can all debug exactly the same problems.

huangapple
  • 本文由 发表于 2023年5月22日 00:48:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300961.html
匿名

发表评论

匿名网友

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

确定