How To Get Sum Of All Numbers In A Row Of 2D Array With Row Being Selected By User

Mar 9, 2015

I just learned about 2D arrays and am still trying to get a grasp on the concepts. I'm a little confused by how you return all the values in a row to add up and display the sum if the row is entered by the user.

import java.util.*;
public class RowSum {
public static void main(String[] args) {
Scanner s = new Scanner (System.in);
int userpick = 0;
int sum = 0;

[Code] .....

View Replies


ADVERTISEMENT

User To Input 10 Numbers - Find Smallest Element In Array

Dec 2, 2014

I am trying to write a code that asks the user to input ten numbers and then finds and displays the smallest number out of the ones given. I am supposed to implement arrays into the program to do this. But the problem I have run into is that when I compile the code in jgrasp, I am given several error messages and I am not quite sure what I have done wrong. I'm assuming it is either a syntax or a logical error on my part but reading over the code I do not understand what is causing these errors.

This is the most current draft of my code:

import java.util.Scanner;
 public class Exercise7_9 {
public static void main(String[] args) {
double[] numbers = new double[10];
//Enter ten double numbers: Scanner(System.in)
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.println("Please enter ten numbers: ");

[Code] .... 
 
/* Sample Run:
Enter ten numbers: 1.9, 2.5, 3.7, 2, 1.5, 6, 3, 4, 5, 2
*/

And these are the exact error messages:

----jGRASP exec: javac -g Exercise7_9.java
Exercise7_9.java:35: error: '.class' expected
if (double m > list[i]) {
^
Exercise7_9.java:35: error: illegal start of expression
if (double m > list[i]) {

[Code] .....

View Replies View Related

Creates Array Of 3 Consecutive Ints (0 - 7) User Has To Guess What Those Numbers Are

Aug 16, 2014

I have the following program. In a nutshell, I creates an array of 3 consecutive ints and the user has to guess what those numbers are, knowing that they are between 0 and 7, this is from Head First Java. In the book, the code has a bug that is made on purpose and they challenge you to fix it. As you can see bellow, every time a user's guess matches a int in the array, the NumOfHits is increased by one. When the NumOfHits is 3 (the length of the array) the game finishes.

The bug is that if you guess 1 and 1 is in the array, if you type in 1, 3 times, I will increase the NumOfHits 3 times and end the game as if you had won. To solve the bug, you need to find a way to tell the program that if the user already guessed that number, it should no longer be taken into account and we shouldn't increase the NumOfHits if the same number is provided.

I "fixed" this by searching for the index of the number that matches an int of the array, and changing that index's value to 100, since the user knows that the numbers are between 0 and 7, they will not submit 100.

The result? If the user submits 2 and 2 is in the array, the value of its indexed is replaced by 100, so that if the user submits 2 again it doesn't count as a match.

Please see the comments in the code

import java.io.*;
import java.util.Arrays;

class DotCom{
int NumOfHits = 0;
int[] LocationCells;
public void setLocationCells(int[] locs){
LocationCells = locs;

[Code] .....

The code works, but for some reason, some times I get a ArrayIndexOutOfBoundsException run time error and it appears to be somehow random. This is what I saw on the command line earlier

C:Userspablo.alvarez>java launchDotCom

Enter a number: 3
missed
Enter a number: 4
hit
Enter a number: 5
hit
Enter a number: 7
missed
Enter a number: 5
missed
Enter a number: 4
missed
Enter a number: 3
missed
Enter a number: 4
missed
Enter a number: 5
missed
Enter a number: 6

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at DotCom.checkYourSelf(launchDotCom.java:16)
at DotComGame.startGame(launchDotCom.java:59)
at launchDotCom.main(launchDotCom.java:72)

As you can see here, 3 returned 'missed' but 4 and 5 returned hit. Since the numbers are in sequence and are only 3, it makes sense that the last number is 6 (4,5,6). You will then notice that when I submitted 3 and 4 again it returned 'missed' as expected, because the numbers were already matched earlier, but then when I submitted 6, it returned the error seen above.

Sometimes this doesn't happen at all, sometimes it happens at the 2nd guess or third, it appears to be somehow random. I'm familiar with the error message, but I don't see where I'm using an index that doesn't exist in the array. The only time I'm referencing an array index is on

LocationCells[index] = 100;

but the index variable will only hold a value if the int submitted by the user matches on of the values in one of the indexes, so how is it that I'm going over the number of available indexes?

View Replies View Related

Ask User To Store 10 Numbers In Array - Print In Order Entered And Reverse

Apr 15, 2014

Write a Java program that asks the user to store 10 numbers in an array. You should then print the array in the order entered and then print in reverse order. However, instead of putting all the information in the main, you should use a class called PrintIt. The PrintIt class should have an instance variable that is an array to hold the numbers. You should have a method that will print the array in the order entered. You will also need a method to print in reverse oder. Then create a tester class that asks the user to enter 10 numbers and puts them in an array.

So far I've got this.

public class printIt {
int i;
int [] numArr = new int [10];
public printIt () {
i = -1;

[Code] .....

Output:

Enter a number:

8
34

Forward:

8
34

Reverse:

34
8

end

what i want as an number is being able to print out 10 numbers but this only lets me do two numbers. Why is that so?

View Replies View Related

JSF :: Storing User Session Attributes Through Selected Item?

Mar 18, 2014

I have to use the company's login resource, which returns a login page for authentication after a browser/user page request; after successfully authenticated, the session attributes are set in a (remote) valued object (VO) invoked via servlet; then, such values are set in my (local) POJO.

I want to display the current user session attribute (see xChave below) and save it in database. How can I persist http User session attributes through the structure below?

Abstract Controller (just the main code):

public abstract class AbstractController<T> {
@Inject
private AbstractFacade<T> ejbFacade;
private Class<T> itemClass;
private T selected;
private Collection<T> items;
//GETTERS AND SETTERS OMMITED
public AbstractController(Class<T> itemClass) {
this.itemClass = itemClass;
}

[code]...

View Replies View Related

Calculate And Store Value Into String The User Selected And Keeping It Going

Oct 28, 2014

This is the first time we are using 2 different classes. This is the code I have so far. What I am having trouble is doing the calculations and storing the value into the planet the user selected and keeping it going. I will attach the instructions ....

public class FakeGravity{
// Instance variables
private String planet;
private int VelocityDecay;
private double BouncinessFactor;
// default constructor
public FakeGravity(){

[Code] ....

View Replies View Related

User To Enter 10 Numbers And At The End Prints Out Distinct And Non-repeated Numbers

Nov 23, 2014

I have to make a program that prompts the user to enter 10 numbers and at the end it prints out the distinct numbers and then the other numbers that weren't repeated...

I have the part where it prints out the distinct numbers but I stuck on how to make it print out the other numbers that didn't repeat...

import javax.swing.JOptionPane;
public class DistinctNumbers {
public static void main(String[] args) { 
String getInput;
int input;
int[] numbers = new int[10];

[Code] ....

View Replies View Related

Program That Takes User Odd Number And Print Prime Numbers Lower Than User Input

Nov 4, 2014

I have been struggling with this program for weeks. This program is supposed to take a user's inputted odd number and then print out all prime numbers lower than that number.

public class PrimeNumber
{
Scanner scan = new Scanner(System.in);
int userNum;
String neg;

public void getUserNum()

[code]...

View Replies View Related

Adding Selected Toppings To Array In Another Class?

Dec 2, 2014

I need to add from what is selected in some check boxes to an array that is in another class, but I can't get the other class to be called correctly.

I need to get the toppings that are selected from the check boxes added into the array that is in the calculations method.

Calculations method

Java Code:

import java.util.ArrayList;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import javax.swing.JMenu;
import javax.swing.JMenuBar;

[Code] .....

View Replies View Related

How Selected Radio Button Of Text Check With Another String Of Array

Jun 20, 2014

Java Code:

ButtonGroup bg=new ButtonGroup();
JRadioButton choice1=new JRadioButton();
JRadioButton choice2=new JRadioButton();
JRadioButton choice3=new JRadioButton();
JRadioButton choice4=new JRadioButton();
bg.add(choice1);
bg.add(choice2);
bg.add(choice3);
bg.add(choice4); mh_sh_highlight_all('java');

here i coded out my radio button..i am confused how to get the selected radio button of string and match with another array of String..

View Replies View Related

Generate 100 Numbers Using Arrays - Sort Even Numbers Into Separate Array And Display Both

Apr 24, 2014

I've just written a program that generates 100 numbers that range from 0 ~ 25 using arrays, the program calls another method that sorts the even numbers into a separate array and returns the array. I need it to display both arrays, however, when I run my program, the numbers of both arrays are mixed together, and I'm not sure how to separate them.

[ public class Array1
{
public static void main(String[] args)
{
int array [ ] = new int[100];
for (int i = 0; i < array.length; i++)
{
array[i] = (int) (Math.random() * 26);

[Code] .....

View Replies View Related

User Must Get Three Random Numbers In A Row - Do While Loop

Feb 12, 2015

I need to fix a program in which the user must get a three random numbers in a row. They have five chances. This is what i have so far:

public static void main(String[] args)throws IOException {
// TODO code application logic here
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int num1=(int)(Math.random()*(9-0));
int num2=(int)(Math.random()*(9-0));
int num3=(int)(Math.random()*(9-0));

[Code] ....

View Replies View Related

Reversing Numbers (User Input)

Dec 11, 2014

I am writing a program to reverse a user inputted number (Example - 54321 is 12345) The code works great minus the fact that if I input a number that begins with zero, the output will drop the zero. Would it make more sense to just set this up using strings?

import java.util.*;
public class Week7_Programming_Problem
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
int inputNum, outputNum;

[Code] ....

View Replies View Related

1D Arrays List Of Numbers From User Input?

Dec 4, 2014

Write a program to maintain a list of the high scores obtained in a game. The program should first ask the user how many scores they want to maintain and then repeatedly accept new scores from the user and should add the score to the list of high scores (in the appropriate position) if it is higher than any of the existing high scores. You must include the following functions:

-initialiseHighScores () which sets all high scores to zero.

-printHighScores() which prints the high scores in the format: “The high scores are 345, 300, 234”, for all exisiting high scores in the list (remember that sometimes it won’t be full).

-higherThan() which takes the high scores and a new score and returns whether the passed score is higher than any of those in the high score list.

-insertScore() which takes the current high score list and a new score and updates it by inserting the new score at the appropriate position in the list

View Replies View Related

Swing/AWT/SWT :: GUI To Add A List Of Numbers Entered By The User

May 9, 2014

I have a GUI with a textArea for the user to input numbers, a button which should "listen" for those numbers, and then a textField to display the sum. I have my code working for user input of one number; but I'm at a loss as to what sort of loop I need to create to get it to read each line of input. I wasn't sure whether to put this in the beginner forum or here, because I am definitely a beginner.

So my code thus far that works with one input:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

[Code] ....

View Replies View Related

Need To Count Strings That How Many Inputs From User Were Numbers

Jan 30, 2014

import java.util.Scanner;
public class CountStringNumbers { // show the number of Strings that were numbers . . ..
public static void main(String []args){
int count = 0;
int countNumbers=0;
Scanner input=new Scanner(System.in);

[code]...

can count the numbers from 0-9 if the user puted but if it is 10 it doesnt count it , and i need to write all the numbers to have that option. . . . Is there an easy way that includes all the numbers? like if(word.equals(number)){<<<???? (numbers) i know htat doesnt work but is there anything similar ?? ??

countNumbers++;
}
System.out.println("Type Something , , , and to end the the termination type STOP");
word=input.nextLine();
count++;
}
System.out.println("You wrote "+count+" lines wich from those lines "+countNumbers+" were numbers and the programe terminated ");
}
}

View Replies View Related

Output Min And Max Numbers The User Enters - Storing Value

Jul 17, 2014

I need to receive input from the user, as many times as he wants. tha is done with the while loop. I also need to output the min and the max numbers the user enters, therefore i have the if statememnts. also if the user enters only one value i need to output that one value as the min and the max.

int min =0;
int max = 0;
int option = JOptionPane.YES_OPTION;
while ( option == JOptionPane.YES_OPTION){
String dataString = JOptionPane.showInputDialog("Enter an integer");
int data = Integer.parseInt(dataString);

[Code] ....

View Replies View Related

Add All Even Numbers Between 2 And User Input Number Which Is Included In Addition?

Apr 17, 2014

import java.util.*;
 public class SumOfAllEvens { 
public static void main (String[] args) { 
Scanner s = new Scanner (System.in);
 //for (int i=1; i<4; i++){
int usernumber;

[code]....

I'm supposed to use a for loop that runs until it reaches the number input by the user, but I'm not sure how to tell the program to add the user's number along with all of the even numbers in between the user input and 2.

View Replies View Related

Math On Three Numbers - Get Average / Sum And Number Entered By User

Feb 25, 2015

Java program : For this assignment you are going to write code for the following class:

MathOnThreeNumbers

Here are the specifications of class MathOnThreeNumbers:

Methods of class MathOnThreeNumbers:

1. inputThreeNumbers
2. getAverage
3. getSum
4. getNumberOne
5. getNumberTwo
6. getNumberThree

Constructor of class MathOnThreeNumbers

write a constructor that initializes the first, second, and three numbers to 1, 2, and 3 respectively.

Specs for the methods methods:

1.
name: inputThreeNumbers
accessibilty: public
arguments: none
what it does: asks the user for three numbers of type double

2.
name: getAverage
accessibilty: public
arguments: none
what it does: returns average of the three numbers

3.
name: getSum
accessibilty: public
arguments: none
what it does: returns sum of the three numbers

4.
name: getNumberOne
accessibilty: public
arguments: none
what it does: returns the first number entered by the user

5.
name: getNumberTwo
accessibilty: public
arguments: none
what it does: returns the second number entered by the user

6.
name: getNumberThree
accessibilty: public
arguments: none
what it does: returns the third number entered by the user

Here is an example of how the class MathOnThreeNumbers works. The following code produces the output displayed after the code.

MathOnThreeNumbers mm = new MathOnThreeNumbers();
System.out.println("first: " + mm.getNumberOne());
System.out.println("second: " + mm.getNumberTwo());
System.out.println("third: " + mm.getNumberThree());
mm.inputThreeNumbers();

[Code] .....

View Replies View Related

Numbers Inputted By User And Sorting Them Out In Ascending Order

Nov 12, 2014

I am writing a program that is supposed to take 3 numbers inputted by the user and sorting them out in an ascending order. I believe my code is correct but can't figure out why the program isn't behaving as expected.

import java.util.*; //Required to use the scanner console
public class Week3_Programming_Problem
{
static Scanner console = new Scanner(System.in); //Allows for user input

public static void main(String[] args)

[code]....

View Replies View Related

Write A Program That Prompts User For 3 Double Numbers

Feb 15, 2015

So i need to write a program that prompts the user for 3 double numbers x, y, z that outputs 2x^3 + 3y^5 + 3x^3y^2 +xyz with

(1) 4 digits precision
(2) with a ',' to separate thousands
(3) all digits of the result including the precision are put to 20 positions (from right to left)

I am not really sure what it is suppose to look like or how to start it.how to mix Math.pow with multivariable and printf.

View Replies View Related

Perform Mathematical Calculation Between Two Numbers Entered By User

Feb 16, 2015

I need to write a Java program to perform a mathematical calculation between two numbers entered by the user. User has to choose the mathematical operation and input it and then when the user enters 2 numbers, he gets the answer. When user enters any other character other than *, /, + and - he should be able to exit. Perform calculation between numbers should be until user decided to exit from the program. My code is below, calculation part goes well, but can't get it exit when user enter any other character.. How to fix it?

import java.io.*; 
public class q11

public static void main(String[]args)throws IOException
{
InputStreamReader ISR=new InputStreamReader(System.in);
BufferedReader BR=new BufferedReader(ISR);
while(true){
System.out.println("Enter..");
System.out.println("* : For multiplication");

[Code]...

View Replies View Related

Request A User To Input Two Numbers For Num1 And Num2 And Do Arithmetic?

Feb 6, 2014

New to programming, and my teacher wants us to declare two variables : num1 and num2, and have the user input values, then run it through 2*num1 - num2.

Here is what I have so far, and I am lost and frustrated:

package hellowworld;
import java.util.Scanner;
public class definevariables {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
int num1;
int num2;

[Code] ....

View Replies View Related

PhoneBook Application - Allow User To Enter Names / Numbers Of Up To 20 Friends

Apr 13, 2014

This application is supposed to allow a user to enter the names an phone numbers of up to 20 friends, until the user enters "zzz" or 20 names. Then the console is supposed to display the names and have the user enter a name to get the phone number. Here's my code:

import java.util.Scanner;
public class PhoneBook{
public static void main(String[] args){
String name;
int phoneNumber;
final int nameAmount=20;

[Code] ....

I can't seem to get it correct, every time I correct a compiler error, another set of them appear.

View Replies View Related

Add Integers To ArrayList And Prompt User To Delete Specific Numbers Not Required

Jan 21, 2015

This program simply adds integers to an ArrayList and then prompt the user if they would like to delete specific numbers that they don't want.

The output that I get when I want to delete numbers like 2 and 4 from 1 2 3 4 5 is 1 2 3 4 5 instead of 1 3 5.

Java Code:

import java.util.ArrayList;
import java.util.Scanner;
public class AL
{
// A regular array like int arr[] has to have its size declare before run-time and it's not dynamic which mean it can not grow or expand on its own.
static Scanner input = new Scanner(System.in);
// You have to use reference types(Integer, Double, Float); not primitive types(int).

[Code] .....

View Replies View Related

User Input Random Numbers Then Program Sort Them In Different Ways - Missing Return Statement

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







Copyrights 2005-15 www.BigResource.com, All rights reserved