相关疑难解决方法(0)

java.util.Scanner跳过输入请求

我正在尝试使用java.util.Scanner以下方式接收输入:

Scanner scanner = new Scanner(System.in);
int bla = scanner.nextInt();
String blubb = scanner.nextLine();
Run Code Online (Sandbox Code Playgroud)

但是nextLine()命令只是被跳过并返回一个空字符串.我怎么解决这个问题?

java java.util.scanner

2
推荐指数
1
解决办法
2460
查看次数

为什么重新排列变量后输出不同?

我有以下代码,称之为代码#1

Scanner keyboard = new Scanner(System.in);
String s = keyboard.nextLine();
int x = keyboard.nextInt();
double y = keyboard.nextDouble();
System.out.println("String: "+s);
System.out.println("Double: "+y);
System.out.println("Int: "+x);
Run Code Online (Sandbox Code Playgroud)

如果我输入

Hello World
12.1
12 
Run Code Online (Sandbox Code Playgroud)

输出将是

String: Hello World
Double: 12.1
Int: 12
Run Code Online (Sandbox Code Playgroud)

但是,如果我重新排列我的代码,请将其称为 code#2,如

Scanner keyboard = new Scanner(System.in);
int x = keyboard.nextInt();
double y = keyboard.nextDouble();
String s = keyboard.nextLine();
System.out.println("String: "+s);
System.out.println("Double: "+y);
System.out.println("Int: "+x);
Run Code Online (Sandbox Code Playgroud)

我输入

12
12.1
Run Code Online (Sandbox Code Playgroud)

编译器跳过字符串输入和输出

String:
Double: 12.1
Int: 12 
Run Code Online (Sandbox Code Playgroud)

这对我来说很奇怪。我被告知编译器总是从上到下阅读。我想象编译器将 code#2 读取为

int x = keyboard.nextInt();
Run Code Online (Sandbox Code Playgroud)

首先等待用户输入一个整数,以便将其分配给 x。然后读 …

java

2
推荐指数
1
解决办法
81
查看次数

scan.nextLine不起作用

我使用Java,我尝试编写可以读取大量患者的程序,然后让员工输入他们的姓名和年龄.

我希望程序读取全名,如(Maha Saeed),所以我写了这样的代码,但我不知道为什么它不起作用

name = scan.nextLine();
Run Code Online (Sandbox Code Playgroud)

这是完整的代码

import java.util.*;

public class Answer1
{
static Scanner scan = new Scanner (System.in);
public static void main (String[] args)
{

int age ; 
int PatientNumber ;
int PatientNO;
String name ;


System.out.print("Enter number of patients :");

PatientNumber =  scan.nextInt();
PatientNO = 1;

while ( PatientNO <= PatientNumber)
{
System.out.println("Patient #" +PatientNO);
System.out.print("Enter patient's Name: ");
name = scan.nextLine(); //<- here is the problem if I write scan.next it works but it reads only …
Run Code Online (Sandbox Code Playgroud)

java

1
推荐指数
1
解决办法
8515
查看次数

String Array抛出错误-Java

我是Java的新手,这可能是一个愚蠢的问题,但我真的需要你的帮助.

码:

String str[] ={"Enter your name","Enter your age","Enter your salary"};
        Scanner sc = new Scanner(System.in);
        int[] i = new int[2];
        String[] s = new String[2];
        int[] y = new int[2];
        for(int x = 0  ; x <= 2 ; x++)
        {
            System.out.println(str[0]);
            s[x] = sc.nextLine();
            System.out.println(s[x]);

            System.out.println(str[1]);
            i[x]=sc.nextInt();
            System.out.println(i[x]);

            System.out.println(str[2]);
            y[x]=sc.nextInt();
            System.out.println(y[x]);
        }
Run Code Online (Sandbox Code Playgroud)

输出:

run:
Enter your name
Sathish
Sathish
Enter your age
26
26
Enter your salary
25000
25000
Enter your name

Enter your age
23
23
Enter …
Run Code Online (Sandbox Code Playgroud)

java netbeans indexoutofboundsexception

1
推荐指数
1
解决办法
264
查看次数

正在跳过扫描仪nextLine

所以这是我使用的代码:

System.out.println("Create a name.");
name = input.nextLine();
System.out.println("Create a password.");
password = input.nextLine();
Run Code Online (Sandbox Code Playgroud)

但是当它到达这一点时,它只是说"创建一个名字".和"创建密码".两个在同一时间然后我必须输入一些东西.所以它基本上是跳过我需要键入字符串的Scanner部分.在"创建一个名字"之后.和"创建密码".是打印出来的,然后输入,名称和密码都改为我输入的内容.如何解决这个问题?

这是全班.我只是测试所以它实际上不是一个程序:

package just.testing;

import java.util.Scanner;
public class TestingJava
{
    static int age;
    static String name;
    static String password;
    static boolean makeid = true;
    static boolean id = true;

    public static void main(String[] args){
        makeid(null);
        if(makeid == true){
            System.out.println("Yay.");
        }else{

        }
    }
    public static void makeid(String[] args){
        System.out.println("Create your account.");
        Scanner input = new Scanner(System.in);
        System.out.println("What is your age?");
        int age = input.nextInt();
        if(age<12){
            System.out.println("You are too …
Run Code Online (Sandbox Code Playgroud)

java

0
推荐指数
1
解决办法
2万
查看次数

Java do/while循环抵消前两个问题

我在java编程类中编写程序.该程序是一个简单的do/while循环,它使用扫描仪类和带有布尔值的启动读取来询问一些问题.当循环第一次迭代时,它可以正常工作,但是当它第二次迭代时,它会立即显示第一个问题和第二个问题,并且只允许我回答第二个问题.我希望这很清楚.有人有任何见解吗?码:

      /*
      * Programmed By: Cristopher Ontiveros
      * Date: 9/19/13
 * Description: Compute weekly salary and witholding for employees
 */
package project.one.cristopher.ontiveros;

import java.util.Scanner;
public class ProjectOneCristopherOntiveros {

     static Scanner sc = new Scanner(System.in);
     public static void main(String[] args) {
     String fName, lName;
     double rate=0, wPay=0, withholding=0;
     int option = 0;
     boolean badval = true;
         System.out.println("Welcome to the Pay Calculator");


     do{
        System.out.println("Enter employee first name: ");
        fName = sc.nextLine();
        System.out.println("Enter employee last name: ");
        lName = sc.nextLine();
        System.out.println("Enter employee hourly …
Run Code Online (Sandbox Code Playgroud)

java loops boolean

0
推荐指数
1
解决办法
852
查看次数

Java,Do-While循环问题的新手

我对Java比较新,我在运行这个do-while循环时遇到了一些麻烦.

问题1

编写一个程序,允许用户将度数从摄氏度到华氏度或华氏度转换为摄氏度.使用以下公式:Degrees_C = 5(Degrees_F - 32)/ 9 Degrees_F =(9(Degrees_C)/ 5)+ 32提示用户输入温度,C或c表示摄氏度或F或f表示华氏度.如果输入摄氏度,则将温度转换为华氏温度;如果输入华氏温度,则将温度转换为摄氏温度.以可读格式显示结果.如果输入C,c,F或f以外的任何内容,则打印错误消息并停止.

问题2

让我们继续问题1,但使用循环以便用户可以转换其他温度.如果用户在温度之后输入大写或小写的C或F以外的字母,则打印错误消息并要求用户重新输入有效选择.每次转换后,要求用户键入Q或q退出或按任何其他键重复循环并执行另一次转换

public class Lab_4 {

    public static void main(String arg[]) {
        Scanner input = new Scanner(System.in);
        double F; //Fahrenheit
        double C; //Celsius
        String method; 
        boolean keepGoing = true; 
        do {            
            System.out.println("Choose a method:  ");
            System.out.println("(F)  Fahrenheit to Celsius.  ");
            System.out.println("(C)  Celsius to Fahrenheit.  ");
            System.out.println("(Q)  Exit the loop.  ");
            method = input.nextLine();
            if (method.charAt(0) == 'f' || method.charAt(0) == 'F') {
                System.out.println("Method F");
                System.out.println("Enter the temperature in …
Run Code Online (Sandbox Code Playgroud)

java loops boolean while-loop

0
推荐指数
1
解决办法
1287
查看次数

Java,在do-while循环结束时不注册用户输入

我有这个代码:

import java.util.*;

public class MyAccount {
    public static double balance = 0.0;

    public static double deposit(double deposit){
        return balance += deposit;
    }
    //public void setBalance(double balance){
    //  this.balance = balance;
    //}
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String redo = "";
        do{
        System.out.println("What do you want to do today?");
        String answer= in.nextLine();
        if(answer.equals("deposit")){
            System.out.println("How much do you want to deposit?");
            double money = in.nextDouble();
            deposit(money);
        }
        System.out.println("Your balance is " + balance);
        System.out.println("Anything else(Y …
Run Code Online (Sandbox Code Playgroud)

java input while-loop

0
推荐指数
1
解决办法
69
查看次数

读取整数和双读后如何读取整个字符串?

所以我有以下输入:

3
3.0
How are you doing
Run Code Online (Sandbox Code Playgroud)

我需要将它们存储到以下变量中:

int myInt
double myDouble
String myString
Run Code Online (Sandbox Code Playgroud)

这是我尝试过的:

myInt = scan.nextInt();
myDouble = scan.nextDouble();
myString = scan.nextLine();
Run Code Online (Sandbox Code Playgroud)

这适用于整数和Double值,但String给了我一个NULL值.

然后我尝试了这个:

myInt = scan.nextInt();
myDouble = scan.nextDouble();
myString = scan.next();
Run Code Online (Sandbox Code Playgroud)

它再次适用于前两个,但字符串只保留第一个字.我怎样才能读完整个字符串?我需要设置循环吗?

java string

0
推荐指数
1
解决办法
5038
查看次数

System.out.println打印字符串但System.out.print不打印

我试图将单词存储在java数组中由昏迷分隔
的文件中.文件是

年龄,收入,学生,信用评级,类:购买电脑

青年,高,不,公平,无

青年,高,不,优秀,无

中年,高,不,优秀,没有

高级,中等,不,公平,是

高级,低,是的,公平的,是的

高级,低,是的,优秀的,没有

public class Test {
  public static void main(String args[]) throws FileNotFoundException, IOException{ 
    FileInputStream f=new FileInputStream("F:\\pr\\src\\dmexam\\inp2.txt");
    int size,nr=7,nc=5,j=0,i=0;
    char ch;
    String table[][]=new String[nr][nc];
    size=f.available();
    table[0][0]=new String();
    while(size--!=0){
         ch=(char)f.read();
         if(ch=='\n')
         {
             i++;
             if(i>=nr)
                 break;
             table[i][0]=new String();
             j=0;
             continue;

         }
         if(ch==',')
         {     
            j++;
            table[i][j]=new String();
            continue;
         }
          table[i][j]+=ch;
    }
    f.close();
    System.out.println("The given table is:::---");
    for(i=0;i<nr;i++){
       for(j=0;j<nc;j++){ 
         System.out.print("  "+table[i][j]);
         System.out.print("  ");
       }
     }
 }  
}
Run Code Online (Sandbox Code Playgroud)

但输出是

给定的表是::: ---

但如果for改变了这样的话

System.out.println("The given table is:::---");
for(i=0;i<nr;i++){
    for(j=0;j<nc-1;j++){ …
Run Code Online (Sandbox Code Playgroud)

java printing file

0
推荐指数
1
解决办法
486
查看次数

使用nextInt()方法后使用nextLine()方法

使用nextLine方法后无法使用nextInt方法。这是下面给出的注释...

在黑客等级中的注释:(如果您在该nextLine()方法之后立即使用该方法nextInt(),请回想起nextInt()读取整数标记;因此,该整数输入行的最后一个换行符仍在输入缓冲区中排队,下一个nextLine()将读取整数行的其余部分(为空)。nextLine方法不会被跳过,但它为空。

import java.util.Scanner;

public class Solution {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int i = scan.nextInt();
    double d=scan.nextDouble();
    String s=scan.nextLine();
    // Write your code here.

    System.out.println("String: " + s);
    System.out.println("Double: " + d);
    System.out.println("Int: " + i);
  }
}
Run Code Online (Sandbox Code Playgroud)

输出:字符串:双精度:3.1415整数:42

java stdin scanning

-1
推荐指数
2
解决办法
3254
查看次数

Scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?") 是什么意思?

我一直在解决一些 hackerrank 基本 JAVA 问题,其中一个问题陈述有以下代码:

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {



    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int N = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
        scanner.close();
        if(N%2!=0){
            System.out.println("Weird");
        }
        else if(N%2==0 && N<=5 && N>=2){
             System.out.println("Not Weird");

        }
        else if(N%2==0 && N<=20 && N>=6){
             System.out.println("Weird");
    }
    else if(N%2==0 && N>20){
             System.out.println("Not Weird");
}}}
Run Code Online (Sandbox Code Playgroud)

你能解释一下“scanner.skip(”(\r\n|[\n\r\u2028\u2029\u0085])吗?“)”我知道它会跳过某些模式,但括号里是什么?

java java.util.scanner

-1
推荐指数
1
解决办法
4787
查看次数