Unable To Return Array And It Is Printing Out Random Characters?
Dec 21, 2014
This is my code:
public class rotate
{
public static void main(String[] args)
{
int[] arrayInput = {1, 7, 8, 6, 2};
for(int i = 0; i<arrayInput.length; i++)
[Code]...
It's printing out this (the second line):
View Replies
ADVERTISEMENT
Jun 15, 2014
I am having a hard time trying to figure out how to print random numbers from a an array list. I tried google but nothing worked. I have to pick certain values from two lists and print them on the screen. I have included comments in the code to facilitate the explanation.
import java.util.Random;
public class Parachute {
public static void main(String[] args) {
Random randomNumbers=new Random();
int number;
int array []={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21};
char A[] = {'a', 'b', 'c','d','e','f','g','h', 'i','j','k','l','m','n','o','p','q'};
[Code]...
View Replies
View Related
Dec 14, 2014
For part of my program, I am trying to ask the user for input on generating a number of random characters. I have already done this with integers and doubles, how would I do this for characters using ASCII values? Would I use the same format as I did for generating integers (Shown in code)?
import java.util.Random;
import java.util.Scanner;
public class NewNumberCharacter {
public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in);
[code]....
View Replies
View Related
Jan 30, 2015
Here is the java code that i compiled on eclipse but each time i run it i am getting different sequence of output !!
What I did is that my main() calls a function m1() which calls a function m2() which throws an exception back to m1() which throws exception back to main().
public class ClassB {
public static void main(String[] args) {
try {
m1();
} catch (Exception ex) {
ex.printStackTrace();
[Code] ....
The output is as shown in screenshots at different times :
SCREENSHOT 1:
SCREENSHOT 2:
SCREENSHOT 3:
How is this possible ??
View Replies
View Related
Jul 7, 2014
I want to use this idea in a different context but for the sake of the argument lets suppose I have this method:
public int random number(int range)
{
Random rand = new Random();
int randomNumber = rand.nextInt(range);
.
.
.
return randomNumber;
}
How to avoid getting the same random number twice in a row?
View Replies
View Related
Nov 7, 2014
I`m trying to make a gradebook that uses random number generator to give the score, I also need to implement student numbers and sort everything with a total at the bottom. But I cant figure out how the bubblesorting works. This is my code so far:
package karakterbok;
import static java.lang.Math;
import java.util.Random;
public class Karakterbok {
public static void main(String[] args) {
Random karakterer = new Random();
Random studid = new Random ();
[Code]...
how to sort this using bubblesort?
View Replies
View Related
Apr 7, 2014
I have been assigned to write a program that has a user input random numbers then the program is to sort them in different ways. I have the coding for the different sorts but, I have an error saying that I am missing a return statement in the "Bubble" method. I am trying to return "arr[i]" in the "for loop" which gives me this error, and when I try to take the "return arr[i]" outside of the "for()" loop the error reads the it cannot locate variable "i".
import java.awt.* ;
import java.awt.event.*;
import javax.swing.*;
public class SwingSorts extends JFrame implements ActionListener
{
JRadioButton bubble;
JRadioButton selection;
[Code] .....
View Replies
View Related
Feb 13, 2014
I have double checked this code over and over and I just can't find the problem.
What I'm trying to do is take a file and input it into an 2D array.
Ultimately, I should convert the array of integers to an array of characters, then print it out. The file contains a set of ASCII values.
After printing it out, I should then create methods to manipulate the image produced.
Using 2D arrays is a requirement for this exercise.
I think that somehow I'm overcomplicating this and the solution is a lot more simple than I think, but I can't think of what to change.
The error I am getting is:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 40
at main.main(main.java:17)
Java Code:
import java.util.*;
import java.io.*;
public class main {
public static void main(String[] args)
throws FileNotFoundException {
String[][] data = new String[22][40];
[Code] .....
View Replies
View Related
Mar 7, 2014
Is that the only way to code the JOptionPane? Seems rather similar to Sys.out
--- Update ---
I had more than this, but post was deleted somehow and this was the gist of it:
for(i = 0 ; i < charArray.length; i++){
if(Character.isDigit(charArray(i))){
address.replace("6", "*");
address.replace("7", "*");
Is there a better way to scan a character array for digits and replace any number in the array with another character ("*") in this case? The array is the address, " 1234 Runner Road " .
View Replies
View Related
Oct 17, 2014
As of right now my code can take characters from a string to an array from a string like "ABCD" but the project says I have to take it from a string like "A B C D" how can I correct my code to grab the characters from a single spaced line?
Scanner sc = new Scanner(System.in);
System.out.println("Enter Order of Cars:");
String carsInput = sc.next();
int x = carsInput.length();
int[] cars = new int[x];
for (int i=0; i < cars.length; i++) {
cars[i] = carsInput.charAt(i)-64;
}
View Replies
View Related
Nov 8, 2014
So I need to generate 10 random integers in the range 1-20 but i have to store them in an array called numbers. Then I have to call a method called displayArray which displays the contents of the array and for the assignment i have to use a for loop to traverse the array. The method header for the displayArray method is:
public static void displayArray(int[] array)
This is what I have done
public class RandomIntegers {
static int numbers = 0;
public static void displayArray(int[] array) {
System.out.println(numbers + "Numbers Generated");
[Code] .....
View Replies
View Related
Nov 5, 2014
I would like to ask how with such an array:
x_z01
x_z02
x_q01
x_q02
x_q03
From this table I have searched:
for the
"x_01"
algorithm chosen:
x_z01
for the
"x"
algorithm chose :
"x_z01"
for the
"Z01"
algorithm chose :
"x_z01"
for the
"x_q"
algorithm chose :
"x_q01"
For any character - chose any value from an array
import java.util.Scanner;
/** */
public class Array_value{
/** * @param args the command line arguments */
public static void main(String[] args) {
[Code] ....
Whether this algorithm: [Code] ....
View Replies
View Related
Feb 16, 2015
filling out a Random array: An Array of Specific Length Filled with Random Numbers This time what I need to do is take the elements from this Random array and assign them to a new Byte array:
for(int i = 0; i < limit-10; i++) {
Random dice = new Random();
int randomIndex = dice.nextInt(array.length);
if (array[randomIndex] < 128) {
System.out.print(array[randomIndex] + " ");
} else if (array[randomIndex] >= 128) {
System.out.print(array[i] + " ");
}
}
byte[] noteValues = new byte[]
{ 64, 69, 72, 71, 64, 71, 74, 72, 76, 68, 76 }; //This is the byte array filled manually!
I've tried amending the manual input to fit in with the Random array, as follows:
byte[] noteValues = new byte[]
{ array[randomIndex] };
In this case, however, the Byte array can't interpret the int values. Also, if the Byte array is outside the 'for' loop, array[randomIndex] cannot be resolved.
View Replies
View Related
Jun 30, 2014
In this simple example, I print a byte array to String:
Java Code:
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
public class PrintByteArray {
public void print(){
System.out.println(Charset.defaultCharset());
byte[] arr = {1, 2, 3, 4};
[Code] ....
However, Eclipse prints out strange boxes, which I was unable to copy into this message.
It's supposed to print out the values of the bytes. What am I doing wrong?
View Replies
View Related
Dec 11, 2014
I am trying to change an input String to an array of characters, but it only stores the word before the space into the array. Here is the code:
Scanner scanner = new Scanner(System.in);
System.out.println(" Enter text: " );
String text = scanner.next();
char[] characterArray = text.toCharArray(); // convert string to array of characters
String char = "";
for( i = 0; i < characterArray.length; i++) {
char = char + characterArray[i]
} System.out.println(char);
Just typing hello gives me hello, but when I type hello world it does not type in the word "world".I am trying to change an input String to an array of characters, but it only stores the word before the space into the array.Here is the code:
Scanner scanner = new Scanner(System.in);
System.out.println(" Enter text: " );
String text = scanner.next();
char[] characterArray = text.toCharArray(); // convert string to array of characters
String char = "";
for( i = 0; i < characterArray.length; i++) {
char = char + characterArray[i]
} System.out.println(char);
Just typing hello gives me hello, but when I type hello world it does not type in the word "world".
View Replies
View Related
Sep 7, 2014
I want to use a method, which takes for example an int and also returns an integer. For example, if the the given integer is strong return a, if it is notstrong return b. How would you write that in a Code?
I want to use that in a more general way. I want to give a method mlong the value X of the type date and let it return an int. Type date consists of 3 int, one of them is the int month.
mlong should return an int depending on the X.moth. at the moment my code looks like this:
// File1:
public class date {
public int day;
public int month;
public int year;
}
// File 2:
public class monthlength {
public int mlong(date X) {
int t;
t = X.month;
if (t == 1 || t == 3 || t == 5 || t == 7 || t == 8 || t == 10 || t == 12)
{ return 31; }
if(t == 4 || t == 6 || t == 9 || t == 11)
{return 30;}
}
}
View Replies
View Related
Oct 10, 2014
The point of this program is to read characters from a txt file and store them into a 2D array. After this has been accomplished, the information is to be printed in the same manner it is read from in the txt file.
Here is the code I have so far:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main
{
public static void main(String[] args) throws FileNotFoundException
[Code] ....
And this is the error I am receiving when trying to accomplish the goal of the project:
Exception in thread "main" java.lang.NumberFormatException: For input string: "############"
at java.lang.NumberFormatException.forInputString(Unk nown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Maze.<init>(Maze.java:15)
at Main.main(Main.java:20)
What's going on here and how I can correct this?? The information I am trying to store in a 2D array and then print is this:
############
#.#........#
#.#.######.#
#.#....#...#
#.###.*#.#.#
#...####.#.#
#.#.#..#.#.#
#.#.#.##.#.#
#o#......#.#
View Replies
View Related
May 2, 2014
So I'm trying to write a program that will take in 5 numbers, store them in an array and then print out the array and the average of the array. Problem is that every time I run my program I get a "ArrayIndexOutOfBoundsException: 5" error and I don't know who to fix it.
public class ConstructorHomework {
final static int size = 5;
static double[] myArray = new double[size];
public static double average;
public void printArray(){
[code]...
View Replies
View Related
Apr 15, 2014
So for fun I've decided to write a program that can keep track of a simple card game my friends and I designed. I've built a class for an array, that stores the basic stats of each card. I need to be able to access this array from another class, that will print the array information to a file, so we can easily keep track of who's cards have leveled up and their stats. The main class will also be using a random number generator to determine how much damage the attacker/defender deals and takes respectively. So far I have the random number generator built as well. I'm just having issues creating a print to file class and what I need to change in my array class to make it accessible by the other class that prints it to a file.
public class StartingStats {
public static void main(String[] args)
{
//Variable to use for how many spots in a stat array there are
final int statArray = 2;
[code]...
what I need to change so that a print to file class can access EACH array in this array folder. Also if any cookie cutter printToFile class I could use/borrow/change that'd be really useful, as I've never done any printing to file before. Also, the levels of each stat and exp will be changing so that's why I need a separate class to print so that I can call it when it's been updated so we always have the most up to date stats saved.
View Replies
View Related
Nov 17, 2014
My array isn't printing. I have tried everything. I have tried putting a nested for loop in different methods but still no luck. I have also tried System.out.print but that prints random characters/numbers.
import java.util.Scanner;
public class Square
{
public static void main(String[] args)
[Code].....
^For some reason the site doesn't show the spaces between the *. It's supposed to be a 5x5 square.
View Replies
View Related
Apr 6, 2015
I was having trouble printing the product of the array I am getting runtime errors. Here is part of my code
int [] grades;
grades = new int [10];
grades [0] = 100;
grades [1] = 100;
grades [2] = 100;
grades [3] = 100;
grades [4] = 83;
[Code] ......
View Replies
View Related
Aug 12, 2014
I am trying to return the lowest value from the array. having trouble trying to capture the return value by placing it into the variable ..
int myLowest = getLowest(yourNumbers); and then printing out myLowest
import java.util.*;
public class numArrays
{
public static void main(String[] args){
int numbers;
int[]yourNumbers;
[code]....
View Replies
View Related
Nov 21, 2014
what would the return be for adding two array's together,
public static int sum (int[][] a, int[][] b) {
int[] [] c = new int[a[0].length][b[0].length];
for (int i=0; i< a.length; i++){
for (int j=0; j<a[i].length; j++){
c[i][j] = a[i][j] + b[i][j];
}
}
return c[i][j];
}
My return is wrong...... I know, Any hints?
View Replies
View Related
Mar 11, 2014
I want to return values from arrays to the main and the problem is i cant use my variables from my constructor, I use new variables in my functions and i know this is no good, when I used the variables from the constructor in my function. I have a compilations errors,also i want to create un object in main and with this object i want to call the functions.
Java Code:
package javaapplication4;
import java.util.Scanner;
public class JavaApplication4 {
public static int[] MyInt;
public static double[] MyDouble;
public static String[] MyString;
public static char[] MyChar;
[Code] .....
The code is working when i run it and i have the right result in my screen but i know this is all wrong with the variables.
View Replies
View Related
Oct 15, 2014
I have tried to print array elements using standard print statement. I am getting errors. How to print them. Here is my code:
class arrayEx1{
public static void main(String args[]) {
int a[]=new int[3]; //Declaring Single Diomentional Array
a[0]=10;
a[1]=20;
a[2]=30;
int total=a[0]+a[1]+a[2];
System.out.println("Values stored in a[0],a[1],a[2]elements are :" + a[0] a[1] a[2]);
System.out.println("Total values of a[0],a[1],a[2]elements is :"+ total);
}
}
if i give comma (,) in between above print stament (print statement 1) stil i am getting errors.
View Replies
View Related
Apr 25, 2014
Ok, so the program we are supposed to do is to create implement a employee class then create a main tester class that can accept user inputs for 5 employee names, salaries, and performance rating. After that, we have to input code to calculate a provided raise amount, then print out the array with the updated raises. I have completed the program with no syntax errors, but it doesn't do anything. not even a screen comes up on my end to accept user inputs. I know its something basic, but what am i missing
Employee class
public class Employee
{
private String employeeName;
private int salary;
private int performanceRating;
[Code] ....
View Replies
View Related