Error In Program To Read Int Or Float Type Data From Keyboard

Aug 9, 2014

Here is a code and error .....

View Replies


ADVERTISEMENT

Compile Time Error - Cannot Invoke Read On Primitive Data Type Int

Jul 10, 2014

I 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?

View Replies View Related

Compilation Error Type Mismatch - Cannot Convert From Double To Float

Apr 20, 2014

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;

View Replies View Related

Run Time Error In Reading Data From Keyboard

Aug 9, 2014

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 Related

Using Boolean Data Type In Program

Sep 25, 2014

While 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.

View Replies View Related

Invalid Type And Syntax Error For Translator Program

Apr 5, 2015

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] .....

View Replies View Related

Read Float Numbers From A File / Put Them In Array And Sort

Jun 30, 2014

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[ ....

View Replies View Related

Read Integer From Keyboard Until Negative Number Is Entered

Apr 8, 2014

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] ....

View Replies View Related

Read A Character From Keyboard - Illegal Start Of Expression

Jul 1, 2014

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);
}
}
}

View Replies View Related

Hurricane Java Project - Read Information From Data File And Run It Through But Program

Dec 2, 2014

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.

View Replies View Related

Error - Invalid Float Literal

Apr 26, 2014

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] ....

View Replies View Related

Create A Java Program That Can Read Specific Data Within Csv File To Work Out Averages

Aug 7, 2014

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.

View Replies View Related

Scanner Class Error For Decimal Values (double / Float)

Apr 23, 2014

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.)

View Replies View Related

Error Cannot Find Symbol - Variable Keyboard

Sep 14, 2014

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] ....

View Replies View Related

Insert Data From Keyboard And Check Whether Any Of Constructor Fields Remained Empty

May 12, 2015

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?

View Replies View Related

Program To Accept String Float And Integer As Command Line Arguments

Feb 8, 2015

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 Related

Null Is What Type Of Data Type

Sep 3, 2007

method passing null to the called function, compiler would take that parmer as what type of paramer???

View Replies View Related

Make A Program Which Take Input From Keyboard And Count Numbers

Sep 12, 2014

import 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.

View Replies View Related

Write A Java Program Which Display To Get GPA Of A Student As Keyboard Input

Feb 15, 2015

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.

View Replies View Related

Retrieving User Input String From Keyboard - Program Errors (try / Catch)

Sep 23, 2014

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?

View Replies View Related

User Inputs His Full Name In A String From Keyboard / Program Will Give His Initials As Output

Jan 5, 2014

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] ....

View Replies View Related

Data Type That Can Store Methods

Mar 21, 2015

I was wondering is there any data type that can store method?

View Replies View Related

Get Wrong Type Error When Calling Methods

Apr 21, 2015

Basically 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] ....

View Replies View Related

NewExcel Cannot Be Resolved Into A Type Error Message

May 5, 2014

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] .....

View Replies View Related

Creating Array With Multiple Data Type?

Oct 10, 2014

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

View Replies View Related

Passing Primitive Data Type Arguments

Jun 5, 2014

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] ....

View Replies View Related







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