我想让用户能够打印客户的副本,如果他们愿意的话.我正在考虑将整个类对象转换为字符串,然后设置为富文本框控件,格式类似于以下内容:
Name: blah blah
Age: blah blah
Email: blah blah
Description: blah blah blah
blah blah blah blah blah
Run Code Online (Sandbox Code Playgroud)
等等.是否有一种简单的方法来完成行间距/特殊格式化?
在此先感谢,Ari
我正在刷我的C++,我试图找出为什么我的toString函数没有输出我定义的格式化字符串.
我指的功能是: friend std::ostream& operator<<(std::ostream&, const Employee&);
Employee.cpp
#include <iostream>
#include <stdio.h>
using namespace std;
class Employee {
private:
string name;
double rate;
double hours;
double getPay() const;
friend std::ostream& operator<<(std::ostream&, const Employee&);
public:
Employee(string, double);
void setHours(double);
};
Employee::Employee(string name, double rate) {
this->name = name;
this->rate = rate;
this->hours = 0;
}
void Employee::setHours(double hours) {
this->hours = hours;
}
double Employee::getPay() const {
double gross = this->hours * this->rate;
double overtime = this->hours > 40 ?
(this->hours …
Run Code Online (Sandbox Code Playgroud) 我编写了一个名为Node的类,它表示图中的节点.它看起来像这样:
public class Node{
protected ArrayList<Node> neighbours = new ArrayList<>();
public Node(ArrayList<Node> neighbours){
for(int i=0;i<neighbours.size();i++){
this.neighbours.add(neighbours.get(i));
}
}
public Node(){}
public void setNeighbours(ArrayList<Node> neighbours){
this.neighbours.clear();
this.neighbours.addAll(neighbours);
}
public ArrayList<Node> getNeighbours(){
return this.neighbours;
}
@Override
public String toString(){
String s = new String(""+this.neighbours);
return s;
}
}
Run Code Online (Sandbox Code Playgroud)
我在覆盖toString
方法之前测试了它,创建了一个基本图形.输出是正确的,唯一的问题是输出是每个对象的地址,而不是对象本身(Node@61672c01
例如).在编写toString
方法之后,我开始收到诸如"源文件没有附件"和java.lang.StackOverflowError
错误等大量错误
我尝试更改项目的构建路径(我认为错误的构建类型是原因),但这没有帮助.我认为递归java.lang.StackOverFlowError
有问题,因为,但在编写toString()
方法之前没有任何问题.
Collection col=new ArrayList();
col.add("a");
col.add("b");
System.out.println(col);
Run Code Online (Sandbox Code Playgroud)
当包中toString()
没有覆盖方法时,java.util.collection
如何打印集合对象会打印其所有内容?
class Employee
{
String name ;
// Constructor
Employee(String name) {
this.name = name;
}
// override toString method in Employee class
@Override
public String toString() {
return name;
}
}
public class TestArraylistIterator {
public static void main(String[] args) {
Employee obj1 = new Employee("Java");
Employee obj2 = new Employee("Microsoft");
TestArraylistIterator obj3 = new TestArraylistIterator();
List ls = new ArrayList();
ls.add(obj1);
ls.add(obj2);
System.out.println("List object :: " + ls);
System.out.println("TestArraylistIterator :: " + obj3);
}
}
output :
List object …
Run Code Online (Sandbox Code Playgroud) 是否可以使用自定义格式的ToString()方法显示字符串?
例如,我有字符串:"123456789",我想显示为"123 456 789".
我试过这样的:
string myString = "123456789"
mystring = myString.ToString("{0:### ### ###}")
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
下面是map的一部分,其中map被初始化为:
Map<Integer,Integer> map = new HashMap<>();
和我想要修改输出的行是
System.out.println("Price and items "+map.toString());
目前的输出是
{100 = 10,200 = 5}
我要显示
{100:10,200:5}
我个人的偏好是x.ToString()
,因为它的字母数量略少.这两个都有什么优势吗?
我的输入是Array of Java Objects
:
[{"name"="Demo","platform"=[{"id"="1","value"="ios"},{"id"="2","value"="android"}],"language"=[{"id"="1","value"="eng"}],"date"="20/05/2018"}, {"name"="Kernel","platform"=[{"id"="1","value"="macos"},{"id"="2","value"="linux"}],"language"=[{"id"="1","value"="ger"}],"date"="20/05/2018"}]
Run Code Online (Sandbox Code Playgroud)
每个都Java Object
包含平台和语言键中的数组,如下例所示:
{"name"="Demo","platform"=[{"id"="1","value"="ios"},{"id"="2","value"="android"}],"language"=[{"id"="1","value"="eng"}],"date"="20/05/2018"}
Run Code Online (Sandbox Code Playgroud)
这是预期的输出text/plain
类型:
{"name":"Demo","platform":[{"id":"1","value":"ios"},{"id":"2","value":"android"}],"language":[{"id":"1","value":"eng"}],"date":"20/05/2018"}
{"name":"Kernel","platform":[{"id":"1","value":"macos"},{"id":"2","value":"linux"}],"language":[{"id":"1","value":"ger"}],"date":"20/05/2018"}
Run Code Online (Sandbox Code Playgroud)
是否可以应用它而不必玩字符串替换?
我正在为一个类学习 Java,我需要做的一项任务是在我的代码中实现 String 方法。在提示用户设置文本后,我使用了一个 toLowerCase() 方法并将其打印出来。在另一行中,我使用了 toUpperCase() 方法并打印了它。两者都打印正确,但每当我使用 toString() 方法时,它都只显示我的小写文本。
这是我的主要课程:
import java.util.Scanner;
public class TalkerTester
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter some text: ");
String words = input.nextLine();
Talker talky = new Talker(words);
String yelling = talky.yell();
String whispers = talky.whisper();
System.out.println(talky);
System.out.println("Yelling: " + yelling);
System.out.println("Whispering: " + whispers);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的课程和我所有的方法
public class Talker
{
private String text;
// Constructor
public Talker(String startingText)
{
text = startingText;
}
// Returns …
Run Code Online (Sandbox Code Playgroud)