I have to Create a RandomDataGenerator. The code I have kind of works, but I am having trouble with some parts of it. The fist issue I am having is with the integers. this is what I get when I run it:
run:
What do you want to generate?
i = integer, d = double, c = char
i
How many integers do you want?
10
Choose low value for your integer:
3
Choose high value for your integer:
7
Integers:
4
3
6
6
6
5
2
3
5
6
It gives me the 10 numbers, but it is not supposed to go lower than 3.
The other issue I'm having is with the characters this is what I get
run:
What do you want to generate?
i = integer, d = double, c = char
c
How many characters do you want?
5
Choose low limit for your character:
C
Choose high limit for your character:
S
P
false
how can I fix this problems?
package javaapplication5;
import java.util.Random;
import java.util.Scanner;
public class JavaApplication5 {
private static int a;
public static void main(String[] args) {
System.out.println("What do you want to generate? ");
System.out.println("i = integer, d = double, c = char" );
char i, d, c;
int low_int = 0,high_int = 0, quant_int = 0;
double low_double = 0, high_double = 0, quant_double = 0;
char low_char = 0, high_char = 0, quant_char = 0;
int result = 0;
String input;
Scanner in = new Scanner (System.in);
String A= in.next();
int largo = A.length();
char select []= new char [largo];
select[a]=A.charAt(a);
for(int q=0; q<largo;q++){
if (select[q]=='i') {
System.out.println( " How many integers do you want? ");
Scanner Integerswanted = new Scanner(System.in);
String B= in.next();
quant_int = Integer.parseInt(B);
System.out.println( " Choose low value for your integer: ");
Scanner LoVa = new Scanner(System.in);
String LV= in.next();
low_int = Integer.parseInt(LV);
System.out.println( " Choose high value for your integer: ");
Scanner HiVa = new Scanner(System.in);
String hV= in.next();
high_int = Integer.parseInt(hV);
System.out.println( " Integers: ");
int random_generator = JavaApplication5.random_generator(quant_int, low_int, high_int);
int s = random_generator;
if(s>low_int && s<high_int){
}
}
else if (select[q]=='d'){
System.out.println( " How many doubles do you want? ");
Scanner DoublesWanted = new Scanner(System.in);
String R= in.next();
quant_double = Double.parseDouble(R);
System.out.println(" Choose low value for your double: ");
Scanner LoDo = new Scanner(System.in);
String LD= in.next();
low_double = Double.parseDouble(LD);
System.out.println(" Choose high value for your double: ");
Scanner HiDo = new Scanner(System.in);
String HD= in.next();
high_double = Double.parseDouble(HD);
System.out.println( " Doubles: ");
double random_generator = JavaApplication5.random_generator(quant_double, low_double, high_double);
double k = random_generator;
}
else if (select[q]=='c'){
System.out.println( " How many characters do you want? ");
Scanner CharWanted = new Scanner(System.in);
String Cw= in.next();
quant_char = A.charAt(0);
System.out.println( " Choose low limit for your character: ");
Scanner LoCh = new Scanner(System.in);
String LC= in.next();
low_char = A.charAt(0);
System.out.println( " Choose high limit for your character: ");
Scanner HiCh = new Scanner(System.in);
String HC= in.next();
high_char = A.charAt(0);
char random_generator = JavaApplication5.random_generator(quant_char, low_char, high_char);
char e = random_generator;
} }}
public static int random_generator(int quant, int low, int high){
Random rand_num = new Random();
int return_int = 0;
for(int z = low; z <= quant + 2 ; z++)
System.out.println(rand_num.nextInt(high) + 1 );
return return_int;
}
public static double random_generator(double quant, double low, double high){
Random rand_num = new Random();
double return_double = 0;
for(double z = low; z <quant + 2; z++) {
System.out.println(rand_num.nextDouble() + low );
}
return return_double;
}
public static char random_generator(char quant, char low, char high){
String [] abecedario = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
"K", "L", "M","N","O","P","Q","R","S","T","U","V","W", "X","Y","Z" };
int Rand_num = (int) Math.round(Math.random() * 26 ) ;
System.out.println( abecedario[Rand_num] );Random
rand_num = new Random();
char return_char = 0;
for(double z = low; z < quant + 1; z++)
System.out.println(abecedario[Rand_num].equals(high));
return return_char;
}
}
For the integer, you should use:
rand_num.nextInt(high - low) + high;
For the Character, you should use the same thing except you need to typecast it to a character after you find the random integer.
(char)rand_num.nextInt(high - low) + high;
Related
Having trouble getting my program to output the index[1] of my array "nArray", if nArray[0] = bob, and nArray[1] = jim. When I'm trying to print the input, it will print nArray[0] bob, but when it gets to nArray[1] it does not output.
public String toString(){
return getClass().getName() + "#" + Integer.toHexString(hashCode());
}
public static double salePercent(double[] sArray, String[] nArray){
double total = 0;
for (int b =0 ; b < sArray.length; b++){ // sum calculator
total = total + sArray[b];
}
double percent = 0;
for (int k = 0; k < nArray.length; k++){
System.out.println(nArray[k]);
}
return total;
}
edit
showing how i am creating and declaring and creating my arrays
in my mainclass
System.out.println("How Many Employees :");
int size = input.nextInt();
input.nextLine(); //dummy
String[] nArray = new String[size]; // array for staff names
double[] sArray = new double[size]; // array for staff sales
for (int i = 0 ; i < size; i++){
sales.nameArray(nArray,i, input);
input.nextLine();
sales.saleArray(sArray,i,input);
}
sales.salePercent(sArray, nArray);
in my class
public String inputStaff(){
Scanner user = new Scanner(System.in);
int size;
System.out.print("How Many Employees :\n");
size = user.nextInt();
user.nextLine(); //dummy to grab \n value from nextInt so nextline can function
String[] nArray =new String[size];
double[] dArray = new double[size];
int i =0;
for(i = 0 ; i < nArray.length;i++){
System.out.print("Enter name: ");
nArray[i] = user.nextLine();
System.out.print("Enter sales ($): ");
dArray[i] = user.nextDouble();
user.nextLine();
}
return null;
}
public static String[] nameArray(String[] nArray,int i,Scanner input){
System.out.print("Enter name: ");
nArray[i] = input.nextLine();
return nArray;
}
public static double[] saleArray(double[] sArray,int i,Scanner input){
System.out.print("Enter sales ($): ");
sArray[i] = input.nextDouble();
return sArray;
}
Binary to decimal converter without the use of built-in Java methods. It must do this conversion automatically. When I get the last number of the integer during input it gives me a number format exception.
import java.util.Scanner;
public class Homework02 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter an 8-bit binary number:");
int binary = keyboard.nextInt();
int copyBinary = binary;
int firstDigit = Integer.parseInt(Integer.toString(copyBinary).substring(0, 1));
int secondDigit = Integer.parseInt(Integer.toString(copyBinary).substring(1, 2));
int thirdDigit = Integer.parseInt(Integer.toString(copyBinary).substring(2, 3));
int fourthDigit = Integer.parseInt(Integer.toString(copyBinary).substring(3, 4));
int fifthDigit = Integer.parseInt(Integer.toString(copyBinary).substring(4, 5));
int sixthDigit = Integer.parseInt(Integer.toString(copyBinary).substring(5, 6));
int seventhDigit = Integer.parseInt(Integer.toString(copyBinary).substring(6, 7));
int eigthDigit = Integer.parseInt(Integer.toString(copyBinary).substring(7));
firstDigit = firstDigit*128;
secondDigit = secondDigit*64;
thirdDigit = thirdDigit*32;
fourthDigit = fourthDigit*16;
fifthDigit = fifthDigit*8;
sixthDigit = sixthDigit*4;
seventhDigit = seventhDigit*2;
eigthDigit = eigthDigit*1;
System.out.println(firstDigit+" "+secondDigit+" " +thirdDigit+" "+fourthDigit+" "+fifthDigit+" "+sixthDigit+" "+seventhDigit+" "+eigthDigit);
System.out.println(copyBinary + " in decimal form is " + (firstDigit+secondDigit+thirdDigit+fourthDigit+fifthDigit+sixthDigit+seventhDigit+eigthDigit));
}
}
Leading zeros are ignored when you parse and format an int. The simplest solution is to keep the full value as a string and only then parse the individual digits:
String binary = keyboard.next();
int firstDigit = Integer.parseInt(binary.substring(0, 1));
// etc.
What I proposed in the comments is to read the whole input as string and then convert one character at the time to integer
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter an 8-bit binary number:");
String input = keyboard.nextLine();
// need to validate input here
int dec = 0;
for (int i=0; i<input.length(); i++) {
int x = Character.getNumericValue(input.charAt(input.length()-1-i));
dec += x * Math.pow(2, i);
}
System.out.println("For binary number " + input + " its decimal value is " + dec);
I am trying to run a program that outputs the sum of every digit of an entered in integer. How would I go about reading the number and outputting each digit?
Example: Input is 4053 the output would be "4+0+5+3 = 12".
import java.util.Scanner;
public class Digits{
public static void main(String args[]) {
//Scans in integer
Scanner stdin = new Scanner(System.in);
System.out.println("Enter in a number: ");
int number = stdin.nextInt();
//Set sum to zero for reference
int sum = 0;
int num = number; //Set num equal to number as reference
//reads each digit of the scanned number and individually adds them together
//as it goes through the digits, keep dividing by 10 until its 0.
while (num > 0) {
int lastDigit = num % 10;
sum = sum + lastDigit;
num = num/10;
}
}
}
That is the code I used for calculating the sum of the individual digits, now I just need help with outputting the individual digits. Any tips and tricks would be much appreciated.
import java.util.Scanner;
public class Digits{
public static void main(String args[]) {
//Scans in integer
Scanner stdin = new Scanner(System.in);
System.out.println("Enter in a number: ");
int number = stdin.nextInt();
//Set sum to zero for reference
int sum = 0;
int num = number; //Set num equal to number as reference
//reads each digit of the scanned number and individually adds them together
//as it goes through the digits, keep dividing by 10 until its 0.
String numToString = "";
while (num > 0) {
int lastDigit = num % 10;
numToString +=lastDigit+" + ";
sum = sum + lastDigit;
num = num/10;
}
//eliminate the last + sign
numToString = numToString.substring(0,numToString.lastIndexOf("+")).trim();
System.out.println(numToString +" = " +sum);
}
}
I am not sure what you mean by outputting but instead of this you can read the number as string and take each character and parse it to integers
Scanner stdin = new Scanner(System.in);
System.out.println("Enter in a number: ");
String number = stdin.next();
int[] result = new int[number.length];
for(int i=0;i<number.length;i++) {
result[i] = Integer.parseInt(number.charAt(i)+"");
}
return result;
You may read this like String and then divided by the number.
final Scanner s = new Scanner ( System.in );
final String line = s.nextLine ().trim ();
final char [] array = line.toCharArray ();
int sum = 0;
for ( final char c : array )
{
if ( !Character.isDigit ( c ) )
{
throw new IllegalArgumentException ();
}
sum = sum + Character.getNumericValue ( c );
}
System.out.println ( "sum = " + sum );
without a scanner you can do
StringBuilder sb = new StringBuilder();
String sep = "";
int ch;
long sum = 0;
while((ch = System.in.read()) > ' ') {
if (ch < '0' || ch > '9') {
System.out.println("Skipping " + (char) ch);
continue;
}
sb.append(sep).append((char) ch);
sep = " + ";
sum += ch - '0';
}
sb.append(" = ").append(sum);
System.out.println(sb);
Try this:
import java.util.*;
public class Digits {
public static void main(String [] args)
{
Scanner input = new Scanner (System.in);
System.out.println("Enter number -> ");
int number = input.nextInt();
int sum = 0;
String numStr = "" + number;
while(number > 0)
{
int lastDigit = number % 10;
sum += lastDigit;
number = number / 10;
}
for(int i = 0; i < numStr.length();i++)
{
System.out.print(numStr.charAt(i));
// Dont print an extra + operator at the end
if( i == numStr.length() - 1) continue;
else
System.out.print(" + ");
}
System.out.print(" = " + sum);
}
}
I am doing a credit card validation program( if you are unfamiliar with the method http://9gag.com/gag/70886/cracking-the-credit-card-code) , here is the link for it).
Problem is: when trying to to do operation "int multi" in the code, the int does not import the real values of the credit card as proccessed by the charAt operation. how can I solve this, and what is it that I am doing wrong ? Also char does not allow math operations, or does it ?
import java.io.PrintStream;
import java.util.Scanner;
public class CreditCardCheck {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
PrintStream ps = System.out;
int multi;
System.out.println("Enter CreditCard number: ");
String ccn = sc.nextLine();
if(ccn.length() < 16 || ccn.length() > 16 ){
System.out.println("ccn is larger or less than 16-digits");//checking for 16-digit
}else if(ccn.length() == 16){
System.out.println("Validating CreditCard ");
//multiplied numbers
char zero = ccn.charAt(0);
char second = ccn.charAt(2);
char fourth = ccn.charAt(4);
char sixth = ccn.charAt(6);
char eight = ccn.charAt(8);
char ten= ccn.charAt(10);
char twelve = ccn.charAt(12);
char fourteen = ccn.charAt(14);
// added numbers
char first = ccn.charAt(1);
char third = ccn.charAt(3);
char fifth= ccn.charAt(5);
char seventh = ccn.charAt(7);
char nineth = ccn.charAt(9);
char eleven = ccn.charAt(11);
char thirteen = ccn.charAt(13);
char fifteen = ccn.charAt(15);
//multiplication and addition
multi = ((zero*2)+ first) + ((second*2)+third)
+ ((fourth*2)+ fifth) + ((sixth*2)+seventh) + ((eight*2)+nineth)
+ ((ten*2)+eleven) + ((twelve*2)+thirteen) + ((fourteen*2)+fifteen);
System.out.println(multi);
}
}
}
import java.io.PrintStream;
import java.util.Scanner;
public class CreditCardCheck {
public static void main(String[] args){
Scanner sc = new Sc`enter code here`anner(System.in);
int multi;
System.out.println("Enter CreditCard number: ");
String ccn = sc.nextLine();
if(ccn.length() != 16 ){
System.out.println("ccn is not equal to 16-digits");//checking for 16-digit
}
else if(ccn.length() == 16){
System.out.println("Validating CreditCard ");
//multiplied numbers
int zero = Integer.parseInt(ccn.charAt(0)+"");
int second = Integer.parseInt(ccn.charAt(2)+"");
int fourth = Integer.parseInt(ccn.charAt(4)+"");
int sixth = Integer.parseInt(ccn.charAt(6)+"");
int eight = Integer.parseInt(ccn.charAt(8)+"");
int ten= Integer.parseInt(ccn.charAt(10)+"");
int twelve = Integer.parseInt(ccn.charAt(12)+"");
int fourteen = Integer.parseInt(ccn.charAt(14)+"");
// added numbers
int first = Integer.parseInt(ccn.charAt(1)+"");
int third = Integer.parseInt(ccn.charAt(3)+"");
int fifth= Integer.parseInt(ccn.charAt(5)+"");
int seventh = Integer.parseInt(ccn.charAt(7)+"");
int nineth = Integer.parseInt(ccn.charAt(9)+"");
int eleven = Integer.parseInt(ccn.charAt(11)+"");
int thirteen = Integer.parseInt(ccn.charAt(13)+"");
int fifteen = Integer.parseInt(ccn.charAt(15)+"");
//multiplication and addition
multi = ((zero*2)+ first) + ((second*2)+third) + ((fourth*2)+ fifth) + ((sixth*2)+seventh) + ((eight*2)+nineth) + ((ten*2)+eleven) + ((twelve*2)+thirteen) + ((fourteen*2)+fifteen);
System.out.println(multi);
System.out.println(zero);
}
}
}
I tried to convert octal to decimal and I got some output but I am not satisfied with it. Can anyone give an idea how to convert a different model without using API-s?
public static void main(String args[]){
System.out.print("Enter the number to convert");
Scanner ss =new Scanner(System.in);
int a = ss.nextInt();
int b = ss.nextInt();
int c = ss.nextInt();
int d = ss.nextInt();
int temp =(a*(8*8*8));
int temp1 =(b*(8*8));
int temp2 =(c*(8));
int temp3 =(d*(1));
System.out.println("The decimal is " +"\n" + (temp +temp1 +temp2 +temp3))
}
Try this one:
public static void main(String[] args)throws IOException
{
BufferedReader reader =
new BufferedReader(new InputStreamReader(System.in));
String oct = reader.readLine();
int i= Integer.parseInt(oct,8);
System.out.println("Decimal:=" + i);
}
I havn't try this but try to get a logic,
take input in a string then
String s ; //for input
String answer ="";
for(a=0,b=s.length-1;b>=0;b--)
{
int digit =Integer.parseInt(s.charAt(a));
int ans = (int)(digit * Math.pow(8,b));
answer+=ans;
a++;
}
Maybe my version is something you are looking for:
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number: ");
String numberString = sc.nextLine();
int[] eachNumbers = new int[numberString.length()];
for(int i = 0; i < eachNumbers.length; i++){
eachNumbers[i] = Integer.parseInt(numberString.substring(i, i+1));
}
int digit = 0;
for(int i = 0; i < eachNumbers.length; i++){
if(eachNumbers.length > 1){
digit += (eachNumbers[i] * Math.pow(8, eachNumbers.length - (i+1)));
}else{
digit += (eachNumbers[i] * 1);
}
}
System.out.println("The Octal " + numberString + " as Decimal is " + digit);
}
I know there is alot that can be made better but I'm still learning and wanted to help you :)