Clarification On Array Logic
Mar 3, 2014
explain me the logic behind this array..how this logic flows
min=max=nums[0];
for(int i=1;i<5;i++){
if(nums[i] < min) min=nums[i];
if(nums[i] > mzx) max=nums[i];
//Find the minimum and maximum values in an array.
class Minmax{
public static void main(String[] args){
int nums[] = new int[10];
int min, max;
[code]....
View Replies
ADVERTISEMENT
Oct 3, 2013
I should mention here that number of elements can be from 1....10'000. And it has to be done as O(N)
I attempted at the problem like so :
public static int adjacencies(int[] A) {
int count = 0;
boolean found = false;
for(int i = 0; i < A.length-1; i++) {
if (A[i] == A[i+1]) {
[Code] .....
To me it looks right and it seems to work.
View Replies
View Related
Oct 12, 2014
The program i am working on is to take string from the user which should be a phone number then it will return true or false based upon the method that checks to see if it meets the criteria of a certain format if does not it will return false otherwise return true. I am working on a if/else structure that will print this number is valid if it meets the criteria of the method i previously mentioned or return the number is not valid if it is false. the logic of the expression i put in the parentheses of the if/else statement. This is what i have so far:
if(){
System.out.println("The phone number is valid");
}
else {
System.out.println("This isn't a valid phone number");
}
Do i need to compare it based upon the method that checks if it is true?
View Replies
View Related
Oct 31, 2014
int a=Integer.parseInt(jTextField1.getText());
String c = "";
String d="";
if (a>32) {
a-=32;
c+="1";
[Code] ....
I've made this but when i enter a no. than the result comes like eg 9=001001 so i need to delete the zeroes before 1 ....
View Replies
View Related
Mar 2, 2014
I've decided to go back to basics and start using pseudocode--but i want to see if my logic is correct.so here's the first program:each delegate to a certain conference must wear a tag showing their name and the organization they represent as shown below:
######################
#### Annual Conference #####
######################
##NAME: ##
######################
##ORGANIZATION: ##
######################
write a class called TagMaker that prints out tags. supply methods to (a) set the name (b) set the organization (c) print tag with the name and organization (d) clear the name and organization (e) print a blank tag. then write a TagTester class to test the TagMaker class. so this is the pseudo code (or logic) that i've come up with for the program:
TagMaker main class:
-Prompt user to enter in first and last name, as well as their organization.
-Read user input for the name and organization.
-Return user input.
-Write conference tag showing the person's information.
-Clear information.
-Print blank tag.
1. i assume i would have to import a few java libraries.
Java Code: java.util.Scanner mh_sh_highlight_all('java'); and Java Code: java.io.*; mh_sh_highlight_all('java');
2. afterwards i would declare the instance variables (private, i assume). static too? so for example: Java Code: private static string firstName mh_sh_highlight_all('java');
3. i'm not sure if this program requires a constructor...being that i'm supposed to clear the program at the end anyway.
4. i create a public class declaring my input variable. example: Java Code: String first = firstName.nextLine(); mh_sh_highlight_all('java');
5. so i then i have to create methods that get/set the user input.
getter/setter example:
Java Code:
public String getFirst(){
return first;
}
public void setFirst(){
this.first = new first;
} mh_sh_highlight_all('java');
6. to print out both tags, would i write most of the code as part of a loop and make an "if" statement where the user input is valid, it prints out a tag with information, and another "if" statement that would automatically clear the buffer?
7. last i would create a tester class that would primarily be responsible for the program's output.
View Replies
View Related
Mar 4, 2014
I am trying to find out the logic behind the Bubble sort,
for(a=1; a < size; a++)
for(b=size-1; b >=a ; b--){
if(nums[b-1] > nums[b]){
t=nums[b-1];
nums[b-1] = nums[b];
[code]....
View Replies
View Related
Oct 31, 2014
int a=Integer.parseInt(jTextField1.getText());
String c = "";
String d="";
if (a>32)
[Code] ....
I've made this but when i enter a no. than the result comes like eg 9=001001 so i need to delete the zeroes before 1.
View Replies
View Related
Jul 7, 2014
if (number % 2 == 0 && number % 3 == 0)
System.out.println(number + " is divisible by 2 and 3");
if (number % 2 == 0 || number % 3 == 0)
System.out.println(number+ " is divisible by 2 or 3");
if (number % 2 == 0 ^ number % 3 == 0)
System.out.println(number + " is divisble by 2 or 3 but not both");
I understand the logic operators and the variable number, what i dont understand is what number % 2 == 0 and number % number == 0 mean. I know that % is a remaining operator and == means equals, i know that = and == are not the same.
View Replies
View Related
Jan 2, 2015
I'm trying to fix this logic error in my slot machine game where no matter what slot combo comes up, it says you have won $10 and proceeds to add that amount to the balance and subtract the bet amount to the balance, even though it is not a winning combo! I've been trying to solve that and when doing so, I commented out Slot1.randNum1 == 0, Slot2.randNum2 ==0, and Slot3.randNum3 ==0 and anything relating to see if that was the problem, and it seemed like it was, because after that, the logic error described above was gone, but in doing so, the loss/win counter didn't increment, nor did the program pick up on the winning combos. However, I do not know why commenting out anything relating to randNum ==0 would cause that.
Here is the code for the processing (as you can see, some parts are commented in attempt to fix the logic error, but right now the code below is for the logic error that keeps on telling that the user has won):
public void askData ()
{
title ();
int betAmount;
while (true)
{
try
{
c.setCursor (3, 1);
[code]....
View Replies
View Related
Mar 18, 2014
I'm trying to get the server start up arguments in java using System.getProperty("arg") but it returns null value.
I set the argument value in server console----server----star tup----argument(-Darg=dev).
Other than this any configurations are required. I used weblogic 10.3.5.
View Replies
View Related
Sep 12, 2014
My code runs and populates an arraylist. However my break statement, while stopping the loop ends up being added to the arraylist. And I'm not sure how to fix this error.
public static void main(String args[]) throws Exception
{
// declaring variables
String input = "";
// creating array list
ArrayList<String> nameList = new ArrayList<String>();
[Code] ....
View Replies
View Related
Apr 11, 2014
I want to run a few things in my main when a JButton is pressed but can't work out the best way to do it. Here's the code in the main.
public static void main(String[] args) throws FileNotFoundException, ParseException, InterruptedException{
Map m = new Map("defaultMap");
GameLogic g = new GameLogic("defaultMap");
GodsEyeView gev = new GodsEyeView(g, m);
gev.initialRun();
//Want to run this when a button is pressed.
HumanUser h = new HumanUser(g);
playerGUI gui = new playerGUI(h);
gui.run();
gev.run();
}
View Replies
View Related
Feb 8, 2015
I was tasked with creating a program that encrypts a line of text (for example, CANDY) by shifting the letters X amount of times. For example, if the user inputs the sentence CANDY and selects a shift of 5, the output would be: HFSID. I got this part working fine. The issue I am having is with the decryption part of the program.
This is simply the reverse of the above, as the user would enter the phrase HFSID, with a shift of 5, and the program would output: CANDY. It works fine, all except for one letter, being the "F" letter. With my code, when I enter the above word to be decrypted it outputs: C[NDY
Obviously, that [ bracket is not an 'A'. I realise the issue falls with the equation, Here is my code:
public class SimpleEncryption {
/**
* @param args the command line arguments
*/
static int answer;
public static void main(String[] args) {
String cipher = JOptionPane.showInputDialog(null, "Please enter a sentence or word that you wish to encode or decode. This program uses"
+ " a basic cipher shift.");
[code]....
View Replies
View Related
Jan 28, 2014
I need to find the Winner using Object Orientation logic I have my old logic from my Tic Tac Toe game but it is not Object Orientated. So I want to convert it and add that code to my GUI Tic Tac Toe. I need to return a winner or tie.
View Replies
View Related
Jun 30, 2014
I am going to develop a new web application of medium complexity. Right now i am somewhat comfortable with JSF and hibernate. I have never used JSF and hibernate together before.I just wanted to ask if it is good practice to use JSF (for both view layer and handling business logic) and hibernate(for persistence) without spring as a middle layer. The reason why i am asking this is i don't know anything about spring framework.
View Replies
View Related
Feb 12, 2015
Why is the logic not correct with the method public static boolean containsColor(String color, Circle [] ca)? It only returns me only false values.
Java Code: public class Circle {
private double radius;
private String color;
public Circle()
{
radius = 1;
color = "red";
[code]....
View Replies
View Related
Oct 6, 2014
I'm having trouble with sorting Strings- 3 strings inputted by user, and I would like to output them in alphabetical order. I've used the str.compareToIgnoreCase method, and then I've tried to loop them through a series of if/ else statements. Everything I've been able to find online (including the forums here) has suggested to use the Comparator class, or to put the strings into an array, and sort list- I really would like to stick with just the String class, and its methods .
The program itself works and compiles, but I am getting logic errors that I have been unable to solve. I'm using IntelliJ Idea, and I've ran it through the built in debugger, about 100+ times (not exaggerating, lol) just to see what it's doing in particular scenarios. For instance, I can get c, a, b, to print out as a,b,c correctly, but a,b,c, will print out as b,a,c.
For me this is kind of like a Sudoku puzzle, or a Rubik's cube! Each time I fix one scenario, it breaks another one, so I don't know if there's a(logic) solution to fix all possible scenarios (abc, acb, bac etc... to all print abc) or if possibly I just need more if statements. I've only pasted in the area where I'm having problems (the if statements). I'm a big fan of the "Next Line" syntax.
(Note: please assume the non relevant content- import Scanner class, main method, etc... I didn't want to paste the entire program.)
System.out.println("Enter the first statement: ");
//input.nextLine();
string1 = input.nextLine();
System.out.println("Enter the second statement: ");
string2 = input.nextLine();
System.out.println("Enter the third statement: ");
string3 = input.nextLine();
[Code] ....
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
Mar 24, 2015
I am taking the Class Algorithms and Datastructures and got an assignment for Lab that really throws me off. The goal is to create an Array out of a given CSV file, implement several Methods that get the size of array, etc.
I am still stuck in the first part where the CSV has to be imported into the Array. My problem is that I need a mechanism that figures out the needed size for the Array, creates the array, and only then transfers the data from the CSV.
The list consists of the following wifi related values:
MAC-Adress, SSID, Timestamp, Signalstrength.
These are on the list, separated by comma. The Columns are each of these, and the rows are the four types of values making up the information on a certain wifi network.
The catch is, we are not allowed to use any of the following:
java.util.ArrayList
java.util.Arrays
and any class out of java.util.Collection.
So far I used the BufferedReader to read in the file and tried to implement the array, but I get an arrayindexoutofboundsexception.
Below is my Code (Its still an active construction zone):
public class WhatsThere {
public WhatsThere(String wifiscan) throws IOException {
}
public static void main(String[] args) throws IOException {
// WhatsThere Liste = new WhatsThere(String wifiscan);
String[][] arrayListe = new String[0][0];
[Code] ....
View Replies
View Related
Feb 13, 2014
i am working on the same project and i got the code to make them print going down but not sideways.
public class ArrayofHope
{
public static void main(String args[])
{
System.out.println("Decimal Character
");
for(int j = 65; j <= 90; j++)
{
System.out.print(j);
System.out.println(" " + (char)j); //Character
}
}
}
View Replies
View Related
Aug 3, 2014
I am working on a problem where i have to create a 2d array with given input of the dimensions (odd number) of array, along with a number within the array and to then print out all of the numbers surrounding that number.
Anyway, i am working on simply making the spiral, which should look like the one below.
n = 3
7 8 9
6 1 2
5 4 3
where the 1 always starts in the center with the 2 going to the right, 3 down, then left etc. etc. I was able to create the code by starting on the outer edges rather than the center and working my way to the middle, however my code always starts from the top left and goes around to the center where it needs to start from the top right. I am having trouble altering my code to meet this criteria. This is what i have thus far.
import java.io.*;
public class Spiral
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the number of elements : ");
int n=Integer.parseInt(br.readLine());
[Code] .....
View Replies
View Related
Nov 15, 2014
We were given a class lab that asks us to write a program that create a multidimensional array ( 5 x 5 ), populates the array using nested loops with letter from A until Y, and displays the array to the screen. and the result should look like this:
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
How to write this program.. I have tried all my best but the results are not coming like this..
View Replies
View Related
Jun 21, 2014
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();
My code:
private String[][] maze = {{"*","*","*"," ","*","*","*","*","*","*"},
{"*"," ", "*"," "," "," ","*"," ","*","*"},
{"*"," ","*","*","*"," ","*"," ","*","*"},
{"*"," "," "," "," "," "," "," "," ","*"},
{"*","*","*","*","*"," ","*","*","*","*"},
{"*","*","*","*","*"," ","*","*","*","*"}};
//private String[][] mazeCopy = copyMaze(new String[6][10]);
private <T> T[][] copyMaze(T[][] ar){
for (int r = 0; r < ar.length; r++){
ar[r] = maze[r].clone();
}
return ar;
}
My compiler says: Required: T[]. Found: java.lang.String[]
Since ar[r] is an array and .clone() also returns an array why is this line incorrect.
View Replies
View Related
Jun 15, 2014
Write a method that returns a new array by eliminating the duplicate values in the array using the following method header: public static int[] eliminateDuplicates(int[] list). The thing is that I found the working solution that is written below, but how it works. How to eliminateDuplicates method done with flag and flag2.
Here is the code:
Java Code:
import java.util.Scanner;
public class Exercise06_15 {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
int[] numbers = new int[10];
System.out.println("Enter ten numbers: ");
[code]....
View Replies
View Related
Oct 13, 2014
I am receiving an ArrayIndexOutOfBoundsException for the following code, which moves a creature through a 2D array maze. I have altered the clauses of the first if statement for the four direct methods (north, south, east, and west) multiple times (i.e. x + 1 >= 0 && x > 0 && x - 1 > 0 && x < array.length...etc). However, while the code occasionally runs, more often than that it returns this exception. Catching the exception seems like a poor workaround though if worst comes to worst I'll do that.
I included only the relevant functions of the code:
public boolean goNorth(char[][] array) {
boolean success = true;;
x = getX();
//x = this.x;
y = getY();
//y = this.y;
if ((x - 1 >= 0 && x - 1 < array.length)
&& (y >= 0 && y < array[x].length)) {
[Code] .....
View Replies
View Related
Feb 7, 2015
I am passing input from the user to a method that will initialize an array of the data (scores in this case). The method is filling the entire array with the last input value.
array initializer method
Java Code:
public static float[] inputAllScores(float validScore) {
float[] diverScores = new float[7];
for (int i = 0; i < diverScores.length; i++) {
diverScores[i] = validScore;
}
return diverScores;
} mh_sh_highlight_all('java');
[Code] .....
View Replies
View Related