My current problem is when im trying to choose case 2 and call markItemOnLoan i cant seem to find the title i want after i enter a few. I will include all of my code but i have mentioned what I am currently stuck on :
import java.util.Scanner;
public class Library {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
MediaItem mCall = new MediaItem();
Library call = new Library();// makes instance
I'm trying to find a word in an array of char.....but I'm stuck. How to formulate the code to step through the array and pick out the word. This is what I have so far...
public static void searchAcross(String string, char[][] puzzle) { // Gets the number of rows in the matrix int rowLength = puzzle.length; //Gets the number of columns in the matrix. int colLength = puzzle[0].length;
I have an assessment for college, the part of my code that im struggling with is the part where the user enter their first name followed by a space and then their second name. I m using ---------------fullname = JOptionPane.showInputDialog("enter your firstname followed by a space then your second name " ); ------------- to capture this. the issue is if the user only enter 1 name I need to output an error. the issue is I cant find a way to tell if the user entered a second name. This is what I have so far:
public void makename() { // makes an inputbox to ask their name fullname = JOptionPane.showInputDialog("enter your firstname followed by a space then your second name " ); //separates the first and second name into 2 strings in order to make the username
[code]....
The problem is it accepts anything I type in without causing an error. What should I type in to stop this? ive tried anything but I cant find a way to tell if a surname has been entered or not.
The problem asks me to write an expression whose value is the number of characters in a specific string. Normally, it would be a simple task of using the String.length() method, but that is not allowed. In fact, no variable declaration is allowed (it's a MyProgrammingLab assignment, if any are familiar).
Simply put, if I have a String "This is a sample string." is it possible to find the length without assigning it to anything?
In other words, the code must be able to go into the parentheses of System.out.println( ); and correctly print the length of said string.
The answer, for those curious:
System.out.println("This is a sample string.".length());
I didn't know the .length() method could be called on a literal. Now if only I could find out how to close this thread...
My interest in Java leads me to try and print numbers from 1-100. The output should show all numbers from 1-100 and each number that is divisible by by 13 should be replaced with a string "Fuzzy".
public class Fuzzy { public static void main (String[]args){ for(int i = 0; i < 100; i++) if(i % 13==0) System.out.print(i + "fuzzy"); } }
I have assigned random numbers to an 2D array, and I had to write a method that prompts the user for a number between 100 and 200. If that number is in the array, it should return 1. If not, 0. It only stays 0 though, even when the number matches one of the numbers in the array.
public static int Findit(int[][] numbers, int guess) { int Findit = numbers[0][0]; int i = 0; int x = 0; boolean found = false; for(i=0;i<numbers.length;i++)
How to declare a 2 dimensional array of interger of size 10 by 10. and then set every element of the array to 3. After this I need to add up all the element in this array and return the value.
final int ROWS = 10; final int COLS = 10; int[][] foo = new int[ROWS][COLS]; for (int i = 0; i < ROWS; i++){ for(int j = 0; i< COLS; i++){ foo[i][j] = 3; } }
This is what i have done so far(not 100% if is correct..) but how to add up all the array.
I'm trying to find the median of a set of numbers inputted into an array and I wanted to know how I would pass the array to the median calculator after I use selection sort to organize the numbers.THIS IS THE CODE THAT ORGANIZES THE ARRAY:
public void selectionSort(double[] myArray) { for (int i = 0; i < myArray.length - 1; i++) { int index = i; for (int j = i + 1; j < myArray.length; j++)
if (myArray[j] < myArray[index])
[code]....
The code that finds the median will only work if the array being used is already organized. How do I pass the new sorted array to the code that finds the median?
I'm almost finished with my assignment, I'm just having one small issue. The goal of this assignment is to have a user input as many values they want(up to 10) into a list. I need to store those in an array then figure out the sum of the numbers, the average, the Largest value, and the smallest value. I get everything to print out correct so far except for the Smallest number, it is returning a 0 every time. Here is my code:
import java.util.Scanner; public class SimpleList{ Scanner in = new Scanner(System.in); final private static int ARRAY_LENGTH = 10; private float[] List = new float[ARRAY_LENGTH]; private int count = 0;
public class StuTest { private static Scanner keyboard = new Scanner(System.in); public static void main(String[] args){ int[][] testScores; // [students][tests] String[] stuNames;
[Code] ....
The method I am having issues with is the "printStudentReport" method. I am trying to calculate the average of a randomly generated number of students and tests. For my "printTestReport" method, I have to calculate the average of the test by test number.
I have a 2D array that is of type int[][]. it is populated with 1's and 0's. I need to create a method that allows me to search from the top row finding a possible path to the bottom--the path is a path of connecting cells containing 1. I need to create an array that stores the cells that are needed to be searched. When the search carries out, for each cell stored it needs to check the cells to the left, right, below and above the current cell.
I also need to create a variable to store the current cell. I thought initially it would be an int but it can't be because it needs to hold the the index of the current cell. and any of the cells searched that are an immediate neighbour of the current cell are added to the storage array.
I have these instructions but I am having trouble converting into code
Finding a path through vegetation from the top row to the bottom row: The idea is we will start from a cell in the top row and advance below for as long as we can. Since we are still not done exploring a cell until we have explored ALL it’s vegetation neighbors, we will add the cell to an array of cellsToExplore and only come back to it if the current cell we are examining is fully explored. Here is pseudo-code of the algorithm !
• Create array cellsToExplore (what size should this array be?)!
• Create an integer count that stores the number of cells in cellsToExplore!
• Go through the top row and add ALL vegetation cells to cellsToExplore!
• Create a variable to store currentCell we are exploring and set it to null.!
• while count > 0 do!
- set currentCell to the last cell in cellsToExplore (last cell added to the array).!
- label currentCell as burning vegetation!
- If the currentCell is on the bottom row then we return true (we found the path).!
- if the cell below currentCell is vegetation then add the cell below to the cellsToExplore array.!
- else if cell to the right of currentCell is vegetation then add the cell to the right to cellsToExplore!
- else if cell to the left of currentCell is vegetation then add the cell to the left to cellsToExplore!
- else if cell above the currentCell is vegetation then add the cell above to cellsToExplore.!
- else remove the currentCell from the cellsToExplore (we are done with this cell). !
What program needs to find is the most biggest number. It does the job, but another task of the program is to find the index of that number . The second loop should do just that, but for some reason, as the loop goes further, it passes through the if statement even though answer "a[i]" is not equal to "answer". The idea is that if a[i] and answer are equal, the "i" should represent the index number.
I am working on this project that wants me to write a program that inputs 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, display it only if it is not a duplicate of a number already read. The only part I am confused about is how to go about checking for duplicate values that the user may enter. And IF the user does input a duplicate value, it should not be stored again.In addition, the value entered should be printed out after it is entered along side the value that have been previously entered by the user such as:
23 23 45 23 45 67 23 45 67 12 and so on.
I am still fairly new at java programming.
import java.util.*; public class NumberArray { public static void main(String[] args) { // declare an array with 5 elements
I am attempting to find the element that holds the lowest time ( i have used System.currentMillisTime ) in the array using a linear search and to then print the times held by the array in lowest to highest order . While i understand how to do a linear search using user input as the key i am not to sure how to do it by initializing a search key in the program to the lowest number and have little experience in using a search in a program that is not a simply linear search. i have attempted to code this, as seen below, but i know i am definitely wrong and i have tried another of different ways even Array.sort and then a binary search .
static long store_MAX[]; // Now an ‘array’ of ints static int deptSize; // Holds the length of the buffer private int shopper_MAX; // Holds the number of items in the buffer
I have a problem where I have to create random integers from 1 to n + 1. When the array is displayed I have to find the missing integer from the values that are displayed and show that value. I have to use O(n^2) and O(n) operations. I have created this code so far but I cannot figure out how to make my values display n + 1. It is only displaying values 1, 2, 3, 4 and 5 in random order. I also cannot figure out how to find the missing value. I have created a boolean displayed statement but can't determine how to use it in this code.
=Java import java.util.Random; public class Task6 { public static void main(String[] args) { int[] numbers = new int[7]; //create array of numbers Random random = new Random(); boolean displayed = false; //boolean value to determine if number was displayed
A java program to print 10 random numbers between 512 and 1024 and then finds the smallest and biggest numbers from the list using only one loop
class BSLab4d { public static void main (String[] args){ int[] nums = new int[10]; int largest = nums[0]; int x = 0; int smallest = nums[0]; int y = 0;
Code a Java method that accepts a String array and a String. The method should return true if the string can be found as an element of the array and false otherwise. Test your method by calling it from the main method which supplies its two parameters (no user input required). Use an array initialiser list to initialise the array you pass. Test thoroughly.
public class test { public static void main(String[] args) { Printhelloworld(); String[] verbs = {"go", "do", "some", "homework"}; printArrays(verbs);
How do I compare a String to each element of a string array?
For example:
int headscount = 0; if (coins[i].equals("heads")){ headscount++; System.out.println("b" + headscount); }
This doesn't give me the right value because the IDE says that equals() is an incompatible type. I also tried changing the "heads" to an variable, but the results remains the same.
So I'm creating a class which when given three inputs uses them as sides of a triangle and tells ther user what type of triangle it is, or if the input is invalid, tells them why it is invalid. I'm readin the input as a string and then trying to split it into a string array, from there checking to see if it has 3 elements.. in which the data is good at that point, and then converting them to ints and checking to see if they're negative ansd finally checking to see if they can work as sides of a triangle ie a+b >c, a+c >b , b+c >a.
I'm trying to split it into an array of strings but am getting an error, and can't seem to figure out why as this should be working from what I've read of the string.split method online.
import java.util.*; public class TriangleTest{ private int sideA; private int sideB; private int sideC; public static void main(String[] args){ TriangleTest triangle = new TriangleTest("3 4 5");
I have the following code in which I am looping through the rows of one array (composed of Strings) and copying it to another array. I am using .clone() to achieve this and it seems work as it changes the memory location of the rows themselves. I did notice that the String objects are still pointing to the same location in memory in both arrays but I won't worry about that for now, at the moment I just want to understand why the array I am cloning is not successfully assigning to the other array.
This is the incorrect line: ar[r] = maze[r].clone();