Posted By

DrPepper on 01/24/11


Tagged


Versions (?)

[JAVA] converter


 / Published in: Java
 

this programming convert number and key word to a sentence. however, it still has a problem about if statement, when i add else after the if statement every time it executes so every result became in the else statement one. [UPDATED] Now it runs, I added another huge contents of if statement.

  1. /**
  2.   write a programming that takes user input describing a playing card in the followint shorthand notaiton (see text p221)
  3.   Your program should print the full description of the card.
  4.   Implement a class card whose constructor takes the card notation string and whose getDescription method returns a description of the card. If the notation string is not in the correct format, the getDescription method should return the string "Unknown"
  5. */
  6.  
  7.  
  8. import java.util.Scanner;
  9. public class card
  10. {
  11. public String notation;
  12. //public int key;
  13.  
  14. /**
  15.   this function is input number and return int value
  16.   */
  17. public String getScription()
  18. {
  19. System.out.println("Enter a key word : ");
  20. Scanner in = new Scanner(System.in);
  21. notation = in.next();
  22. return notation;
  23. }
  24.  
  25. /**
  26.   this function has an error
  27.   seems like confuse to return value
  28.   this function supposed to take a string and return the string to main function
  29.   */
  30. public int getScriptionS()
  31. {
  32.  
  33. System.out.println("Enter an number");
  34. Scanner in = new Scanner(System.in);
  35. int key = in.nextInt();
  36. return key;
  37.  
  38. }
  39.  
  40. public static String getNotation(String inputData)
  41. {
  42. /**
  43.   A : Ace, 2 to 10 :card value
  44.   J : Jack, Q : Queen
  45.   K : King, D : Diamond
  46.   H : Heart, S : Spades
  47.   C : Clubs
  48.   */
  49. String mean = new String();
  50. if( inputData.equals("A") ||
  51. inputData.equals("J") ||
  52. inputData.equals("Q") ||
  53. inputData.equals("K") ||
  54. inputData.equals("D") ||
  55. inputData.equals("H") ||
  56. inputData.equals("S") ||
  57. inputData.equals("C"))
  58. {
  59. if (inputData.equals("A"))
  60. mean = " of Ace";
  61. if (inputData.equals("J"))
  62. mean = " of Jack";
  63. if (inputData.equals("Q"))
  64. mean = " of Queen";
  65. if (inputData.equals("K"))
  66. mean = " of King";
  67. if (inputData.equals("D"))
  68. mean = " of Dianomd";
  69. if (inputData.equals("H"))
  70. mean = " of Hearts";
  71. if (inputData.equals("S"))
  72. mean = " of Spades";
  73. if (inputData.equals("C"))
  74. mean = " of Clubs";
  75. }
  76. else
  77. {mean = "Unknown";}
  78. System.out.println(inputData);
  79. System.out.println(mean);
  80. return mean;
  81. }
  82.  
  83. public static String getNotation2(int inputData2)
  84. {
  85. String data4 = new String();
  86. if((inputData2 <= 13) || (inputData2 >= 1))
  87. {
  88. if(inputData2 == 1)
  89. {data4 = "One";}
  90. if(inputData2 == 2)
  91. {data4 = "Two";}
  92. if(inputData2 == 3)
  93. {data4 = "Three";}
  94. if(inputData2 == 4)
  95. {data4 = "Four";}
  96. if(inputData2 == 5)
  97. {data4 = "Five";}
  98. if(inputData2 == 6)
  99. {data4 = "Six";}
  100. if(inputData2 == 7)
  101. {data4 = "Seven";}
  102. if(inputData2 == 8)
  103. {data4 = "Eight";}
  104. if(inputData2 == 9)
  105. {data4 = "Nine";}
  106. if(inputData2 == 10)
  107. {data4 = "Ten";}
  108. if(inputData2 == 11)
  109. {data4 = "Eleven";}
  110. if(inputData2 == 12)
  111. {data4 = "Twelve";}
  112. if(inputData2 == 13)
  113. {data4 = "Thrteen";}
  114. }
  115. else
  116. {data4 = "Unknown";}
  117. System.out.println(inputData2);
  118. System.out.println(data4);
  119. return data4;
  120. }
  121. public static void main(String [] args)
  122. {
  123. card userInput = new card();
  124. String input = new String();
  125. String cardName = new String();
  126. int data2;
  127. String data3 = new String();
  128. input = userInput.getScription(); // String
  129. data2 = userInput.getScriptionS(); // int
  130. cardName = getNotation(input); // String
  131. data3 = getNotation2(data2); // int
  132. System.out.print(data3); //int
  133. System.out.println(cardName); // String
  134.  
  135.  
  136. }
  137. }

Report this snippet  

You need to login to post a comment.