Change between numbers system - arraylist

I try to change big numbers between number systems. Bin to Dec, from Dec to Oct, etc. I try to do it on int/long but there is to small range.
public static String naSystem(String liczba,int system){
int dec = 0;
for (int a=0, b=liczba.length()-1; a<liczba.length(); a++, b--) {
String n = liczba.substring(a, a+1);
Pattern pattern = Pattern.compile("\\d");
Matcher matcher = pattern.matcher(n);
if (!matcher.matches()) {
dec += (((int)liczba.charAt(a) - 55) * Math.pow(system, b));
} else {
dec += (Integer.parseInt(n) * Math.pow(system, b));
}
}
return String.valueOf(dec);
}
I think the only way is to use ArrayList but I do not know how to do it

Related

Roman Numerals. Could you point out my mistakes further down the road?

Beginner here. This piece of code converts number into roman numerals in multiples of 50 if not 10 if not 9 and down to 0. Methods are so intertwined. Is there something (just at a glance) you could suggest I should avoid doing? Thank You.
public static void main(String[] args) {
System.out.println(fiftyAndAbove(37));
}
public static String nineAndDown(int number) {
String one = "I", five = "V", ten = "X", sum = "";
if(number == 5) {
return five;
} else if(number == 9) {
return one + ten;
}
else if(number > 5) {
for(int i=1; i<=number-5; i++) {
sum += one;
}
return five + sum;
} else {
if(number == 4 ) {
return one + five;
} else
for(int i=1; i <=number; i++) {
sum += one;
}
} return sum;
}
public static String tenAndAbove(int number) {
int remainder = number % 10, numberOftens = number/10;
String ten = "X", sum = "";
if(numberOftens > 0) {
while(numberOftens > 0) {
sum += ten;
numberOftens -= 1;
}
}
return sum + nineAndDown(remainder);
}
public static String fiftyAndAbove(int number) {
int remainder = number % 50, numberOfFifty = number/50;
String fifty = "L", sum = "";
if(numberOfFifty > 0) {
while(numberOfFifty > 0) {
sum += fifty;
numberOfFifty -= 1;
}
}
return sum + tenAndAbove(remainder);
}
Is there something (just at a glance) you could suggest I should avoid doing?
I'd not unnecessarily complicate the logic as with
if(numberOfFifty > 0) {
while(numberOfFifty > 0) {
…
}
}
which is equivalent to
while (numberOfFifty > 0)
{
…
}
You could also have a look at this implementation and see what you prefer:
import java.util.Arrays;
…
public static String fiftyAndAbove(int number)
{
int remainder = number%50, numberOfFifty = number/50;
char [] Ls = new char [numberOfFifty];
Arrays.fill(Ls, 'L');
return new String(Ls) + tenAndAbove(remainder);
}
You have four places like this in your program where you need a string of a character repeated. If you're willing to require a certain Java version or above, you can also use one of the methods described at Java: String - add character n-times; otherwise I'd suggest to use a function to do it.
You could also think about whether you find
String one = "I", five = "V", ten = "X", sum = "";
if(number == 5) {
return five;
} else if(number == 9) {
return one + ten;
}
really better than
if (number == 5) return "V";
if (number == 9) return "IX";

Shorten the process of finding the sum of positive number (users input 6 different intteger)

I nedd to find the fatest and shortest way to calculate the sum of positive integer that the user input in.
else if(num1<0 && num2 >0 && num3>0 && num4>0 && num5>0 &&num6>0){
totalPositiveNumber =num2 + num3 + num4 + num5 + num6;
System.out.println("The sum of positive integer is: " + totalPositiveNumber);
}
You can read the numbers and store them in an array. Later iterate over the array and sum only, when number is greater than 0. Check below for sample code for reading input from System.in using Scanner class.
import java.util.Scanner;
public class Sum {
public static void main(String args[]) {
Scanner read = new Scanner(System.in);
int array[] = new int[6];
int sum = 0;
for (int i = 0; i<6; i++) {
array[i] = read.nextInt();
}
for (int i = 0; i<6; i++) {
if (array[i] > 0) {
sum = sum + array[i];
}
}
System.out.println(sum);
}
}

How to print out the digits of an integer of any length?

This program is works as long as the divide variable is of the same base 10 power as the variable num, in this case the number is 12345 so divide needs to be 10000. While this works for 5 digit numbers, anything with more or less than 5 digits will not have their individual digits printed out. How do I configure divide to have be of the same base 10 power as num automatically?
public class lab5testing
{
public static void main (String args[])
{
int num = 12345, digit = 0, divide = 10000;
if (num != 0)
{
while(num != 0 )
{
digit = ((num/divide)%10);
System.out.println(digit);
divide /= 10;
if (divide == 0)
{
num = 0;
}
}
}
else
{
System.out.println(num);
}
}
}
Maybe you should try with this :
int length = (int)(Math.log10(num)+1);
and then :
int divide = Math.pow(10,lengh);

Getting a Date and description associated with a file revision - Java API Perforce

I am trying to write a method which takes a file path and a revision number as its arguments and returns the date the revision is associated with. The code I have works (although pretty slowely) however, when I put a revision number greater than 51 in, the output gets messed up.. Here is the API.
Input
String [] filePaths= {"//file/x/y/strings/somefile.csv"};
p4Client.getDateAssociatedWithFileRevision(filePaths, 52);
Output - This should just be one line...
Rev number: 2 :: Revision Date: Wed Aug 24 23:48:42 BST 2005
Rev number: 52 :: Revision Date: Wed Aug 24 23:52:53 BST 2005
Rev number: 51 :: Revision Date: Sat Aug 20 02:01:59 BST 2005
getDateAssociatedWithFileRevision
public Date getDateAssociatedWithFileRevision(String [] filePath, int revisionNumber) {
List<IFileSpec> fileList = null;
Map<IFileSpec,List<IFileRevisionData>> fileRevisionData = null;
String currentFile = null;
Date revisionDate = null;
try
{
String file = filePath[0] + "#" + revisionNumber;
currentFile = file;
fileList = getIFileSpecList(file); //Get list of file(s) in path
for(IFileSpec fileSpec: fileList)
{
if(file.toString() == null)
{
System.out.println("\"" + currentFile +"\"" + " does not exist...");
break;
}
fileRevisionData = fileSpec.getRevisionHistory(0, true,false,true,false);
int i = 0;
for(List<IFileRevisionData> revisionData : fileRevisionData.values()) {
revisionDate = revisionData.get(0).getDate();
int revision = revisionData.get(0).getRevision();
System.out.println("Rev number: " +revision +" :: " + "Revision Date: " + revisionDate);
System.out.println(i);
i++;
}
}
}
catch(Exception e){e.printStackTrace();}
return revisionDate;
}
GetIFileSpecList
public List<IFileSpec> getIFileSpecList(String file) {
List<IFileSpec> fileList = null;
try {
fileList = iServer.getDepotFiles(
FileSpecBuilder.makeFileSpecList(new String[] {file}), false); //Get list of file(s) in path
}
catch(Exception e){e.printStackTrace();}
return fileList;
}
Edit
Just figured out that the output is getting messed up after an integration, just need to find a way to handle them now..
Too much work. Take this command-line idea ("p4 files will get you that info, parsed") and make Perforce do the joins of the data for you. Then Java-ize it.
% p4 -Ztag files //guest/jeff_bowles/scripts/0228devbranch.html
... depotFile //guest/jeff_bowles/scripts/0228devbranch.html
... rev 2
... change 4421
... action edit
... type ktext
... time 1093044566
% p4 -Ztag files //guest/jeff_bowles/scripts/0228devbranch.html#1
... depotFile //guest/jeff_bowles/scripts/0228devbranch.html
... rev 1
... change 4420
... action add
... type ktext
... time 1093042787
I managed to return only the date I want by adding an if statement (marked below). I don't know how elegant this solution is... any comments are welcome.
getDateAssociatedWithFileRevision
public Date getDateAssociatedWithFileRevision(String [] filePath, int revisionNumber) {
List<IFileSpec> fileList = null;
Map<IFileSpec,List<IFileRevisionData>> fileRevisionData = null;
String currentFile = null;
Date revisionDate = null;
try
{
String file = filePath[0] + "#" + revisionNumber;
currentFile = file;
fileList = getIFileSpecList(file); //Get list of file(s) in path
for(IFileSpec fileSpec: fileList)
{
if(file.toString() == null)
{
System.out.println("\"" + currentFile +"\"" + " does not exist...");
break;
}
fileRevisionData = fileSpec.getRevisionHistory(0, true,false,true,false);
for(List<IFileRevisionData> revisionData : fileRevisionData.values()) {
int revision = revisionData.get(0).getRevision();
-------------> if(revision.equals(revisionNumber))
{
revisionDate = revisionData.get(0).getDate();
System.out.println("Rev number: " +revision +" :: " + "Revision Date: " + revisionDate);
break;
}
}
}
}
catch(Exception e){e.printStackTrace();}
return revisionDate;
}

How do i create a calculated measure that will filter data by days overdue

I have a field in my fact table called days overdue. I would like to create a set that will do the following: If the days due is between 0 - 29, then 0 - 29 days overdue, if between 30 and 59 days old, then '30 - 59 days overdue. How would i create this?
We need to know what kind of array you're using, or linked list, or my favorite for these things, a vector, etc.
If you were using a vector, you would create your own class to be used as a datatype with things like:
Class MyData
{
String name;
int daysPastDue; // how you want to factor this is up to you,
// i suggest looking into Java.util.date or Java.util.calendar
public MyData
{
name = "";
daysPastDue = 0;
}
}
Class DoWork
{
public void myWork() // excuse the indent, forgot to put in the class name
{
vector <MyData> input;
MyData 0To29 [] = new MyData[input.size()];
MyData 33To59 [] = new MyData[input.size()];
MyData item = new MyData();
int 0To29count = 0;
int 30To59count = 0;
for (i = 0; i <= list.size(); i++)
{
item = input.elementAt(i)
if (item.daysPastDue <= 29)
{
0To29[0To29Count] = input;
0To29Count ++;
}
elseif (item.daysPastDue >= 30 && item.daysPastDue <= 59)
{
30To59[30To59Count] = input;
30To59Count ++;
}
}
}
}
then you have your 2 arrays and can output them as you wish. however i would recommend starting at daysPastDue = 100000 and decrement it and check the number through the vector until you have all the items in the vector listed. That way they're all in order from the most past due, to the least and you get the output of exactly how long they've been past due.