When I input "the" or "and" I always receive "its not here" as the output. Why is this? I also tried printing the values of v.get(i).other and the system printed null twice.
public class Translator {
public String other;
public Translator(String x) {
x = other;
While reading head first java i encountered a problem(Pg. 90 chapter 4 - mixed messages).
Suppose in a class(say A) outside main() a counter variable is declared and initialized to 0.
In main() declared the array of objects of the class A.
Consider a while loop in which we increment the counter as follows:
public class A{ int counter = 0; public static void main(String[] args){ A[] arr = new A[20]; int x = 0; while(x<4){ arr[x] = new A(); //arr[] is array object arr[x].counter += 1; x++; } } };
what is the final value of counter ? will it be the same for all array objects.
I am having some problem accessing variables from an array instance of a class. Heres what i have done;
In the main class:
Example obj[]= new Example[4];
In the main class constructor:
obj[0] = new Example(0); obj[1] = new Example(1); obj[2] = new Example(2); obj[3] = new Example(3);
In the main update() method:
if(condition) //update
In the Example class constructor:
private boolean change = false;
In the Example class update() method:
if(x >20) change= true;
Now, i want to access the variable change from the main class, how do i do it? The 'condition' in the if statement is the condition of wether the change variable ia true or false. How do i access it?
Suppose I have private static void name() { ... } that has a two dimensional string array named array[. Now suppose I have private static void different() {...} and I want to write a condition where if (item == array) { ... }, how can I access my array from name() when I am in different()? I get a compile error saying cannot find symbol. My code is similar to:
I'm having some issues, trying to solve this problem in java. I want to print some election results, and i have to loop through a vector of objects and retrieve the partial sums of each party's seats for each constituency and the national results for each party. For now i can print the results per contituency, but i'm having problems in getting the national results. Like, adding the seats for labour party in Constituency A and B and C, etc, and print the sum. And do the same for conservative party.
This is what i have.
Java Code:
while (i < h.geral.size()) { show += "Constituency - " + ((Party) h.geral.elementAt(i)).getConstituency() + "
I understand how vectors work I'm currently using one to store my id's from my txt file but how do you put them in a defaultComboBoxModel?
DefaultComboBoxModel defaultComboBoxModel = new DefaultComboBoxModel(); //declare a vector object that will hold a string (only thing that works with comboboxmodel Vector<String> myVector=new Vector<String>(); //try statement try{ FileReader fr = new FileReader(file);
[Code]...
Any example of how a vector is used with a defaultComboBoxModel so I can then use that to populate my JComboBox?
I'm having extreme difficulty in working with a Vector storing a column with a BigDecimal value, and converting that single value into a float. I'm not sure why the code was written this way, but basically, I'm working with something called a vector that has a single Big Decimal value/column (not sure what the correct terminology is), and I want to store that value in a float variable called "dp". However, I don't know how to convert from the Big Decimal to a float.
Code is below:
String s = ""; sql = "SELECT DiscountPercentRate FROM Attendees WHERE AttendeeId=" + attendeeId; Vector v2 = sqldb.getResults(sql); /*I know that sqldb.getResults(sql) returns a vector with a single BigDecimal column of 15.0, in the test example I'm using*/ if (!v2.isEmpty()) { Vector data2 = (Vector)v2.elementAt(0); if (!data2.isEmpty())
[Code] .....
In case you're wondering what the sqldb.getResults() method looks, like, here's a snippet of it - There's an else statement that triggers in my case, adding a BigDecimal column to a vector, and returns that vector.
Vector v = new Vector(); ... else { BigDecimal bd = rs.getBigDecimal(i); vCols.add(bd); } v.add(vCols); ... return v;
How can I take the single result of my SQL query in that Vector/Big Decimal thing, and turn that result into a float value?
I have a project where I am required to read and write a vector of bank account objects and I am struggling with this concept. I was able to figure it out when the accounts were not stored in a vector but now that I am dealing with vectorsThis is my best attempt. Even though I know it's wrong, what I am trying to do.write/read methods in main:
public static void readTrans() { textArea.setText(""); chooseFile(1); try { FileInputStream fis = new FileInputStream(filename); ObjectInputStream in = new ObjectInputStream(fis); for (int index=0; index != fileIndex; index++)
im trying to create an insertion sort method for a vector. I know how to insertionsort for an array, but for a vector im having problems
Source code: PHP Code: package test; import java.util.*; import java.io.*; public class LinearSearch { public static void main (String[] args) { Vector myVector = new Vector();
[Code]...
I'm getting errors at lines 38 and 39 "Left-hand side of an assignment must be a variable". "Syntax-error(s) on token(s) misplaces contructor(s)". How can i fix them ??
class GVector { // TODO: declare a private array to save the vector coordinates // Creates a mathematical vector of d dimensions, initialized at 0 public GVector(int d) { // TODO: implementation
[Code] ....
I'm confused with what type of array I need to use to save the vector coordinates and what to put in Gvector. Is it a constructor?
I'm playing with vectors for the first time... What I'm trying to do is to allow a user to input one or more integers and store them in a vector for manipulation later on in the program... Here's the portion of the program I'm working with:
Java Code:
package com.itse2317; import java.util.*; public class VectorTest { public static void main(String[] args) { Scanner input = new Scanner(System.in);
[code]...
My question is this: Is there any way to move from inputting integers to printing them, without entering a non-integer (for example, hitting enter)? I looked at the API for the Vector class, and either I'm not thinking about the problem the right way to be able to find an answer, or it's just not there.
I am running into some trouble with Line-Plane intersection, my method works, provided I am perpendicular to the plane I want to intersect. This is caused be the plane equation (with my given x, y, and z coordinates, the equation is -Z - 1 = 0). So the equation is only relying on the Z value of anything inputted. Is there another way to calculate the equation of a plane that would rely on all axis? Here is my current code:
Java Code:
Vector3f A = new Vector3f(0, 0, -1); Vector3f B = new Vector3f(0, 1, -1); Vector3f C = new Vector3f(1, 1, -1);
why does sevenPointsOrLess only work if I declare it as a String and use next() instead of nextInt()? Also why does it take part of the next line with it?
Here is the text I am trying to scan:
England,3,0,1,2,0 Samoa,1,0,3,0,1
Code:
lineScanner.useDelimiter(","); country = lineScanner.next(); Won = lineScanner.nextInt(); Drawn = lineScanner.nextInt(); Lost = lineScanner.nextInt(); fourOrMoreTries = lineScanner.nextInt(); sevenPointsOrLess = lineScanner.next();
Results:
country = "England" Won = "3" Drawn = "0" Lost = "2" fourOrMoreTries = "2" sevenPointsOrLess = "0 Samoa"
I'm trying to write a basic java program so that when I run it, I can store the names and codes of some of my school textbooks...I try the scanner statement to input the name of the book but an exception is thrown when I type the name of the book..I have attached the code for my program.
import java.util.Scanner; public class Books{ public static void main(String[] args) { Scanner s=new Scanner(System.in); System.out.print("Enter amount of products : "); int products= s.nextInt(); long [] code= new long[products]; String[] names = new String[products]; for (int x=1;x<(products+1);x++){ System.out.println("Enter IMEI number "+x); code[x] = s.nextLong(); System.out.println("Enter name "+x);
I had to use scanner to receive input from the user for a formula and everything compiled and worked correctly except when I entered a double into the Scanner, it only accepts ints and gives me this error every time I enter a double:
java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextDouble(Unknown Source) at TestA1Q2.main(TestA1Q2.java:36) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
I know that you usually get that error when the Scanner expects something else that is different from what you type and I have looked over my code time and time again and cannot find anything. I created a test program to see if it was just that problem and not something else in the code and I still got the same error but when I tried compiling and running it here browxy.com everything worked and I got the right answer for the formula. Here's the test I used:
import java.util.Scanner; public class TestA1Q2 { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.println("Enter a double"); double velocity = keyboard.nextDouble(); } }
At this point I'm thinking it might be a problem with my install of DrJava and I've tried re-installing but nothing changed.
I created a variable for the scanner called serena. Serena variable is equal to what the user inputs. My if statement says that if the answer the user enters is not equal to the actual answer then it is to display "wrong". It is a basic math game I am working on. NetBeans is telling me that I cannot use the scanner in an if statement?
package pkgnew; import java.util.Scanner; public class New{ public static void main(String args[]){ Scanner serena = new Scanner(System.in); double fnum, snum, answer;
[Code] ....
Do I have to define Serena as whatever number the user inputs? If so, how?
Scanner sc = new Scanner(System.in); Question question = new Question(); Quiz quiz = new Quiz(); System.out.print("Enter the prompt: "); question.prompt = sc.nextLine();
[Code] .....
Here is the example run that I am trying to achieve:
* 1. Enter the prompt: What is the capital of the USA? * 2. Enter a possible answer: New York City, NY * 2. Is New York City, NY the correct answer (y/n)? n * 3. Enter a possible answer: Pittsburgh, PA * 3. Is Pittsburgh, PA the correct answer (y/n)? n * 4. Enter a possible answer: Washington, DC * 4. Is Washington, DC the correct answer (y/n)? y * 5. Enter a possible answer: Chicago, IL * 5. Is Chicago, IL the correct answer (y/n)? n
The problem is that my for loop runs through correctly the first time, but doesn't seem to react to me initializing my choices array after that. It ends up looking like this:
Enter the prompt: What is the capitol of the USA? Enter a possible answer: New York City, NY Is New York City, NY the correct answer (y/n)?n Enter a possible answer: Is the correct answer (y/n)?
I have a small bug in my program. The user is asked what person(s) information they want to access but lets say they want captain they must enter "captain" twice. I think it will make more sense to you with the code. I have searched all over to see what is causing the bug but still have found no resolution. I even tried making two different scanners but that didn't work either.
I know the while loop (line 16) I am using is causing the bug because it works fine without that but then I cannot validate the input.
package myproject; import java.util.Scanner; public class Enterprise { public static void main (String[]args){ String userInput;