Error In Program To Read Int Or Float Type Data From Keyboard
Aug 9, 2014Here is a code and error .....
View RepliesHere is a code and error .....
View RepliesI have a code in which I am reading input from System.in and Destination is some where else
Here is my code
File file=new File("D:/output.txt");
OutputStream os=new java.io.FileOutputStream(file);
Scanner scanner=new Scanner(System.in);
System.out.println("Enter Data to write on File");
String text=scanner.nextLine();
int c=Integer.parseInt(text);
int a;
while((a=c.read())!=-1)
os.write(a);
System.out.println("File Written is Successful");
In the line while((a=c.read())!=-1)
a compile time error is shown "cannot invoke read on primitive data type int"
Where I am going wrong?
I have a simple doubt
float k = 0;
k+=0.2;
k=k+0.2; // here compilation error
compliation error Type mismatch: cannot convert from double to float
My question is why not a complilation error at k+=0.2;
i tried to run simple program od adding two number by using scanner class but there is run time error here it is code file and error.
View Replies View RelatedWhile practicing java i have came across boolean data type. i have executed below program but i am not sure how i got output of the program like below:
10 > 9 is true
Here is my program:
class BoolTest3
{
public static void main(String args[])
{
System.out.println("10 > 9 is "+(10>9));
}
}
My question is: how this program will come to know that 10>9 true or false.. how bolean data type used in this program.
Getting the following error at line 13.
Multiple markers at this line - void is an invalid type for the variable loadDictionaryFromFile - Syntax error on token ")", ; expected - Syntax error on token "(", ; expected
Line 13 starts public void load....
Code:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class PirateTranslator
{
public static void main(String[] args)
[Code] .....
I'm supposed to write a program, which reads float numbers from a file, puts them in an array and sorts the array. After that I'm suppose to add the numbers so that when I add the 1 and 2 number of the array, I'm suppose to save the sum on the position of number 1, then I add number 3 and 4 and save the sum on position 2 etc. Also if my array has an uneven number of floats it's suppose to add the last 3 and not 2 numbers in the last iteration. The problem is the method throws an ArrayOutOfBounds Exception but I can't seem to find my mistake.
That's the second method. The first one just stores the sum in another variable and then returns it. Also is there a way in that I can Scanner/File/array etc. and initialize the array only once so I don't write the same code two times like it is now.
package sumOfFloats;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
public class SumOfFloats {
public static float sumFloats() throws FileNotFoundException{
[Code[ ....
Basically i have a question ask me to write a program that read integer from keyboard until a negative number is entered, and then print out the max and min out of the number i have entered.
This is what i have done already which works, but i need to ignore the negative number when print out max and min...which i dont know how to do so. when i compile this program now it will always give me the negative number i enter at the end as minimum. which i need to ignore
Also if negative number is entered at once the program should print a error message say max and min are undefined.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int large = Integer.MIN_VALUE;
int small = Integer.MAX_VALUE;
while(true) {
System.out.print("Enter a integer: ");
int n = in.nextInt();
[Code] ....
I need to know why this program is giving me error (illegal start of expression). What must i do to stop the error?
//Read a character from keyboard
class Kbin{
public static void main(String[]args){
throws java.io.IOException{
char ch;
System.out.print( " Press a key followed by ENTER: ");
ch = (char) System.in.read();//read character from keyboard
System.out.println( "Your key is: " + ch);
}
}
}
I am now stuck. I am writing a program that reads information from a data file and runs it through but program. However, I am almost finished with the program, but cannot figure out the last few parts.
public class Storm {
private final double KnotsToMPH = 1.15;
// global user-defined types:
private int beginDate;
private int duration;
private String name;
[Code] .....
I have not attached the data file because it contains a total of 360 lines of hurricane information.
While doing trial and error got caught in the below scenario.
public class Crypt {
public static void main (String args[])
{
/*all I want is calculate a binary number (ex -: 22 , 34) using decimal base (10n).
*So, I have to convert 2 p into 10n form so I have to find n in terms of p . We have x as the input.
* The formula works as below.
*2p =10n
*p ln (2) =n ln (10)
*n = p [ln(2) / ln(10)]
*2 p = 10 p [ln(2) / ln(10)]
[Code] ....
i am fairly new to java but have made a few projects so i do know most aspects that would allow me to complete the task its just i have been stuck at the same place now for 2 days.The project is to create a java program that can read specific data within a csv file to work out the averages etc. The csv file is a database of different weather stats . an example of the first 12 months of the csv file is below..
[CSV FILE]
year ,month,average rainfall etc
1946 15.70.616108.131.5
1946 28.23.56111.871
1946 38.82932.9 102.9
1946 414.16.3029.2 150.5
1946 513.96060.7 143.6
[code]...
I know that once the data is indexed i can than use a double to find the average of the 12 specific pieces of data and so on. once this is complete i should than be able to transfer the data to a simple graph.
I'm not getting why my program is giving error.
package rockjava;
import java.text.NumberFormat;
import java.util.Scanner;
public class file4 {
public static void main(String[] args)
[Code] ....
Please Input Num of Pods + Num of Keys= 20.5
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextFloat(Unknown Source)
at alijava.file4.main(file4.java:14)
(It is not accepting double values. However working fine for integers.)
I am trying to create this program I am pretty sure it is easy but I am making it difficult lol, it keeps giving me a error, it is saying cannot find symbol - variable keyboard, I don't think I have keyboard as a variable but I may be wrong.
double distancel = keyboard.nextdouble(); that is the specific line ....
import javax.swing.JOptionPane;
import java.io.File;
import java.util.Scanner;
//import java.util.totalInches;
//instance variables
public class Map{
public static void main(String[] args){
[Code] ....
I'm trying to use in the constructor inserts from keyboard and then check whether any of the constructors fields remained empty. Let say my constructor is:
public SomeClass(String a, String b, String c)throws SomeClassException{
if (a == null || b == null || c == null) {throw new SomeClassException("Some message"); // here is the empty test
} //...
Thus I prepared method: insertConstruct() as follows:
public static String insertConstruct(){
String lettering = null;
Scanner sc = new Scanner(System.in);
lettering= sc.nextLine();
return lettering;
}
And during creating the new object in some main method I call:
// ...
ClassName obj1;
obj1 = new ClassName(insertConstruct(),insertConstruct(),insertConstruct());
//...
The problem is not with inserting data but with leaving the field empty. When I pass any field just with keyboards Enter, the empty test doesnt return me any message about it. While it works if I build object with the constructor like:
// ...
className obj1;
obj1 = new ClassName("word a",null,"word c");
//...
What might case this failure?
Write a java program to accept a string, float and an integer as command line arguments. String value should be displayed. Also the program should display the maximum value of two numbers. Use Integer.parseInt() and Float.parseFloat()
View Replies View Relatedmethod passing null to the called function, compiler would take that parmer as what type of paramer???
View Replies View Relatedimport java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number;
System.out.println("Input a number");
[code]....
What i want to do is that if the number is greater than 50 to count until 100. else if the number is less than fifty to count for example from 36 to zero.
I want to write a java program which displays to get the GPA of a student as a keyboard input. Then display the class of the degree according to the following criteria using a switch-case statement (if-else statements cannot be used). If the user insert an invalid gpa you should display a message Invalid GPA.
gpa ≥ 3.50 First Class Hons
3.49 ≥ gpa ≥ 3.00 Upper Second Class Hons
2.99 ≥ gpa ≥ 2.50 Lower Second Class Hons
2.49 ≥ gpa ≥ 2.00 Pass
2.00 ≥ gpa Fail
Here is my code:
import java.io.*;
public class q7
{
public static void main(String[] args)
{
InputStreamReader ISR=new InputStreamReader(System.in);
BufferedReader BR=new BufferedReader(ISR);
[code]...
.But when I use Command console to run this it says: 1111.jpg. I also wanna know is there another way to do this with switch- case statements.
I need to design, implement, and test a program to input and analyze a name. The program begins by retrieving a user input string from the keyboard. This string is intended to be the user's name. These are the errors we have to search analyze the user input for: No blanks between names firstName and lastName, Non-alphabetic characters in names, Less than two characters in first name, and Less than two characters in last name. Each of these errors must be thrown. All exceptions must be derived from a programmer-defined class called NameException. Each exception should use a detailed message to differentiate among the file types of errors.
This is the format of my NameException class, is the format itself correct? I will fill in the details of each exception I am just wondering if that is how I should set it up.
public class NameException extends Exception {
private String firstName, lastName;
public NoBlanksException(String firstName,String lastName) {
}
public NonAlphabeticalCharactersException(String firstName, String lastName) {
[Code] .....
I was told not to try and catch thrown errors in the main method, would I just create another method in the Driver class to take care of that then?
Following is the code for my assignment where a user inputs his full name in a string from keyboard. It should be done using BufferedReader. The program will give his initials as output.
############################
For example: User inputs-> Ram Kumar Das
Program Returns-> R.K.D.
I am written the following piece of code. It does not return the correct output. If "Ram Kumar Das" is given as input, it gives R.K.K. as output. Arrays and objects are strictly not allowed.
Java Code:
import java.io.*;
public class Assignment
{
static int i=0;
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main (String args[]) throws IOException
[Code] ....
I was wondering is there any data type that can store method?
View Replies View RelatedBasically I'm trying to code this program but I keep getting error can't be applied to given types. I know it has to do with my method trying to be called by an array, but I'm just kinda lost.
Write a program that prompts the user to input cash amounts and use a method to calculate the average amount for a week given 7 inputs of daily cash intake amounts for a cash register. Use an array to store the values. Recall that average is the sum of the values divided by the number of values. The method should take argument(s) of type double and return average weekly cash amount as a double.
Make sure you test your program using the test data given below and calls the averageCash() method defined above passing in the values. Verify that the input values are greater than or equal to 0. Negative numbers are not allowed. Output the values used and the averageCash value.
import java.util.*;
public class ArrayHandout {
public static void main(String args[]) {
int[] a=new int[6];
Scanner sc=new Scanner(System.in);
[Code] ....
I am getting the error message as Exception in thread "main" java.lang.Error: Unresolved compilation problems: NewExcel cannot be resolved to a type for the line NewExcel test = new NewExcel(); in the below code to read the columns from an excelsheet "test.xls". Why I am getting the error message :-
import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class ReadExcel
[Code] .....
i need to create an array with attributes name, gender, phone, age.and then sort acording to age in ascending order.
i created like this,
public class Array{
private String[] name={"ram", "katy", "priti", "john"};
private String[] gender={"male","female","female","male"};
private int[] phone={989898089,89898989,8982989089,898908989};
private int[] age={45,24,30,28};
public void printarray(){
[code]....
This code sorts the age attribute alone and when printing ouput it swaps one person's age to other person, how to make it correct
I am totally new to Java. What is the purpose of this method?
Flow of the int x=3; like where does the 3 go step by step?
Passing Primitive Data Type Arguments (from oracle java tutorials)
Primitive arguments, such as an int or a double, are passed into methods by value. This means that any changes to the values of the parameters exist only within the scope of the method. When the method returns, the parameters are gone and any changes to them are lost. Here is an example:
public class PassPrimitiveByValue {
public static void main(String[] args) {
int x = 3;
// invoke passMethod() with
// x as argument
passMethod(x);
[Code] ....