So, I have this tiny problem. I'm prompting the user to input 3 variables (Str, Str, Int) that need to be stored in a multi variable array and I can't get it to work. Any help will be appreciated.
LibraryBook[] book = new LibraryBook[5];
//inputing a new book
Scanner input = new Scanner(System.in);
LibraryBook[] myBook = new LibraryBook[0];
System.out.println("Enter book name: ");
String title = input.nextLine().trim();
System.out.println("Enter author name: ");
String author = input.nextLine().trim();
System.out.println("Enter # pages: ");
int pages = input.nextInt();
myBook[0] =new LibraryBook(title,author,pages);
I get this error
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at LibraryBookSort.main(LibraryBookSort.java:36)
----jGRASP wedge2: exit code for process is 1.
delete this line
LibraryBook[] myBook = new LibraryBook[0];
and replace last line
myBook[0] =new LibraryBook(title,author,pages);
by this
book[0] =new LibraryBook(title,author,pages);
You're initializing your array with zero size, so you basically don't have any space to store a variable (a class in your case). Here's the correction:
LibraryBook[] myBook = new LibraryBook[10];
I'm assuming you will need no more than 10 locations in your program.
Edit: I just noticed you have two LibraryBook arrays declared, but you're only using one. Is there a necessity for the unused one?
Related
Creating a measure distanced annotation does not display the correct measurement unit, nor gives the correct calculation. Any idea which part I am doing wrong? or if there is some lacking data.
I am currently creating a measure distance annotation. Using pixel, it works just fine. but since now i am taking into account actual measurement and unit, i am not sure which part of code i am having problem with or if i am lacking something.
Please see image. That is some application i use to create a distance annotation and i calibrate it that that distance is 14.5cm so by dividing it by pixel, the calibration value per pixel would be 0.0519548.
Now, when I apply it to iText code, i am confused why the display is always still in inches? Even if i set my code to be inches and not cm, the calculation is incorrect.
I am not entirely sure what the problem is.
public class Test {
public static void main(String[] args) throws Exception {
PdfReader reader = new PdfReader("src.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("result.pdf"));
Rectangle location = new Rectangle(55.78125f, 493.875f, 253.59375f, 562.3125f);
PdfDictionary dict = new PdfDictionary();
PdfArray lineEndings = new PdfArray();
lineEndings.add(new PdfName("OpenArrow"));
lineEndings.add(new PdfName("OpenArrow"));
PdfAnnotation stamp = PdfAnnotation.createLine(stamper.getWriter(), location, "test measurement", 55.78125f, 562.3125f, 253.59375f, 493.875f);
stamp.put(new PdfName("LE"), lineEndings);
stamp.put(PdfName.ROTATE, new PdfNumber(0));
stamp.put(PdfName.MEASURE, createMeasureDictionary());
stamp.put(new PdfName("IT"), new PdfName("LineDimension"));
stamp.put(new PdfName("Cap"), new PdfBoolean(true));
stamp.put(PdfName.F, new PdfNumber(516));
stamp.setColor(PdfGraphics2D.prepareColor(Color.RED));
stamper.addAnnotation(stamp, 1);
stamper.close();
reader.close();
}
private static PdfDictionary createMeasureDictionary() {
PdfDictionary measureDictionary = new PdfDictionary(PdfName.MEASURE);
measureDictionary.put(PdfName.R, new PdfString("1 cm = 1 cm"));
PdfDictionary xDictionary = new PdfDictionary(PdfName.NUMBERFORMAT);
xDictionary.put(PdfName.U, new PdfString("cm"));
xDictionary.put(PdfName.C, new PdfNumber(0.0519548f));
measureDictionary.put(PdfName.X, new PdfArray(xDictionary));
PdfDictionary dDictionary = new PdfDictionary(PdfName.NUMBERFORMAT);
dDictionary.put(PdfName.U, new PdfString("cm"));
dDictionary.put(PdfName.C, new PdfNumber(1.0f));
measureDictionary.put(PdfName.D, new PdfArray(dDictionary));
PdfDictionary aDictionary = new PdfDictionary(PdfName.NUMBERFORMAT);
aDictionary.put(PdfName.U, new PdfString("cm"));
aDictionary.put(PdfName.C, new PdfNumber(1.0f));
measureDictionary.put(PdfName.A, new PdfArray(aDictionary));
return measureDictionary;
}
}
#mkl im tagging you in case you have free time to check and guide. thank you.
I do not fully follow your math.
Your line has a length of ca. 209.3 user space units. If you want that line to represent 14.5 cm, the X Conversion factor should be about 0.069273 and not your 0.0519548.
Setting
xDictionary.put(PdfName.C, new PdfNumber(0.069273f));
I get a PDF which upon opening shows
and after a slightest move (after which Adobe Reader rebuilds the appearance)
so no inches...
VBscript:
arytwoDim(1,intLastBucketBlockIndex) = "08:30 AM" 'TIME
arytwoDim(2,intLastBucketBlockIndex) = 5 'Resource QTY
I would like to create a List collection equivalent to vb array, so that I can mirror the logic with hard coded row,cols.
I tried loading list like this:
List<List<String>> arytwoDim = new List<List<String>>(); //Creates new nested List
arytwoDim .Add(new List<String>()); //Adds new sub List
arytwoDim [1][intLastBucketBlockIndex] = "08:30 AM"; //TIME
arytwoDim [2][intLastBucketBlockIndex] = 5; //Resource QTY
I received this error: 'Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index'
I confirmed that value intLastBucketBlockIndex contains is 0.
Please advise, thank you in advance for your help!
I have a file to put in a multidimensional array. I have to put to [0] a date (long) and one of the dimensions must be incremented depending on the value of the second token.
Here's the code :
BufferedReader bufStatsFile = new BufferedReader(new FileReader(statsFile));
String line = null;
List<Long[]> stats = new ArrayList<Long[]>();
stats.add(new Long[11]);
int i = 0; // will be in a loop later
while((line = bufStatsFile.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line,";");
while(st.hasMoreTokens()) {
stats.get(i)[0] = Long.parseLong(st.nextToken());
stats.get(i)[Integer.parseInt(st.nextToken())]++; // Here is the problematic line.
}
}
bufStatsFile.close();
But the incrementation doesn't work. Maybe it is because of my array which is probably not correct, but I didn't found another proper way to do that.
Ok. I have found and it was, of course, stupid.
The problem was in my array declaration. I did it like that :
List<Long[]> stats = new ArrayList<Long[]>();
stats.add(new Long[11]);
And then, I tried to increment an Object and not a long number.
So now, I just do it like this :
List<long[]> stats = new ArrayList<>();
stats.add(new long[11]);
And it's perfectly working.
Check that the elements in your file are numbers from 0 to 10. Why are you having a List if you are only manipulating the row 0?
Which exception are your code throwing away?
Could someone explain why the FLEX 4.5 XMLDecoder does this to my XML-data?
var decoder:XMLDecoder = new XMLDecoder;
var $object:Object = decoder.decode( <xmltag>08.00</xmltag> );
// object = "08.00"
var decoder:XMLDecoder = new XMLDecoder;
var $object:Object = decoder.decode( <xmltag>11.00</xmltag> );
// Object = "11" (HEY! Where did my '.00' part of the string go?)
var decoder:XMLDecoder = new XMLDecoder;
var $object:Object = decoder.decode( <xmltag>11.30</xmltag> );
// Object = "11.3" (HEY! Where did my '0' part of the string go?)
The Flex deserializer also gave me issues with this. It may be interpreting them as Number objects and thus they will return short representations when toString() is called.
Try using .toFixed(2) whenever you need to print a value such as 11.00
var $object:Object = decoder.decode( <xmltag>11.00</xmltag> );
trace($object); //11
trace($object.toFixed(2)); //11.00
So, to the answer the original question of why this is happening:
In the source code for SimpleXMLDecoder (which I'm guessing has similar functionality to XMLDecoder), there's a comment in the function simpleType():
//return the value as a string, a boolean or a number.
//numbers that start with 0 are left as strings
//bForceObject removed since we'll take care of converting to a String or Number object later
numbers that start with 0 are left as strings - I guess they thought of phone numbers but not decimals.
Also, because of some hacky implicit casting, you actually have three different types -
"0.800" : String
11 : int
11.3: Number
In the big picture I want to create a frame based application in Bada that has a single UI control - a label. So far so good, but I want it to display a number of my choosing and decrement it repeatedly every X seconds. The threading is fine (I think), but I can't pass the label pointer as a class variable.
//MyTask.h
//...
result Construct(Label* pLabel, int seconds);
//...
Label* pLabel;
//MyTask.cpp
//...
result
MyTask::Construct(Label* pLabel, int seconds) {
result r = E_SUCCESS;
r = Thread::Construct(THREAD_TYPE_EVENT_DRIVEN);
AppLog("I'm in da constructor");
this->pLabel = pLabel;
this->seconds = seconds;
return r;
}
//...
bool
Threading::OnAppInitializing(AppRegistry& appRegistry)
{
// ...
Label* pLabel = new Label();
pLabel = static_cast<Label*>(pForm->GetControl(L"IDC_LABEL1"));
MyTask* task = new MyTask();
task->Construct(&pLabel); // HERE IS THE ERROR no matching for Label**
task->Start();
// ...
}
The problem is that I have tried every possible combination of *, &, and just plain pLabel, known in Combinatorics...
It is not extremely important that I get this (it is just for training) but I am dying to understand how to solve the problem.
Have you tried:
task->Construct(pLabel, 0);
And by that I want to point out that you are missing the second parameter for MyTask::Construct.
No, I haven't. I don't know of a second parameter. But this problem is solved. If I declare a variable Object* __pVar, then the constructor should be Init(Object* pVar), and if I want to initialize an instance variable I should write
Object* pVar = new Object();
MyClass* mClass = new MyClass();
mClass->Construct(pVar);