Java randInt switch function issue - java

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)
...

Related

Having trouble creating a bag of objects - Scrabble word search

I have to create a scrabble word search for my data structures class. I haven't reached the actual search yet. First, I need to create a bag of scrabble tiles. However, I keep getting errors when trying to add ScrabbleTile objects to my bag.
I have four classes: ScrabbleTile, ScrabbleBag, ScrabbleHand, and WordFinder.
Here is ScrabbleTile:
public class ScrabbleTile {
private char letter;
private int points;
ScrabbleTile (char letter)
{
this.letter = letter;
switch (letter)
{
case '_':
points = 0;
case 'e':
case 'a':
case 'i':
case 'o':
case 'n':
case 'r':
case 't':
case 'l':
case 's':
case 'u':
points = 1; break;
case 'd':
case 'g':
points = 2; break;
case 'b':
case 'c':
case 'm':
case 'p':
points = 3; break;
case 'f':
case 'h':
case 'v':
case 'w':
case 'y':
points = 4; break;
case 'k':
points = 5; break;
case 'j':
case 'x':
points = 8; break;
case 'q':
case 'z':
points = 10; break;
default: System.out.println("Incorrect character. Please enter a lowercase letter, a-z.");
break;
}
}
public char getLetter()
{
return letter;
}
public int getPoints()
{
return points;
}
}
Here is my ScrabbleBag class:
import DSLib.*;
public class ScrabbleBag {
private BagADT<ScrabbleTile> letterBag;
ScrabbleBag()
{
letterBag = new Bag<>();
for (int i = 0; i < 12; i++) {letterBag.add(ScrabbleTile('e'));}
}
}
In the ScrabbleBag constructor, I'm trying to add the correct number of each letter tile, starting with "e". Netbeans had a few suggestions which I tried, but then it was telling me the line syntax was wrong, after creating a few more instance variables in the ScrabbleBag class. How can I properly add ScrabbleTile objects to the bag?
My professor was very clear that we cannot use methods ahead of what we've gone over in class. Thanks in advance!
I think you missed new when create ScrabbleTitle object
for (int i = 0; i < 12; i++) {
letterBag.add(new ScrabbleTile('e'));
}
To create a ScrabbleTile you need the new keyword:
for (int i = 0; i < 12; i++) {
ScrabbleTile tile = new ScrabbleTile('e');
letterBag.add(tile);
}

Converting all letters in the phone number to digits

Im trying to replace each letter with a digit using the international standard letter/number mapping. I got my output to run correctly however, how do get the dashes in the phone number to appear automatically in the output? For example, if I enter 1800Flowers it prints out as 18003569377. How do I get it to print out as 1-800-3569377 without using regular expressions?
import java.util.Scanner;
public class PhoneKeypad {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//while loop keeps the program running until the user enters quit
while (true) {
System.out.println("\nEnter a phone number or quit to exit:");
String phoneNumber = input.next();
if (phoneNumber.equalsIgnoreCase("quit")) {
System.out.print("\nProgrammed by me");
return;
}
//checks if the phone number entered is at least 8 digits
if (phoneNumber.length() < 8) {
System.out.println("Invalid Phone Number");
} else {
System.out.println(getNumber(phoneNumber));
}
}
}
//method converts all letters in the phone number to digits
public static String getNumber(String phoneNumber) {
int keypadNum = 0;
for (int i = 0; i < phoneNumber.length(); i++) {
char letter = phoneNumber.charAt(i);
if (Character.isAlphabetic(letter)) {
letter = Character.toUpperCase(letter);
switch (letter) {
case 'A':
case 'B':
case 'C':
keypadNum = 2;
break;
case 'D':
case 'E':
case 'F':
keypadNum = 3;
break;
case 'G':
case 'H':
case 'I':
keypadNum = 4;
break;
case 'J':
case 'K':
case 'L':
keypadNum = 5;
break;
case 'M':
case 'N':
case 'O':
keypadNum = 6;
break;
case 'P':
case 'Q':
case 'R':
case 'S':
keypadNum = 7;
break;
case 'T':
case 'U':
case 'V':
keypadNum = 8;
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
keypadNum = 9;
break;
default:
System.out.println("Invalid phone number");
}
phoneNumber = phoneNumber.substring(0, i) + keypadNum + phoneNumber.substring(i + 1);
}
}
return phoneNumber;
}
}
Expected Output:
You could use a regular expression with String.replaceAll. Remove the leading one, group the first three digits, the second three digits and the final group of digits. Something like
public static String formatNumber(String phoneNumber) {
if (phoneNumber.startsWith("1")) {
phoneNumber = phoneNumber.substring(1);
}
return phoneNumber.replaceAll("(\\d{3})(\\d{3})(\\d+)", "1-$1-$2-$3");
}
or
public static String formatNumber(String phoneNumber) {
return phoneNumber.replaceAll("1(\\d{3})(\\d{3})(\\d+)", "1-$1-$2-$3");
}
And then call it like
System.out.println(formatNumber(getNumber(phoneNumber)));
I ran it with 1800flowers and got (as expected)
1-800-356-9377
or without regular expressions like
public static String formatNumber(String phoneNumber) {
if (phoneNumber.startsWith("1")) {
phoneNumber = phoneNumber.substring(1);
}
return "1-".concat(phoneNumber.substring(0, 3)) //
.concat("-").concat(phoneNumber.substring(3, 6)) //
.concat("-").concat(phoneNumber.substring(6));
}
Before calling formatNumber, you can remove the dashes to normalize it with something like
public static String removeDashes(String phoneNumber) {
StringBuilder sb = new StringBuilder();
for (char ch : phoneNumber.toCharArray()) {
if (ch != '-') {
sb.append(ch);
}
}
return sb.toString();
}
Then
System.out.println(formatNumber(removeDashes(getNumber(phoneNumber))));

error with switch cases...code not returning anything

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())

Generator printing null before the intended output

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.

How do you place a class in the default package in netbeans for Mac?

My instructor requires us to take the package from our code and make it a default package. The only problem is he taught us how to do that through Windows and I have a MacBook so his way isn't working. I can't figure out how to do it. I've attached the code to the bottom in case that will help.
package romannumeralcalculator;
import java.util.*;
public class RomanNumeralCalculator
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int integer;
do {
System.out.print("Please enter an interger from 1 to 5999. Enter a negative number to exit. \n ->");
integer = input.nextInt();
} while (integer >= 6000);
while (integer == 0) {
System.out.println("");
break;
}
String results = "";
int ones = integer % 10;
int tens = (integer / 10) % 10;
int hundreds = (integer / 100) % 10;
int thousands = (integer / 1000) % 1000;
switch (thousands) {
case 1:
results += "M";
break;
case 2:
results += "MM";
break;
case 3:
results += "MMM";
break;
case 4:
results += "MMMM";
break;
case 5:
results += "MMMMM";
break;
default:
System.out.println("");
}
switch (hundreds) {
case 1:
results += "C";
break;
case 2:
results += "CC";
break;
case 3:
results += "CCC";
break;
case 4:
results += "CD";
break;
case 5:
results += "D";
break;
case 6:
results += "DC";
break;
case 7:
results += "DCC";
break;
case 8:
results += "DCCC";
break;
case 9:
results += "CM";
break;
default:
System.out.println("");
}
switch (tens) {
case 1:
results += "X";
break;
case 2:
results += "XX";
break;
case 3:
results += "XXX";
break;
case 4:
results += "XL";
break;
case 5:
results += "L";
break;
case 6:
results += "LX";
break;
case 7:
results += "LXX";
break;
case 8:
results += "LXXX";
break;
case 9:
results += "XC";
break;
default:
System.out.println("");
}
switch (ones) {
case 1:
results += "I";
break;
case 2:
results += "II";
break;
case 3:
results += "III";
break;
case 4:
results += "IV";
break;
case 5:
results += "V";
break;
case 6:
results += "VI";
break;
case 7:
results += "VII";
break;
case 8:
results += "VIII";
break;
case 9:
results += "IX";
break;
default:
System.out.println("");
}
System.out.println(results);
}
}
In the projects tab of Netbeans in the top-left:
Expand the tree for your Project.
Expand the Source Packages folder.
Expand the package with your java file.
Drag the java file from under the package to the Source Packages folder.
A dialog box will pop up with the title Move Class. On that dialog click the Refactor button.
This should be the same procedure in Windows. I am not sure why your professor told you something different.

Categories