What does it mean by object? in 2.1 PPC the same code works perfectly. I very often just use JOptionPanes for lone ints. I think i'll just get used to the long way.
What does it mean by object? in 2.1 PPC the same code works perfectly. I very often just use JOptionPanes for lone ints. I think i'll just get used to the long way.
robbieduncan said:If you don't know the difference between a primitive type and an Object the you are in real trouble. A primitive type cannot have methods called on it, cannot be subclassed... It's not an object. If a method calls for an Object then passing an int should not work.
Note that Java 1.5 has introduced a new language feature to save the lazy like yourself called Autoboxing which will automatically insert the code to turn an int into an Integer in situations like this. Personally I think this is a bad idea as it leads to a lack of understanding and clarity about what an Object is.
If you installed Java 1.5 on your PPC machine and were using that but have not installed it on your Intel machine this would explain why it works on one machine but not the other. In an exam situation you should do it the correct way (which is not to rely on autoboxing).
Grade 10? Like School, not University? I'm from Scotland so I don't understand how old that makes you but the lack of understanding as to what is going on makes a lot more sense. I assumed you were taking an intro to programming style class at Uni or college...
Does it work if you change the code from:
JOptionPane.showMessageDialog(null,i);
to:
JOptionPane.showMessageDialog( null, new Integer(i) );
Yes! That works, is there any reasoning behind that or is it just the way it is?
Sorry, I just saw this thread or I would have posted earlier.Yes! That works, is there any reasoning behind that or is it just the way it is?
String q=JOptionPane.showInputDialog(null,"choose your lines");
int a=Integer.parseInt(q);
for (int i = 1; i<=a; i++)
{
for (int j = 1; j<=i; j++)
{
System.out.print(i);
}
System.out.println();
}
Sounds like you did well. Congratulations. The code for the question you gave looks correct to me.Damn, you guys are so smart. the exam was simple except this question:
Write a programme that will allow a user to type the no. of rows to display. The following output will be given by only using for loops.
Amount of rows to display?
User entered:5
Output:
1
22
333
4444
55555
I know the basic structure is a nested for loop but the rest leaves me blank!
here's what I did:
Code:String q=JOptionPane.showInputDialog(null,"choose your lines"); int a=Integer.parseInt(q); for (int i = 1; i<=a; i++) { for (int j = 1; j<=i; j++) { System.out.print(i); } System.out.println(); }
Damn, you guys are so smart. the exam was simple except this question:
Write a programme that will allow a user to type the no. of rows to display. The following output will be given by only using for loops.
Amount of rows to display?
User entered:5
Output:
1
22
333
4444
55555
I know the basic structure is a nested for loop but the rest leaves me blank!
here's what I did:
Code:String q=JOptionPane.showInputDialog(null,"choose your lines"); int a=Integer.parseInt(q); for (int i = 1; i<=a; i++) { for (int j = 1; j<=i; j++) { System.out.print(i); } System.out.println(); }