I’m trying to make a program that displays your network information and I’m trying to make it look just a little nice. It’s CLI so far and here’s the code I’m having trouble with:
public class Computer {
String manu;
boolean power;
String powerStr;
String internalIP;
String router;
boolean connected;
int length;
String extIP;
String line;
String netInfo;
int netLength;
int space;
void setIntIP(String ip) {
this.internalIP = ip;
if(router == null) {
System.out.println("There is no router to connect to!");
this.internalIP = null;
}
}
void printInfo() {
if(power == true) {
powerStr = "on";
}
else
{
powerStr = "off";
}
System.out.println("The computer is "+powerStr+" and was made by "+manu);
}
void routingInfo() {
if(router == null) {
internalIP = "0.0.0.0";
router = "0.0.0.0";
}
if(internalIP == null) {
internalIP = "0.0.0.0";
router = "0.0.0.0";
}
if(router == "0.0.0.0") {
System.out.println("Not connected to the internet!");
} else {
netInfo = "+-------Network Information-----------+";
line = "| Internal IP | "+internalIP;
length = line.length();
netLength = netInfo.length();
space = netLength - length;
System.out.println("+-------------------------------------+");
}
}
}
I need it so that if there is 9 in space, it does 9 spaces.
How would I do this? Thanks! 😀
You can use StringBuilder:
As a side note (but an important one) – don’t compare strings using
==, but withequals/equalsIgnoreCase(). (router.equals("0.0.0.0")instead ofrouter == "0.0.0.0")