Passing Strings In Methods

Mar 16, 2014

I am trying to call stringToFile in the main method but it throws error saying "incompatible types scanner cannot be converted to string."
 
public static void main(String[] args) {
Scanner keyboard = new Scanner (System.in);
mask(keyboard);
printingString();
fileName(keyboard);
  String completeFileName = stringToFile(keyboard);

[Code] ....

View Replies


ADVERTISEMENT

Passing Values To Different Methods?

Mar 1, 2015

I am trying to pass the values for UPPER_BOUND and LOWER_BOUND from the main method into the getValidNumber method. However, I'm not sure how to do this. The rest of the code is correct as far as I know, I just need to get the values for the bounds into the getValidNumber method. How would I do this? the notes in the main method explain what I need to do.

public static void main(String[] args) throws FileNotFoundException {
Scanner kb = new Scanner (System.in);
final double LOWER_BOUND = 0;
final double UPPER_BOUND = 100;
//Call the getValidNumber method, passing it the values LOWER_BOUND and UPPER_BOUND.
//Take the returned value and store it in a variable called num.
//Print out the value of num (here in the main method)
 
[Code] .....

View Replies View Related

Passing Values To Methods?

Apr 30, 2014

I'm supposed to write a program that reads in 20 numbers stores them into a one dimensional array and then create a method that will calculate the average of the numbers in a separate method.

I've written a for loop in the main method that will take in the numbers but now I need to know how I can pass those values to a method that will calculate the average.

public class ConstructorHomework {
private static double average; //Declaring the Global Variable
//This Method Calculates The Average
public void avg(int x){
average = x/20;

[Code] .....

This is the main method an it contains the for loop that will take in 20 numbers

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
final int size = 20;
double[] array = new double[size];
for (int i = 0; i < array.length; i++){
array[i] = input.nextDouble();
}
//

Here I was trying to create and object and use it to pass value to the method

ConstructorHomework a = new ConstructorHomework();
a.avg(); //Problem is that I don't know what to put in the brackets
a.showNum();
}
}

View Replies View Related

Using Double Dimensions Strings And Methods

Apr 17, 2015

i have to use a if loop. I just started using methods and loops..You have been hired as a software developer for Canada’s customs - Toronto Pearson International Airport. Your job is to develop part of the Canada Passport System. The Database Administrator has forwarded to you the database for all the Canadians. For simplicity, assume you only have five records (i.e., only five people on file). These fixed records are the following:

First name, Last name, SIN, Last Date of Entry to Canada, Number of Entries
Mike beee 4567 12-12-1999 7
Jessie weree 4444 07-07-2007 1
Liza veee 2121 05-05-2013 2
Zico qeeee 2444 Never left the country 0

the two dimensional array stores all the values. In addition, all these values should be of type String.The Canadian officer can always see the following four choices:

1) Show custom’s database,
2)Update custom’s database,
3) A summary of a passenger’s record, and
4) Exit.

Choice 1 : always allows the officer to see all the database records (i.e., the five records). Each of these records consists of the following: First Name, Last Name, SIN, Last Date of Entry to Canada, and the Number of Entries (i.e., see the above stored values).

Choice 2 : allows the officer to update the database whenever one of the five Canadian passengers arrives. Once the officer chooses this option, the system will ask him/her for the SIN of the passenger. Then the system will ask for today’s date (i.e., the recent entry date). The system will then reflect those changes on the console screen and the number of entries will be increased by one.

Choice 3: summarizes the record for one passenger where his/her first and last names and last date of entry to Canada will be shown. The last date of entry should only have the last four digits (i.e., only the year). To extract those digits, you should use the String’s method(s). However,before a record is summarized, the records of all Canadians should be shown on the screen in order to allow the officer to choose among those records. Choice 4 allows the officer to exit from the system completely; otherwise the list of choices should be always shown on his/her monitor.This is the code i have so far.

import java.util.Scanner;
public class QuestionFive{
public static void main(String[] args) {

double passportInput;
Scanner sc = new Scanner(System.in);
// public static int generalList() // to show the list of choices and return the chosen option
//
// public static void updateRecords(int recordSize, String[][] passportDataBase) //to update the records
//

[code]....

View Replies View Related

Calculator Program - Use Strings And Methods To Include Arrays

Jul 23, 2014

My assignment is to essentially update a calculator program that uses strings, and methods, to include arrays. How to create and call an array that the user defines the size of and then inputs the numbers to fill the array. Here's the example my prof gave us of what the output should look like:

Menu
1. Add
2. Subtract
3. Multiply
4. Divide
5. Dot product
6. Generate random array
7. Quit

What would you like to do? 1

How many values are in the arrays? 3

Enter the values in the first array, separated by spaces:
2 4 6

Enter the values in the second array, separated by spaces:
1 3 5

The result is [3.0, 7.0, 11.0]

How to create an array that would allow the user to define the size of the array and then inputs the values for the array? I'm completely lost. I never should have taken java as an online class.

View Replies View Related

Passing Object Argument To A Method - Unable To Call Methods On Argument

Feb 7, 2015

I am trying to pass an object of type Product p to my editProduct method, however trying to call p.getName(); doesn't work and throws a NullPointerException. The same kind of thing works for my displayRecord method (in a different class) and I can call .getName() on Product p, also passed as an argument to that method. Below is my editProduct class. The NullPointerExcepion is being thrown at line 61 (i.e., nameField.setText(p.getName());).

I don't know if I explained right, so here's a line thing of how the classes relate:

Search >>>(field.getText())>>> displayRecord(Product p) >>>editProduct(p)>>> EditProduct

And as a side note: adding the line p = new Product(); fixes it and successfully runs the class (including the Save and Quit parts) but obviously I want it to specifically refer to the Product I pass to the method.

Java Code:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;

[Code] ....

I'm asking a question because I don't understand how Product p could possibly be null, because the argument is passed through my DisplayRecord class, which also takes a Product p argument and works. In that class, I have declared Product prod = p; and prod is what I am passing to editProduct.

View Replies View Related

How To Find Whether Strings Contained In Array Of Strings Constitute A Similar Character Or Not

Aug 25, 2014

I am having an array of strings and i want to find out whether these strings contained in the array contain a similar character or not.For example i am having following strings in the array of string:

aadafbd
dsfgdfbvc
sdfgyub
fhjgbjhjd

my program should provide following result: 3 because i have 3 characters which are similar in all the strings of the array(f,b,d).

View Replies View Related

Sorting Lowercase Strings Before Uppercase Strings

Mar 26, 2014

I can sort strings in a collection by uppercase and then lowercase though I was wondering if there is any way of doing it in reverse, sorting by lowercase then by uppercase.

View Replies View Related

Value Not Passing From Second Class?

Nov 8, 2014

I am trying to pass value from class to another using net beans but it seems that I am doing something wrong.

find my basic code at sourceforge.net/projects/val1toval2/files/SRC/

I would like to have both VAL1 and VAL2 having same value in Jlabel.

//Main.java 
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Second SEC = new Second();
l_dis.setText(String.valueOf(getInput()));
l_tot.setText(String.valueOf(SEC.getTotal()));

[code]...

View Replies View Related

JSP :: Passing ID To Servlet Through URL

Mar 9, 2015

I was send the id through url like this code.

<td> <%=rs3.getString("Project")%> </td> <td> <a href="TaskAssignment.jsp?id=<%=rs3.getString("id")%>"> <input type="button" name="edit" value="Edit"></a></td>

i have problem to getting this id in servlet.

Servlet:

String id=request.getParameter("id");

the variable id getting only null

View Replies View Related

Passing A Value For Sockets?

Mar 13, 2014

I am having a problem passing an int value from one class to the other. The gist of the objective of this program is to compute a local sum at each client, and then once the local sums are computed.. to compute the overall sum from all the clients. My problem is that I'm having trouble figuring out how to pass each sum from each individual client in the class multicastSenderReceiver to the readThread class. I keep getting an error.

I feel that I have to be able to pass the sum value from each client generated in multicastSenderReceiver to the other class, because that is the only way I'll accurately be able to sum up all the values..

Java Code:

package multicastsenderreceiver;
import java.net.*;
import java.io.*;
import java.util.*;
//import static multicastsenderreceiver.multicastSenderReceiver.numProcesses;
class multicastSenderReceiver{

[code]....

View Replies View Related

Passing A Map As Parameter

Apr 30, 2014

I'm using Java in BlueJ where I'm trying to write a class to store cycling club membership details in a map. I'm trying to pass a map as a parameter to a function that will iterate over the map and print out the member details. My code is:

public class CtcMembers
{
//instance variables
private Map<String, Set<String>> memberMap;
/**
* Constructor for objects of class CtcMembers

[Code] ....

When I execute the following in the OU workspace:

CtcMembers c = new CtcMembers();
c.addCyclists();

I can see a map populated with the expected details.

When I execute the following in the OU workspace:

c.printMap(c.getMap());

I get the following error:

java.lang.ClassCastException: java.util.HashSet cannot be cast to java.lang.String
in CtcMembers.printMap(CtcMembers.java:81)
in (OUWorkspace:1)

I wasn't aware I was trying to cast anything so this has got me baffled.

View Replies View Related

C++ Passing By Value With Two Variables

Jun 24, 2014

This is my first time working with C++ and I have put together this program and came up with two errors and I am unsure what it is wanting me to do. The errors I got are:

1>c:usersownerdocumentsvisual studio 2010projectsweek5week5passing_by_value.cpp(30): error C2064: term does not evaluate to a function taking 1 arguments
1>c:usersownerdocumentsvisual studio 2010projectsweek5week5passing_by_value.cpp(38): error C2064: term does not evaluate to a function taking 1 arguments

#include<iostream>
using std::cin;
using std::cout;
using std::endl;
//initialize arrays
int incr10(int* numa,int* numb);

[Code] ....

View Replies View Related

Passing A Value Into A Method

May 8, 2014

Does the following code pass a value into a method?

/code

lnValue[x][y] = computelnValue();

View Replies View Related

JSF :: Passing Parameters From Servlet

Sep 12, 2014

I'm trying to pass a parmeter to a jsf page from a servlet(i.e. associated with paypal adaptive api), but I keep getting the following error, even though the productType on ProductDetailsVO is a String.

INFO: WARNING: FacesMessage(s) have been enqueued, but may not have been displayed.

sourceId=j_idt2[severity=(ERROR 2), summary=(j_idt2: '45;productType=Sport'

must be a number consisting of one or more digits.), detail=(j_idt2: '45;productType=Sport' must be a number between -2147483648 and 2147483647 Example: 9346)]

Calling JSF page contains:-

returnURL = new URL(new URL(request.getRequestURL().toString()),"pages/paypalpaymentapproved.xhtml?paypalID="+paypalID+";productType=Sport");
response.sendRedirect(returnURL.toString());

[Code] ....

View Replies View Related

Passing Value From JTextField In One Class To Another

Apr 6, 2015

I enter a value in the text box in one class and want to pass that value to another class in order to compare it and color the cell in a table. In the first class I have

package cege.ui;
 import cege.controller.HtmlController;
import cege.controller.ScreenCaptureController;
...
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

[Code] .....

How to pass the threshhold from the first to the second class?

View Replies View Related

Passing A Value From Applet To JavaScript

Feb 5, 2014

I have a java application in which when starting it a map is opened and a screen is captured. The capturing is in certain time interval. I want to enable the user to change the interval for the automatic capturing the map. I placed a text box in a JPanel. I want to pass the value which was entered in it to the html page which opens a Google map. Since I am new in java programming, I found that I can do it by using applets. I made an applet, and tried to call it in the html file. But now instead of opening the map file in a browser, it opens very quickly only black window.

Here is my applet:

package cege.ui;
import java.applet.Applet;
import java.io.IOException;
import cege.ui.GuiMain;
public class AppletTInterval extends Applet {
private int TimeInterval=0;

[Code] .....

And the code in the html file where I try to call the applet, i.e. to pass the value which is returned by the applet is:

<script src=
<!--"https://www.java.com/js/deployJava.js"></script>
<script>
var attributes = { id:'AppletTInterval',
code:'cege.ui.AppletTInterval', width:1, height:1} ;
var parameters = { jnlp_href: 'AppletTInterval.jnlp'} ;

[Code] .....

Can I use this way to pass a value from the java class file (from the JTextField into the html file?

View Replies View Related

Passing Object As Parameter?

Aug 8, 2014

i want to pass an object of type Software to assign it to a computer from Computer class...

i should note that computer and software are arrays of objects thats the variable and method to set software in Computer class

private Software[] software;
public void setSoftware(Software software,int index){
this.software[index]=software;}

note: the user choses the a computer from a list and a software as will

for example the program will show the user 2 computers

0 for computer: apple, Model: mac mini, Speed: 2.8

1 for computer: sony, Model: vaio, Speed: 2.2

the user enters the index he wants then the program will show a list of software to add to the computer selected

the error I'm having is run time error Exception in thread "main" java.lang.NullPointerException and it points to these 2 lines

1.comp[Cch].setSoftware(software,Sch);

2. the method setSoftware

every thing is running correctly but this step above

Cch= the chosen computer index

Sch= the chosen Software index

why am i getting an error and how to fix it?

View Replies View Related

Passing In Variable From One Class To Another?

Aug 16, 2014

It just wont work... Everytime I try to pass a variable into a class it sets it to 0. What to do.

The variable should be '2', but it prints out as 0!?

Java Code: package Zoo;
import java.util.Random;
public class Animal {
Random random = new Random();
public void Eat(){
System.out.println("The animal starts to eat.");

[Code]...

The method bash, needs the variable 'playerTwo' to be 2, to carry on. But it prints out 0.

Int the animal constructor, I passed in the variable from another class, and it prints out 2 in the constructor like it should. But outside of the constructor it is equal to 0.

View Replies View Related

Getting Values From One Class To Another Without Passing Into It

Feb 19, 2015

I am trying to get the username and password for one class and send them to another. However I cannot send them in the constructor because a database class is getting them and I need that constructor blank. However if I do not pass the class in the constructor I get a NULL error, even better when I do pass it in I seem to get a thread lock. I do try to do my own housekeeping and close the one frame before I open a new one but it doesn't work. I will do my best to show the design of the program.

program starts
Java Code: public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// connects to database and generates a GUI
new UserPromptScreen();

[code]....

I have to pass in UserPromptScreen in for it to work. This will cause me issues later with my design.

View Replies View Related

JSF :: Passing Session To Different Machine

Aug 4, 2014

In my JSF application user starts on primary server where the session begins and then the user is redirected to a different server using sendRedirect. I want to pass some authentication token to the next server from primary server. I am trying to set session attribute as:

request.getSession().setAttribute("auth_token", "1");

And then send it to the next server as:

response.sendRedirect(encodedUrl);

But this attribute is not reaching the new server. I cannot pass this auth_token as request parameter as that wont be secure. So how to get some session data to new server?

View Replies View Related

Java Code For Passing By Value

Oct 7, 2014

you will be making four variables (2 primitive integers and 2 Account Objects). You will be following the steps detailed below. The challenge is to understand exactly what is happening and why. Start by coding the following two methods in a java file.

Start your main method with four variables: a, b c, d. a and by should be integer data, set them to be the values 10 and 20 respectively. Variables c and d should be Account Objects. The first account © is Ted, has $70,000 as his balance. The second account (d) is Joe, he has only $15,000 as his balance. Their account numbers are up to you to determine.Next, please print out the four variables: a, b, c, and d. Make sure that you label which ones are which. After you print these variables, please pass all four variables to a static method that you will create below. After you finish the method, please print out the four variables again: a, b, c, and d. Just as they were printed before the method.

Static methods: This method can be called whatever you want, however it will be public, static, and void. This method should take in four parameters: w and x which will be integers, then y and z which will be Account Objects.The first thing this method should do is to print out the four variables w, x, y, and z. This should be done in the same manner as you did in the main method. Next, change the first variable (w) to 15. After that, ted bought a new car. He now has only $20,000 in his account. Change the balance of y to be 20,000. Step three is to make a new Account Object named John, he has a balance of $10,000. Assign him to the variable z. Lastly, print all four variables again: w, x, y, and z.

View Replies View Related

Passing Values And Objects

Oct 28, 2014

Whether I pass primitives or objects the original value does not change. Is this expected result?

Java Code:

public class Class {
public static void main(String args[]) {
int x=3;
doItPrim(x);
System.out.println(x);//3
Integer i=new Integer(3);

[Code] ......

View Replies View Related

Java And Passing By Reference

Nov 12, 2014

I know in C++ It's possible to pass by reference but what about java?

For example, can i pass the address of a health variable into a Ninja class and then

Since Ninja inherits from an enemy class pass the health into the enemy class and within there have a function that returns that same health address and takes 5 from the health returning 95.

View Replies View Related

Passing More Than One Array In A Method?

Nov 3, 2014

Can we pass more than one array in a method?

View Replies View Related

Passing Objects To Constructors?

Mar 20, 2014

We are learning about how to pass objects into constructors. In our class, we made this following code:

public class InventoryItem {
//fields
private String description;
private int units;
//Add New constructor
public InventoryItem(InventoryItem some_object) {
description=some_object.description;

[Code] .....

As you can see the object of the same class is passed as the argument. Inside the constructor, our teacher did

some_object.description

This constructor, from my understanding, copies the description field and unit field to an object to the new object of class. Meanwhile, here is the demo class.

public static void main(String[] args)
{
//Create object
InventoryItem item1;
item1=new InventoryItem("hammer",20);
 System.out.println("Item 1: ");

[Code] .....

Over here, my teacher uses :

.getDescription

My problem is that in the constructor of the first class I showed, why didn't he use

.getDescription

method instead of this

.description

May I know what is difference between these two. If I use .getdescription that would return the value of a field from that object too.

View Replies View Related







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