So this I need to write a program which asks the user to enter any string, but the string has to have integers inside. I need to calculate the sum of those integers mentioned in the String. Also return 0 if there aren't any.
User input: I have a 91
Output: 10
I cant seem to find a way scan through the string to find the integers and add them.
String x1=input.next();
char digit= x1.charAt(3);}
public static int findDigitSum (String x)?
So I wrote a method that simply calculates the sum of all integers between 1 and a given integer n. The method works fine however, as n gets big the solution will have time and space problems. Some I'm just curious if there is a better method than my iterative one that would produce a better Big O value.
public static int sum(int n) { int total = 0; for (int i = 1; i <=n; i++) total += i; return total; }
I have to create a constructor with eight parameters containing both string and integers.the variables were supposed to be entered by user. but when I try to create an object of the class the IDE post error messages about the constructor.
public class hfiledriver { //the class name is hfile //after the main method I try creating an object of the class //after prompting the user to enter the data hfile hfile= new hfile(firstname, lastname, gender, age, weight, height); }
I am pretty new to Java and am just learning about two dimensional arrays. I think that I understand the concept, but I seem to be having trouble adding stuff to my array. I wanted to make an array to hold both strings and integers, but wasn't sure if I could put integers in a string array. So I thought that I would be able to convert my integers to string and then add them. This however causes an error. This is my code(yes its probably not the best):
static String [][] students = new String [14][4]; static int number = 0; String fName, lName, fullName; int test1, test2, test3, test4; String a, b, c, d;
double sum = a + b + c; System.out.printf("Sum = %d", sum);
Heres the error I'm getting
Enter three positive integers separated by spaces, then press enter: 15 20 9
Sum = Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Double at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source) at java.util.Formatter$FormatSpecifier.printInteger(Unknown Source) at java.util.Formatter$FormatSpecifier.print(Unknown Source) at java.util.Formatter.format(Unknown Source) at java.io.PrintStream.format(Unknown Source) at java.io.PrintStream.printf(Unknown Source) at project2.main(project2.java:52)
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(){
After the code is executed the array is supposed to contain 2,3,5,3,2. However, prime[4-0]= prime[i] and prime[4-1]= prime[i]... doesn't the loop terminate before it iterates a third time? Why are there five integers instead of only two?
import java.util.Arrays; import javax.swing.JOptionPane; public class Student { public static void main(String args[]) { String [] A =new String [4]; //krijon tabelen e emrave
[Code] ....
It Prints [firstname,secondname,thirdname,fourthname]
and [0,0,7,9]
The problem is that the first 2 integers of the second array are always 0 even if i put another grade like for example 6 or 7...
Our teacher asked to input the names and grades of the students using JOptionPane and then find the MINIMUM grade and how many times this grade is repeated...
Create a loop where you add the integers from 1 to 50.
public class Sum50 { public static void main(String[] args) { int sum = 0; int max = 50; for(int i = 0;i <= max; i++){ sum=sum+i; } System.out.println("Sum is " + sum); } }
I need to write a class in that uses the For loop and does the following things: asks user to input two integers, the second larger than the first. Next, use a for loop to sum the numbers between the two integers, including the original integer. For example: 5&8 would be 5+6+7+8 and lastly prints out the sum of this.
I have successfully been able to do the first part but when it comes to the For Loop I am a bit lost here is what I have so far
import java.util.Scanner; public class Question3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Please enter an integer"); int num1 =sc.nextInt(); System.out.println("please enter a larger integer"); int num2=sc.nextInt(); int sum=0; for(int i=num1; i<=num2;i++); } }
I have this code running correctly in Eclipse all except that it seems no matter where I declare, highest, lowest, average they always seem to stay at "0". I have a feeling the location of the Initialization is not the error since I have tried moving it to inside differ loops.
Stipulations on writing this code are:
Note: You do not need to use array to save each input score for this lab. A Single loop to read in each grade, compare with the current highest grade, lowest grade, and calculate the running sum at the same time.
import java.util.Scanner;
/*Write a program that prompts the user to enter the total number of students first. *Then ask the user to input each student’s grade and use loop statements to read in each grade. *Check input grade to make sure 0<=grade<=100, if the user input any other number, print out warning message and ask the user to input a new grade. Display the highest score, the lowest score and the average. */
public class LoopStatements { // Main Method public static void main(String[] args) { // Initialize int grade = 0; // grade value
my code is below and i want to read only integers.BUT the text file is starting with text and it stops executing without reading the numbers.Also i want to add the 3 parameters of each line.
try{ File fl = new File("C:/Users/Mario/Desktop/testing.txt"); //BufferedReader rd = new BufferedReader(new FileReader(fl)); Scanner sc = new Scanner(fl).useDelimiter("s+"); LinkedList<Integer> temps = new LinkedList<>(); sc.useDelimiter(System.getProperty("line.separator")); while(sc.hasNext()){
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(port); if (portIdentifier.isCurrentlyOwned()) { System.out.println("Error: Port is currently in use"); } else { CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
[Code].....
To set the serial port parameters i need to use Integers instead of strings.
I am trying to write a method that returns the busiest hour in a logAnalyzer class that read web server data and analyze hourly access patterns and stores them in an array. My problem is, in order to get the busiest hour, I need to go through the hourCounts array to find the element with the biggest count.
Develop a method that accepts as data input three integer numbers, calculates the sum and the product of the numbers, and displays the sum and product.
1. Prompt the user to input two positive integers: firstNum and secondNum (firstNum must be smaller than secondNum). 2. Output all the even numbers between firstNum and secondNum inclusive. 3. Output the sum of all the even numbers between firstNum and secondNum inclusive. 4. Output all the odd numbers between firstNum and secondNum inclusive. 5. Output the sum of all the odd numbers between firstNum and secondNum inclusive.
*Use while loop int firstNum, secondNum; Scanner keyboard = new Scanner(System.in); System.out.println("Enter an integer: "); firstNum = keyboard.nextInt();
[Code] ....
What to do with the while loop and how to find even and odd numbers.
I have a method that receives an array of integers and saves the ID of the user inside the array of integers and I keep getting the error: "int[] cannot be converted to int".Here is the code:
public boolean Borrow (String TitleOrAuthor,int id){ int count = 0; int b1 = 0; int BookCount [] = new int [b1]; for (int i=0;i<Bcount;i++){ if(Booklist[i].getTitle().equals(TitleOrAuthor)) for(int j=0;j<b1;j++){ BookCount [j]= Booklist[i].getCopies(); BookCount [j]= id; b1++;
import java.text.DecimalFormat; public class Money { //Fields for money will hold dollars and cents private long dollars; private long cents;
My task is to use those fields and make a toString method that returns them like a dollars sign. For instance, if there are 32 dollars and 40 cents, then in my String method I have to return something similar to this "$32.40."
I have already tried some of the methods, but they don't seem to work fine.
I have a code like following. x and y are both Integers and have same values (e.g. 5). But they are interpreted as different values. Then, it validates the following condition.
Java Code: if (x != y) { "x and y are different..." } mh_sh_highlight_all('java');
I am workinh with a couple of functions that deal with two dimensional (square) arrays of integers, doing things like checking equality, etc. For example, I know that I get an ArrayOutOfBoundsException in the isEqual function, but I don't know why.
public class MatrixUtils { //This function checks if two matrices are equal public static boolean isEqual(int A[][], int B[][]) { for(int i=0; i<A.length; i++) { for(int j=0; j<A.length; i++) { if (A[i][j] != B[i][j]) return false;
I have devised a simple program that reads a file and then adds up al the integers in the file and print the result, for example if the file had the numbers 1 2 3 4 5 6 7 8 9 10 then the program would print 55
However i have trouble when non integers are put into the file for example if it was 1 2 3 string 4 5 6 test 7 8 9 10
then i get:
Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:840) at java.util.Scanner.next(Scanner.java:1461) at java.util.Scanner.nextInt(Scanner.java:2091) at java.util.Scanner.nextInt(Scanner.java:2050) at Week7.Task3.filereader(Task3.java:25) at Week7.Task3.main(Task3.java:14)
my code is as follows
package testing;
import java.util.*; import java.io.File; import java.io.IOException; public class summingInts { public static void main(String[] args) throws IOException { Scanner textfile = new Scanner(new File("intSum.txt"));