Creating a Repeated String

String s = "abc";
String repeated = new String(new char[3]).replace("\0", s);

result in repeated containing:

abcabcabc

This is sort-of a low-level predecessor to printing repeats using C# LINQ.

Switch on String

Compared to C#, Java does not allow to switch on string. However, this can still be done by wrapping the strings in an enum:

public class Switch {
  public static enum Days {
    Monday, 
    Tuesday, 
    Wednesday, 
    Thursday, 
    Friday,
    Unknown
 
  };
  public static void main(String [] args) {
    Days Day = Day.Monday;
    switch (Day) {
      case Monday:
      case Tuesday:
      case Wednesday:
      case Thursday:
      case Friday:
        System.out.println("Weekdays");
        break;
      default:
        System.out.println("Unknown day");
        break;
    }
  }
}

Get All IP Addresses

import java.io.*;
import java.net.*;
import java.util.*;
import static java.lang.System.out;
 
public class ListAddrs {
    public static void main(String args[]) throws SocketException {
        InetAddress localhost = InetAddress.getLocalHost();
        InetAddress[] allMyIps = InetAddress.getAllByName(localhost.getCanonicalHostName());
        for (int i = 0; i < allMyIps.length; i++)
            out.printf("InetAddress: %s\n", allMyIps[i]);
    }
}

Iterative Fibonacci Implementation

///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3    //
///////////////////////////////////////////////////////////////////////////
public class Fibonacci {
    public static void main(String[] args) {
        int f0 = 0;
        int f1 = 1;
 
        int i = 0;
        while(i < 10) {
            int s = f0;
            f0 = f1;
            f1 = s + f1;
 
            System.out.println(s);
 
            i = i + 1;
        }
    }
}

fuss/java.txt ยท Last modified: 2017/02/22 18:30 (external edit)

Access website using Tor Access website using i2p


For the copyright, license, warranty and privacy terms for the usage of this website please see the license, privacy and plagiarism pages.