简体   繁体   English

如何在调试 Java 代码时使用 Scanner 类对象在变量中设置值?

[英]How can i set value in variable using Scanner class object while debugging Java code?

I am facing problem while debugging my Java source code using jdb(Java Debugger).我在使用 jdb(Java Debugger) 调试 Java 源代码时遇到问题。 when i try to set value in instance variable using scanner class object it will display following output当我尝试使用扫描仪类对象在实例变量中设置值时,它将显示以下输出

Jdb Displaying following output Jdb 显示以下输出

I cant understand why i am getting this message "unrecognized command".我不明白为什么我收到这条消息“无法识别的命令”。 My java Source code is as follows我的java源代码如下

import java.util.Scanner;
class StringPlay{
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
    int t;
    // System.out.println("t");
    t=sc.nextInt();
    for(int l=0;l<t;l++){
            String s="",a="";
        int k=0,player=0;
        // System.out.println("s");
        s=sc.next();
        for(int i=0;i<s.length();i++){
            for(int j=i+1;j<s.length();j++){
                if((int)s.charAt(j)!=(int)s.charAt(i)){
                    // strcat(a,s[j]);
                    a+=s.charAt(j);
                    // k++;
                }
                if(j==s.length()-1){
                    s=a;
                    a="";
                    // string::replace(a,0,"");

                    player++;
                }
            }
        }
        if(player%2==0)System.out.println("player1");
        else System.out.println("player2");
    }

    }
}

Well I am using jdb version 1.8 (Java SE version 1.8.0_151).好吧,我使用的是 jdb 1.8 版(Java SE 1.8.0_151 版)。 and java version "9.0.1" Java(TM) SE Runtime Environment (build 9.0.1+11) Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode)和 java 版本“9.0.1”Java(TM) SE 运行时环境(构建 9.0.1+11)Java HotSpot(TM) 64 位服务器 VM(构建 9.0.1+11,混合模式)

and I am Working on Java in Ubuntu 16.04LTS.我正在 Ubuntu 16.04LTS 中研究 Java。

Thanks in advance for helping me.预先感谢您帮助我。

Try feeding the Scanner an input string for debugging with jdb.尝试向 Scanner 提供输入字符串以使用 jdb 进行调试。 Because your console input is being read by the jdb and not by the Scanner class.因为您的控制台输入正在被 jdb 读取,而不是被 Scanner 类读取。

You can try:你可以试试:

String myInput = "This is my input, that I want in my sc.";
Scanner sc = new Scanner(myInput);

Or you can feed it directly:或者你可以直接喂它:

Scanner sc = new Scanner("This is my input, that I want in my sc.");

Got this info from this post :这篇文章中得到了这个信息:

You're going to need to split up the running of your app, and the debugging in two different terminals.您将需要将应用程序的运行和在两个不同终端中的调试分开。

The first terminal needs to just run the app, but to run it in a way that jdb can connect to it later.第一个终端只需要运行应用程序,但以jdb稍后可以连接到它的方式运行它。 You can do this with this command: java -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n MyClass您可以使用以下命令执行此操作: java -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n MyClass

In your second debug window, you need to connect to your Java app by running: jdb -connect com.sun.jdi.SocketAttach:hostname=localhost,port=8000在第二个调试窗口中,您需要通过运行以下命令连接到 Java 应用程序: jdb -connect com.sun.jdi.SocketAttach:hostname=localhost,port=8000

Now, in order to put in values so your Scanner can read it, go back to the first terminal (the one running the app) and enter the arguments as normal.现在,为了输入值以便您的扫描器可以读取它,请返回到第一个终端(运行应用程序的终端)并正常输入参数。

Any breakpoints you set up will hit in your second debugging window.您设置的任何断点都将在您的第二个调试窗口中命中。

Hopefully that helps.希望这有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在NetBeans 7.1中调试Java代码时如何更改变量的值? - How can I change the value of a variable while debugging Java code in NetBeans 7.1? 在调用方法时,如何在循环内使用扫描仪输入的变量使用哨兵值时运行此 while 循环? - How can I get this while loop to run when using a sentinel value for a variable with scanner input inside of the loop, while calling methods? 在 VS Code 中调试时在 Java 类中的字段上设置断点 - Set a Breakpoint on a field in a Java class while debugging in VS Code 我可以在Java的整个类中使用一个Scanner变量吗? - Can i use one Scanner variable for a whole class in java? 在java中使用Scanner类时如何解决错误 - how to resolve the error while using Scanner class in java 如何使用List设置对象值 <Object> 到java中的模型类? - How to set object value using List<Object> to a model class in java? 如何在Java中使用扫描仪时删除ArrayList中的特定对象 - how to remove specific object in an ArrayList while using a scanner in java 如何在JAVA中设置扫描仪对象? - How do I set up a scanner object in JAVA? 如何通过 Java 中的 OutputStream 将值传递给子进程中的扫描仪 class 两次或更多次? - How can I pass value to Scanner class in subprocess two or more times by OutputStream in Java? 我可以在 Java 中使用 lambda 表达式的扫描仪(一类)吗? [等候接听] - Can I use Scanner(a class) using lambda expresion in Java? [on hold]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM