使用输入更新数组中的数据

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

Update data in array with an input

问题

LocalDateTime myDateObj = LocalDateTime.now();
DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm:ss");
String dateExit = myDateObj.format(myFormatObj);
System.out.println("Date and time exited: " + dateExit);
dateExit = dateExit.substring(option - 1);

这是我的代码,有错误的代码行是这一行:

dateExit = dateExit.substring(option - 1);

首先,系统会要求用户输入姓名和其他详细信息。输入的日期和时间将由系统自动生成并存储到数组中。日期和时间退出的初始值是 "-",因为我们不知道用户何时退出。现在,当用户想要更新退出日期和时间时,系统也会自动生成时间,但现在我的问题是如何更新系统生成的退出日期和时间(已经有值的情况下)到我的数组中

非常感谢,一个Java新手

英文:
LocalDateTime myDateObj = LocalDateTime.now();
DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm:ss");
String dateExit = myDateObj.format(myFormatObj);
System.out.println("Date and time exited: " + dateExit);
dateExit = dateExit[option - 1];

So this is my code, the code with error is this line:

dateExit = dateExit[option - 1]

At first, the system will ask for the user input for name and other details. The date and time entered will be automatically generate by the system and store into the array. And the value for date and time exit is "-" at the beginning because we don't know what time the user is going to exit. Now, when the user want to update the date and time exit, the time will also automatically generate by the system but now my problem is How to update the date and time exit (which generated by the system) to the date and time exit in my array which already have a value inside.

Thanks a lot, a Java Newbie

答案1

得分: 0

dateExit在你的代码中是一个字符串对象。它不是一个数组。你不能对它执行dateExit[option - 1]操作。

英文:

dateExit in your code is a String object. Its not an array. You can not do dateExit[option - 1] on it.

答案2

得分: 0

也许这个代码片段可以帮助您。其中有两个数组被初始化,每个数组包含两个元素。一个用于存储2个LocalizedDateTime对象,另一个用于存储2个String对象。LocalizedDateTime是一个不可变类,这意味着它们不能被更改。您必须再次使用LocalDateTime.now()来创建一个新的对象。如果您已经正确初始化了一个数组(前两行),您可以通过方括号来访问它。正如之前的评论者已经提到的,dateExit不是一个数组,因此编译器会报错。

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class example {

    public static void main(String[] args) throws InterruptedException{

        LocalDateTime[] dateTimeArray = new LocalDateTime[2];
        String[] stringArray = new String[2];
        DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm:ss");

        // 获取开始时的日期和时间
        dateTimeArray[0] = LocalDateTime.now();
        stringArray[0] = dateTimeArray[0].format(myFormatObj);
        System.out.println("开始时的日期和时间:" + stringArray[0]);

        Thread.sleep(1000); // 模拟延迟

        // 获取结束时的日期和时间(退出)
        dateTimeArray[1] = LocalDateTime.now();
        stringArray[1] = dateTimeArray[1].format(myFormatObj);
        System.out.println("结束时的日期和时间:" + stringArray[1]);
    }
}
英文:

Maybe this snippet can help you. In it two arrays are initialized with two elements each. One for 2 LocalizedDateTime objects, and one for 2 String objects. LocalizedDateTime is an immutable class, meaning they cannot be changed. You have to create a new object by using LocalDateTime.now() again. If you have initalized an array properly (First two lines) you can access it via the square brackets. As the previous commetators already said, dateExit is not an array thus compiler complains.

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class example {

    public static void main(String[] args) throws InterruptedException{

        LocalDateTime[] dateTimeArray = new LocalDateTime[2];
        String[] stringArray = new String[2];
        DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm:ss");
        
        // Get date and time at beginning
        dateTimeArray[0] = LocalDateTime.now();
        stringArray[0] = dateTimeArray[0].format(myFormatObj);
        System.out.println("Date and time begin: " + stringArray[0]);

        Thread.sleep(1000); // Simulate delay

        //  Get date and time at end (exit)
        dateTimeArray[1] = LocalDateTime.now();
        stringArray[1] = dateTimeArray[1].format(myFormatObj);
        System.out.println("Date and time end: " + stringArray[1]);
    }
}

答案3

得分: 0

代码存在问题

您没有将 dateExit 声明为 String[],但却将其视为 String[]

解决方法

  1. dateExit 声明为 String[]
  2. 此外,为了简化,建议您使用不同的模式格式化 LocalDateTime,以避免需要拆分格式化的日期时间字符串。

演示:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        // 初始化 dateExit[]
        String[] dateExit = { "-", "-" };

        // 现在
        LocalDateTime now = LocalDateTime.now();

        // 将日期字符串赋给 dateExit[0]
        dateExit[0] = now.format(DateTimeFormatter.ofPattern("dd-MMM-yyyy"));

        // 将时间字符串赋给 dateExit[1]
        dateExit[1] = now.format(DateTimeFormatter.ofPattern("HH:mm:ss"));

        // 显示 dateExit[]
        System.out.println(Arrays.toString(dateExit));

        // 显示各个元素
        System.out.println("Exit date: " + dateExit[0]);
        System.out.println("Exit time: " + dateExit[1]);
    }
}

输出:

[22-Jul-2020, 11:39:27]
Exit date: 22-Jul-2020
Exit time: 11:39:27
英文:

Problem with your code

You have not declared dateExit as String[] but treated it as a String[].

Solution

  1. Declare dateExit as String[].
  2. Also, for simplicity, I suggest you format LocalDateTime using separate patterns to avoid the need to split the formatted date-time string.

Demo:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;

public class Main {
	public static void main(String[] args) {
		// Initial state of dateExit[]
		String[] dateExit = { "-", "-" };

		// Now
		LocalDateTime now = LocalDateTime.now();

		// Assign date string to dateExit[0]
		dateExit[0] = now.format(DateTimeFormatter.ofPattern("dd-MMM-yyyy"));

		// Assign time string to dateExit[1]
		dateExit[1] = now.format(DateTimeFormatter.ofPattern("HH:mm:ss"));

		// Display dateExit[]
		System.out.println(Arrays.toString(dateExit));

		// Display individual elements
		System.out.println("Exit date: " + dateExit[0]);
		System.out.println("Exit time: " + dateExit[1]);
	}
}

Output:

[22-Jul-2020, 11:39:27]
Exit date: 22-Jul-2020
Exit time: 11:39:27

huangapple
  • 本文由 发表于 2020年7月22日 17:36:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63031202.html
匿名

发表评论

匿名网友

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

确定