Switch case program - switch-statement

This is a Swtich Case program. I am not sure why this happens in this program. Can anyone explain why this happening?
class SwitchCase{
public static void main(String args[]){
switch(3*4+2){
case 12:
System.out.println("Hello");
case 14:
System.out.println("Hello World");
case 16:
System.out.println("Hello World Aqib");
case 18:
System.out.println("Hello World Mohd");
default:
System.out.println("Hahah....");
}
}
}
Output:
Hello World
Hello World Aqib
Hello World Mohd
Hahah....

After each case you need a break;
case 12:
System.out.println("Hello");
break;
.....

Related

How to make a Gpars Actor to read from console

I have a simple GPars actor:
class ConsoleActor extends DefaultActor {
protected void act() {
loop {
react { Msg msg ->
switch (msg.type) {
case MsgType.Read:
sender.send(System.console().readLine())
break
}
}
}
}
}
But when I try to send a message to force the actor to read from Console, i get a NPE:
An exception occurred in the Actor thread Actor Thread 3
java.lang.NullPointerException: Cannot invoke method readLine() on
null object
Why this happens and how to read from console in a GPars Actor?
I found the answer here:
StackOverflow - System Console returns null
class ConsoleActor extends DefaultActor {
protected void act() {
loop {
react { Msg msg ->
switch (msg.type) {
case MsgType.Read:
Scanner input = new Scanner(System.in);
String str = input.nextLine();
sender.send(str)
break
}
}
}
}
}
It works for me.

how to show same Dialog in Activity and Fragment in Different situation?

I am doing a sample Project where I am using Navigation Drawer with Fragments.When I click in row item1 it opens Dialog Fragment1 .In Dialog Fragment1 I have a button .
My requirement is I want to open same Dialog which is triggred from navigation row item1 when button in Fragment is clicked ...
I am using Following code to make Dialog in Activity
public void showRegisterDialog() {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog_register);
dialog.show();
}
and following code to open Dialog in Fragment
private void LoadFragmentView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 1:
fragment = new Fragment2();
showRegisterDialog();
break;
case 2:
fragment = new Fragment3();
break;
case 3:
fragment = new Fragment4();
break;
case 4:
fragment = new Fragment5();
break;
default:
break;
}
I need some guidelines,thank you..
I did some research and got the solution.
You need to use callback method in fragment.
For this purpose,you need to use the following codes:
your_button_on_fragment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
((YourActivityName)getActivity()).showRegisterDialog();
}
});

How to work with ids and strings in pagerTabStrip

Super newbie question about accessing information in strings. I have chunked together an app that uses a fragmentpager and the PagerTabStrip.
It is supposed to display titles on the tab, but mine displays nothing...because I am a total newbie hacking his way through. I am so grateful for this community.
From my layout file that calls the content (and once I fix it the title--id#pager_header right?):
<android.support.v4.view.PagerTabStrip
android:id="#+id/pager_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#ffffff" />
My code that gets me the swipeable pages of content:
public class Poems extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contentpage);
/** Getting a reference to the ViewPager defined the layout file */
ViewPager pager = (ViewPager) findViewById(R.id.pager);
/** Getting fragment manager */
FragmentManager fm = getSupportFragmentManager();
/** Instantiating FragmentPagerAdapter */
MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fm);
/** Setting the pagerAdapter to the pager object */
pager.setAdapter(pagerAdapter);
}
}
My code that should get me the titles from the string reference file.
public class MyFragment extends Fragment{
int mCurrentPage;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** Getting the arguments to the Bundle object */
Bundle data = getArguments();
/** Getting integer data of the key current_page from the bundle */
mCurrentPage = data.getInt("current_page", 0);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.myfragment_layout, container,false);
TextView tv = (TextView ) v.findViewById(R.id.tv);
tv.setMovementMethod(new ScrollingMovementMethod());
switch(mCurrentPage){
case 0:
tv.setText(" ");
break;
case 1:
tv.setText(R.string.content_1);
break;
case 2:
tv.setText(R.string.content_2);
break;
case 3:
tv.setText(R.string.content_3);
break;
}
return tv;
}
}
How do I get pager_header from this?
A sample relevant string is:
<string name="content_1">Welcome to this app!</string>
I am thinking that I need to better understand strings since I'm coming from more of a text content background.
How do I include in the stings.xml file the indicators that allow the app to reference the content associated with pageview so that I can have that show up in the tab? How I do make it so that the content shows "Welcome to this app" but the title in the tab shows "Home"?
I think as if each entry (page) is like a line in a database with one point of reference being the title, one being the content, one being a link or some other content. Each has its own id, right?
Thank you in advance for explaining this to a beginner.
You need to implement getPageTitle(int position) in MyFragmentPagerAdapter. The String that is returned from this method is used to set the title of the PagerTabStrip.
#Override
public CharSequence getPageTitle(int position) {
Log.v(TAG, "getPageTitle");
String tabTitle;
switch(position) {
case 0:
tabTitle = "Tab #0";
break;
case 1:
tabTitle = "Home";
break;
case 2:
tabTitle = "Tab #2";
break;
case 3:
tabTitle = "Tab #3";
break;
default:
tabTitle = "Default Tab Title";
break;
}
return tabTitle;
}
You can make this more dynamic by making the title and content arguments that you pass in using a Bundle by calling setArguments(Bundle args) on your MyFragments that you add to your ViewPager. In MyFragment in onCreateView() you can then set your content based on the passed in content String argument. Then in MyFragmentPagerAdapter you can get a reference to the Fragment at the position and pull get the title directly from MyFragment.

how to close a dialog box in lwuit? [duplicate]

I am developing one project using LWUIT, Midlet mobile Application. when I press number keys a dialog box will open. when i press the keys #,0,* Dialog should be close.
I am using Dialog.dispose() method to close dialog. But it is not working. Below is my Code. Can anyone tell me what is the problem in my code?
public class javaForm extends Component implements ActionListener
{
Dialog d=new Dialog();
public void keyPressed(int key){
System.out.println("Key pressed :"+key);
switch(key)
{
case 48:
d.show(130,20,30,30,true);
break;
case 49:
d.show(130,20,30,30,true);
break;
case 50:
d.show(130,20,30,30,true);
break;
case 51:
d.show(130,20,30,30,true);
break;
case 52:
d.show(130,20,30,30,true);
break;
case 53:
d.show(130,20,30,30,true);
break;
case 54:
d.show(130,20,30,30,true);
break;
case 55:
d.show(130,20,30,30,true);
break;
case 57:
d.show(130,20,30,30,true);
break;
case 56:
d.show(130,20,30,30,true);
break;
case 42:
d.dispose();
break;
case 35:
d.dispose();
break;
default:
d.dispose();
break;
}
}
public void actionPerformed(ActionEvent ae)
{
throw new UnsupportedOperationException("Not supported yet.");
}
}
Actually javaForm is a java Program developed using LWUIT and am calling this javaForm inside of MIDLET which is javaForm1. I Included all the Necessary packages.
Why don´t you use Form.addGamekeyListener()?
Put the gameKeyListener in yout Form (extends ActionListener in the Form)and later in the actionPerformed(ActionEvent ae) capture the key with ae.getKeyEvent and close the Dialog.
Map the GameKeys with Canvas. For example: Canvas.FIRE.

How can I Dispose Dialog box From LWUIT Component

I am developing one project using LWUIT, Midlet mobile Application. when I press number keys a dialog box will open. when i press the keys #,0,* Dialog should be close.
I am using Dialog.dispose() method to close dialog. But it is not working. Below is my Code. Can anyone tell me what is the problem in my code?
public class javaForm extends Component implements ActionListener
{
Dialog d=new Dialog();
public void keyPressed(int key){
System.out.println("Key pressed :"+key);
switch(key)
{
case 48:
d.show(130,20,30,30,true);
break;
case 49:
d.show(130,20,30,30,true);
break;
case 50:
d.show(130,20,30,30,true);
break;
case 51:
d.show(130,20,30,30,true);
break;
case 52:
d.show(130,20,30,30,true);
break;
case 53:
d.show(130,20,30,30,true);
break;
case 54:
d.show(130,20,30,30,true);
break;
case 55:
d.show(130,20,30,30,true);
break;
case 57:
d.show(130,20,30,30,true);
break;
case 56:
d.show(130,20,30,30,true);
break;
case 42:
d.dispose();
break;
case 35:
d.dispose();
break;
default:
d.dispose();
break;
}
}
public void actionPerformed(ActionEvent ae)
{
throw new UnsupportedOperationException("Not supported yet.");
}
}
Actually javaForm is a java Program developed using LWUIT and am calling this javaForm inside of MIDLET which is javaForm1. I Included all the Necessary packages.
Why don´t you use Form.addGamekeyListener()?
Put the gameKeyListener in yout Form (extends ActionListener in the Form)and later in the actionPerformed(ActionEvent ae) capture the key with ae.getKeyEvent and close the Dialog.
Map the GameKeys with Canvas. For example: Canvas.FIRE.

Resources