I've created a random word generator and it seems to work pretty well except for the fact that before the desired output, in the same line, it prints "null".
Here's my code:
import java.util.Random;
public class wordGenerator {
private static String r,s;
public static void randChar(int x) {
Random rand = new Random();
x = rand.nextInt((26 - 1) + 1) + 1;
switch(x) {
case 1: r = "a"; break;
case 2: r = "b"; break;
case 3: r = "c"; break;
case 4: r = "d"; break;
case 5: r = "e"; break;
case 6: r = "f"; break;
case 7: r = "g"; break;
case 8: r = "h"; break;
case 9: r = "i"; break;
case 10: r = "j"; break;
case 11: r = "k"; break;
case 12: r = "l"; break;
case 13: r = "m"; break;
case 14: r = "n"; break;
case 15: r = "o"; break;
case 16: r = "p"; break;
case 17: r = "q"; break;
case 18: r = "r"; break;
case 19: r = "s"; break;
case 20: r = "t"; break;
case 21: r = "u"; break;
case 22: r = "v"; break;
case 23: r = "w"; break;
case 24: r = "x"; break;
case 25: r = "y"; break;
case 26: r = "z"; break;
default: r = "|null|";
}
if(x != 1 && x != 5 && x != 9 && x != 15 && x != 21) {
int h = rand.nextInt(4 - 0);
if(h == 2) {
int k = rand.nextInt(6 - 0);
switch(k) {
case 1: r = "a"; break;
case 2: r = "e"; break;
case 3: r = "i"; break;
case 4: r = "o"; break;
case 5: r = "u"; break;
}
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Random rand = new Random();
int y = rand.nextInt((10 - 4) + 1) + 4;
for(int z = 1; z < y; z++) {
randChar(0);
s = s + r;
}
System.out.println(s);
}
To me it seems fine, but when I think it should output something like
opfruvme
it prints
nullopfruvme
What's the problem?
You didn't initialize s.
private static String r,s="";
When you start your application in the loop the statement s = s + r; takes null value before concatenating with r in the first cycle. Initializing variable with empty string will give you result that you have been expected.
You didn't initialize s, so then the first time the s = s + r; line executes the current value of s (null) will be coerced to a String, so you will assign "null" + r to s.
Related
The assignment for the project that I am working on is for the user to be able to input a zip code and for the program to provide the bar code in the output. For example, the bar code for 95014 right now should be "|:|:::|:|:||::::::||:|::|". This is all based on a table I have that denotes what each number should be (shown in the case statements). This is the code I wrote for that portion:
public class Zipcode{
private String zipcode = "";
private String barcode = "";
private int zipnum = 0;
public Zipcode(int zip_number){
zipnum = zip_number;
}
public void createBarcode(){
while (zipnum > 0){
switch (zipnum % 10)
{
case 0:
barcode = "||:::";
break;
case 1:
barcode = ":::||";
break;
case 2:
barcode = "::|:|";
break;
case 3:
barcode = "::||:";
break;
case 4:
barcode = ":|::|";
break;
case 5:
barcode = ":|:|:";
break;
case 6:
barcode = ":||::";
break;
case 7:
barcode = "|:::|";
break;
case 8:
barcode = "|::|:";
break;
case 9:
barcode = "|:|::";
break;
default:
break;
}
barcode += barcode;
zipnum = zipnum / 10;
}
}
public String getBarcode(){
return barcode;
}
}
However, when my tester class calls this class after the user inputs a zip code, nothing comes up as the result. Please help! Why isn't anything being returned? I don't know what I am doing wrong.
Notice that you re-assign barcode variable in each switch-case block and append itself again in the end.
You can do it with a temporary variable inside the method:
public void createBarcode(){
String tempBarcode = "";
...
case 0:
tempBarcode = "||:::";
break;
....
barcode += tempBarcode;
But I think the cleanest solution would be to use it as an utility method without any class variable dependencies:
public static String calculateBarcode(int zipNumber) {
StringBuilder barcode = new StringBuilder();
while (zipNumber > 0) {
switch (zipNumber % 10) {
case 0:
barcode.append("||:::");
break;
case 1:
barcode.append(":::||");
break;
...
...
default:
break;
}
zipNumber = zipNumber / 10;
}
return barcode.toString();
}
Simple testcase:
public static void main(String[] args) {
System.out.println(ZipcodeUtil.calculateBarcode(95014));
/* Output: :|::|:::||||::::|:|:|:|:: */
}
You aren't appending the zip value on every iteration correctly. The value is getting lost as you are overriding the value here on this step
...
case 0:
barcode = "||:::";
break;
...
Instead use a temporary variable to store the current iterations zip code value
public class Zipcode {
private String zipcode = "";
private String barcode = "";
private int zipnum = 0;
public Zipcode(int zip_number) {
zipnum = zip_number;
}
public void createBarcode() {
String temp_barcode = "";
while (zipnum > 0) {
System.out.println(zipnum % 10);
switch (zipnum % 10) {
case 0:
temp_barcode = "||:::";
System.out.println(0);
break;
case 1:
temp_barcode = ":::||";
System.out.println(1);
break;
case 2:
temp_barcode = "::|:|";
System.out.println(2);
break;
case 3:
temp_barcode = "::||:";
System.out.println(3);
break;
case 4:
temp_barcode = ":|::|";
System.out.println(4);
break;
case 5:
temp_barcode = ":|:|:";
System.out.println(5);
break;
case 6:
temp_barcode = ":||::";
System.out.println(6);
break;
case 7:
temp_barcode = "|:::|";
System.out.println(7);
break;
case 8:
temp_barcode = "|::|:";
System.out.println(8);
break;
case 9:
temp_barcode = "|:|::";
System.out.println(9);
break;
default:
break;
}
barcode += temp_barcode;
System.out.println(barcode);
zipnum = zipnum / 10;
}
}
public String getBarcode() {
return barcode;
}
public static void main(String args[]) {
Zipcode z = new Zipcode(95014);
z.createBarcode();
System.out.println(z.getBarcode()); // output: :|::|:::||||::::|:|:|:|::
}
}
Add barcode this way
case 0:
barcode += "||:::";
break;
case 1:
barcode += ":::||";
break;
case 2:
barcode += "::|:|";
break;
case 3:
.....
.....
.....
and remove this line
barcode += barcode;
This will solve your issue.
Your barcode variable overwrite in your code so add local variable in createBarcode method. try this:
public class Zipcode{
private String zipcode = "";
private String barcode = "";
private int zipnum = 0;
public static void main(String[] a){
Zipcode z = new Zipcode(1568);
System.out.println(z.zipnum);
z.createBarcode();
System.out.println(z.zipnum);
System.out.println(z.getBarcode());
}
public Zipcode(int zip_number){
zipnum = zip_number;
}
public void createBarcode(){
while (zipnum > 0){
String barcode;
switch (zipnum % 10)
{
case 0:
barcode = "||:::";
break;
case 1:
barcode = ":::||";
break;
case 2:
barcode = "::|:|";
break;
case 3:
barcode = "::||:";
break;
case 4:
barcode = ":|::|";
break;
case 5:
barcode = ":|:|:";
break;
case 6:
barcode = ":||::";
break;
case 7:
barcode = "|:::|";
break;
case 8:
barcode = "|::|:";
break;
case 9:
barcode = "|:|::";
break;
default:
barcode = "";
break;
}
this.barcode += barcode;
zipnum = zipnum / 10;
}
}
public String getBarcode(){
return barcode;
}
}
First create a Zipcode instance, Zipcode code = Zipcode(9)
Then call createBarcode() for that instance, so in my case code.createBarcode()
Then you can print out the getBarcode() method.
REMEMBER, getBarcode() only returns a String with the barcode but it does not print it out. You must use print to print it out.
EXAMPLE: System.out.println(code.getBarcode())
I am kind of relatively new to java and am working on a project for class. This is a portion of the system and i have been getting errors in this piece.
public class NumberTest
{
private static void main( String[] args )
{
}
static private char GenerateNumber()
{
int gennumber = 0;
while(gennumber < 3)
{
char returnChar = ' ';
switch(randInt(0,9))
{
case 0:
returnChar = '0';
break;
case 1:
returnChar = '1';
break;
case 2:
returnChar = '2';
break;
case 3:
returnChar = '3';
break;
case 4:
returnChar = '4';
break;
case 5:
returnChar = '5';
break;
case 6:
returnChar = '6';
break;
case 7:
returnChar = '7';
break;
case 8:
returnChar = '8';
break;
case 9:
returnChar = '9';
break;
default:
System.out.println("Error Random int outside expected values");
}
return returnChar;
System.out.println(GenerateNumber());
gennumber++;
}
}
}
The error I am receiving is:
Test.Again.java:12: error: cannot find symbol
switch(randInt(0,9)
symbol: method randInt(int,int)
location: class NumberTest
You are trying to call undeclared function randInt. It means you must first write this function in your class and then use it.
However you may use java.util.Random:
Random rand = new Random();
switch(rand.nextInt() % 10)
...
Hello StackOverflow community, I've just begun to work with arrays and I was wondering how I can make the switch statement work with the values below. It's really irritating that it won't work considering the only error is constant expression required:
import java.lang.Character;
public class Ass11a {
public static char[] vowel = new char[4];
public static char[] consonant = new char[20];
public static char[] number = new char[9];
public static char[] punctuation = new char[10];
public static void main(String[] args) {
EasyReader console = new EasyReader();
System.out.print("Enter a character: ");
char input = console.readChar();
Character.toLowerCase(input);
vowel[0] = 'a';
vowel[1] = 'e';
vowel[2] = 'i';
vowel[3] = 'o';
vowel[4] = 'u';
consonant[0] = 'q';
consonant[1] = 'w';
consonant[2] = 'r';
consonant[3] = 't';
consonant[4] = 'y';
consonant[5] = 'p';
consonant[6] = 's';
consonant[7] = 'd';
consonant[8] = 'f';
consonant[9] = 'g';
consonant[10] = 'h';
consonant[11] = 'j';
consonant[12] = 'k';
consonant[13] = 'l';
consonant[14] = 'z';
consonant[15] = 'x';
consonant[16] = 'c';
consonant[17] = 'v';
consonant[18] = 'b';
consonant[19] = 'n';
consonant[20] = 'm';
number[0] = '1';
number[1] = '2';
number[2] = '3';
number[3] = '4';
number[4] = '5';
number[5] = '6';
number[6] = '7';
number[7] = '8';
number[8] = '9';
number[9] = '0';
punctuation[0] = '.';
punctuation[1] = ',';
punctuation[2] = '?';
punctuation[3] = '!';
punctuation[4] = ';';
punctuation[5] = ':';
punctuation[6] = '"';
punctuation[7] = '\'';
punctuation[8] = '(';
punctuation[9] = ')';
punctuation[10] = '-';
switch(input)
{
case vowel[0]:case vowel[1]:case vowel[2]:case vowel[3]:case vowel[4]:
System.out.println("Vowel");
break;
case consonant[0]:case consonant[1]:case consonant[2]:case consonant[3]:
case consonant[4]:case consonant[5]:case consonant[6]:case consonant[7]:
case consonant[8]:case consonant[9]:case consonant[10]:case consonant[11]:
case consonant[12]:case consonant[13]:case consonant[14]:case consonant[15]:
case consonant[16]:case consonant[17]:case consonant[18]:case consonant[19]:
case consonant[20]:
System.out.println("Consonant");
break;
case number[0]:case number[1]:case number[2]:case number[3]:case number[4]:
case number[5]:case number[6]:case number[7]:case number[8]:case number[9]:
System.out.println("Number");
break;
case punctuation[0]:case punctuation[1]:case punctuation[2]:case punctuation[3]:
case punctuation[4]:case punctuation[5]:case punctuation[6]:case punctuation[7]:
case punctuation[8]:case punctuation[9]:case punctuation[10]:
System.out.println("Punctuation");
break;
default:
System.out.println("Other");
break;
}
}
}
You could use an enum like this
public enum Vowels {
A,E,I,O,U;
static boolean isVowel(char c) {
switch (c) {
case 'A': case 'a':
case 'E': case 'e':
case 'I': case 'i':
case 'O': case 'o':
case 'U': case 'u': return true;
}
return false;
}
static Vowels getVowel(char c) {
switch (c) {
case 'A': case 'a': return A;
case 'E': case 'e': return E;
case 'I': case 'i': return I;
case 'O': case 'o': return O;
case 'U': case 'u': return U;
}
return null;
}
}
and use it like
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
while (console.hasNextLine()) {
String input = console.nextLine();
for (Character ch : input.toCharArray()) {
if (Vowels.isVowel(ch)) {
System.out.println(ch + " is vowel");
}
}
}
}
The following code won't run and keeps throwing a Number Format Exception on the line the error is "Exception in thread "main" java.lang.NumberFormatException: For input string: "SAN 1905 1808+24 1512+17 1209+10 1708-06 2016-16 211831 211941 192652" ""
intAlt = Integer.parseInt(strAlt);
I'm not sure why this is happening and would appreciate any advice on the matter
private int getPos(String strAlt)
{
int intAlt;
int intPos =0;
intAlt = Integer.parseInt(strAlt);
switch (intAlt)
{
case 3:
intPos = 4;
break;
case 6:
intPos = 9;
break;
case 9:
intPos = 17;
break;
case 12:
intPos = 25;
break;
case 18:
intPos = 33;
break;
case 24:
intPos = 41;
break;
case 30:
intPos = 49;
break;
case 34:
intPos = 56;
break;
case 39:
intPos = 63;
break;
}
return intPos;
}
If you pass "SAN 1905 1808+24 1512+17 1209+10 1708-06 2016-16 211831 211941 192652" into getPos(String strAlt), it will execute:
intAlt = Integer.parseInt(strAlt);
Of course it will throw exception.
See this doc
I'm working with an application right now for our project in school. My application is about Resistor Color Code calculation. My codes are working, but in displaying the values, I used the value as string. My problem is I want to make my result value as 1.2K ohms, 1.5M ohm or 5.4M ohms, just like that. Because in my codes the result will display 1200 ohms, 1500K ohms or 5400K ohms. Help me Please. Thanks in advance for the help.
This is my code for a, b, c, d and value is the display in EditText.
calcu is a button.
calcu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//for first band
if (a=="Black")
a = " ";
if (a=="Brown")
a = "1";
if (a=="Red")
a = "2";
if (a=="Orange")
a = "3";
if (a=="Yellow")
a = "4";
if (a=="Green")
a = "5";
if (a=="Blue")
a = "6";
if (a=="Violet")
a = "7";
if (a=="Gray")
a = "8";
if (a=="White")
a = "9";
//for second band
if (b=="Black")
b = "0";
if (b=="Brown")
b = "1";
if (b=="Red")
b = "2";
if (b=="Orange")
b = "3";
if (b=="Yellow")
b = "4";
if (b=="Green")
b = "5";
if (b=="Blue")
b = "6";
if (b=="Violet")
b = "7";
if (b=="Gray")
b = "8";
if (b=="White")
b = "9";
//for multiplier
if (c=="Black")
c = " ";
if (c=="Brown")
c = "0";
if (c=="Red")
c = "00";
if (c=="Orange")
c = "000";
if (c=="Yellow")
c = "0000";
if (c=="Green")
c = "00000";
if (c=="Blue")
c = "000000";
if (c=="Violet")
c = "0000000";
if (c=="Gray")
c = "00000000";
if (c=="White")
c = "000000000";
//for Tolerance
if (d=="Brown")
d = "1";
if (d=="Red")
d = "2";
if (d=="Green")
d = "0.5";
if (d=="Blue")
d = "0.25";
if (d=="Violet")
d = "0.1";
if (d=="Gray")
d = "0.05";
if (d=="Gold")
d = "5";
if (d=="Silver")
d = "10";
Value.setText(a + b + c + "\u2126" + " " + "\u00B1" + d + "%" + " Tolerance");
int result = getTheResult();
String Result = "";
if(result > 0 && result < 1000) Result = "" + result + " Ohms";
else if(result >= 1000 && result < 1000000) Result = "" + (result / 1000) + "K Ohms";
else if (result >= 1000000) Result = "" + (result / 1000000) + "M Ohms";
else Result = "Invalid Value";
import javax.swing.JOptionPane;
public class Resistance {
String digit_band1_color;
String digit_band2_color;
String multiplier_band3_color;
String tolerance_band4_color;
int temp1,temp2,temp3;
double temp4;
double result;
public Resistance(String a,String b,String c,String d){
digit_band1_color=a;
digit_band2_color=b;
multiplier_band3_color=c;
tolerance_band4_color=d;
switch (digit_band1_color){
case "Black":
temp1=0;
break;
case "Brown":
temp1=1;
break;
case "Red":
temp1=2;
break;
case "Orange":
temp1=3;
break;
case "Yellow":
temp1=4;
break;
case "Green":
temp1=5;
break;
case "Blue":
temp1=6;
break;
case "Voilet":
temp1=7;
break;
case "Grey":
temp1=8;
break;
case "White":
temp1=9;
break;
}
switch (digit_band2_color){
case "Black":
temp2=0;
break;
case "Brown":
temp2=1;
break;
case "Red":
temp2=2;
break;
case "Orange":
temp2=3;
break;
case "Yellow":
temp2=4;
break;
case "Green":
temp2=5;
break;
case "Blue":
temp2=6;
break;
case "Voilet":
temp2=7;
break;
case "Grey":
temp2=8;
break;
case "White":
temp2=9;
break;
}
switch (multiplier_band3_color){
case "Black":
temp3=0;
break;
case "Brown":
temp3=1;
break;
case "Red":
temp3=2;
break;
case "Orange":
temp3=3;
break;
case "Yellow":
temp3=4;
break;
case "Green":
temp3=5;
break;
case "Blue":
temp3=6;
break;
case "Voilet":
temp3=7;
break;
case "Grey":
temp3=8;
break;
case "White":
temp3=9;
break;
}
switch (tolerance_band4_color){
case "Brown":
temp4=1;
break;
case "Red":
temp3=2;
break;
case "Orange":
temp4=0.05;
break;
case "Yellow":
temp4=0.02;
break;
case "Green":
temp4=0.5;
break;
case "Blue":
temp4=0.25;
break;
case "Voilet":
temp4=0.1;
break;
case "Grey":
temp4=0.01;
break;
case "Gold":
temp4=5;
break;
case "Silver":
temp4=10;
break;
}
result=Math.pow(10,temp3);
System.out.println("Resistance = "+temp1+temp2+result+"+-"+temp4+"%");
}
public static void main(String[] args) {
String a=JOptionPane.showInputDialog(null,"Please Enter Color in Proper Format like this Black,Brown etc");
String b=JOptionPane.showInputDialog(null,"Please Enter Color in Proper Format like this Black,Brown etc");
String c=JOptionPane.showInputDialog(null,"Please Enter Color in Proper Format like this Black,Brown etc");
String d=JOptionPane.showInputDialog(null,"Please Enter Color in Proper Format like this Black,Brown etc");
Resistance calculator=new Resistance(a,b,c,d);
}
}