JAVA: Rewrite the following code to where the inputs are from a file. The file name will be from the input from the user scanner.
CODE:
import java.util.*;
public class Q1
{
public static int sumOD (int k)
{
int sumOD = 0;
int in = k;
while (in != 0)
{
int digitON = in % 10;
sumOD += digitON;
in /= 10;
}
return sumOD;
}
public static void main(String[] args)
{
int n, i, k;
System.out.println("Enter the number of integers: ");
Scanner keyboard = new Scanner(System.in);
n = keyboard.nextInt();
for (i = 0; i < n; i++)
{
System.out.println("Enter the number: ");
Scanner keyboard2 = new Scanner(System.in);
k = keyboard2.nextInt();
if (k < 0)
System.out.println("The integer is negative.");
else
System.out.println("The sum of digits is: " + sumOD(k));
}
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class Q1 {
public static int sumOD(int k) {
int sumOD = 0;
int in = k;
while (in != 0) {
int digitON = in % 10;
sumOD += digitON;
in /= 10;
}
return sumOD;
}
public static void main(String[] args) {
int n, i, k;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter file name: ");
File file = new File(keyboard.nextLine());
try {
Scanner fin = new Scanner(file);
n = fin.nextInt();
for (i = 0; i < n; i++) {
k = fin.nextInt();
if (k < 0)
System.out.println("The integer is negative.");
else
System.out.println("The sum of digits is: " + sumOD(k));
}
fin.close();
} catch (FileNotFoundException e) {
System.out.println(file.getAbsolutePath() + " is not found!");
}
}
}
JAVA: Rewrite the following code to where the inputs are from a file. The file name...
Please write where its written write your code here!! please use java for the code! please use the given code and I will rate thanks Magic index in an array a[1..n] is defined to be an index such that a[ i ] = i. Given an array of integers, write a recursive method to find the first magic index from left to right. If one exists in the given array, return the index number i, otherwise return -1. Here are...
What would be the output if 1, 2, 3, and -1 were entered and please walk me through the process. import java.util.Scanner; public class Practice1 { public static void main(String[] args) { int count = 0; int sum = 0; Scanner keyboard = new Scanner(System.in); System.out.println("Enter integers or -1 to quit"); int anInt = keyboard.nextInt(); while(anInt != -1) { anInt = keyboard.nextInt(); sum += anInt; count++; } anInt = keyboard.nextInt(); sum += anInt; count++; } }
composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main { public static void main(String[] args) { System.out.print("Enter the...
Please explain if the following code is actually correct. If the following code correct, please explain why the code works and is also correct. Don’t use * Java’s Integer .toBinaryString(int) in this program./* According to the textbook and the instructor, I am not supposed to use arrays such as int binary[] = new int[25]; I guess this is the reason why this problem is starting to look kind of hard. Chapter 5 Exercise 37: Java Programming * * (Decimal to...
This java code won't run and I can't figure out the problem with it. Please help import java.io.*; import java.util.*; import java.math.*; import java.util.Scanner; public class Fibonacci { // Returns n-th Fibonacci number static BigInteger fib(int n) { BigInteger[] Fibo = new BigInteger[n+2]; Fibo[0] = BigInteger.ZERO; Fibo[1] = BigInteger.ONE; for (int j=2 ; j<=n ; j++) { Fibo[j] = Fibo[j-1].add(Fibo[j-2]); } return (Fibo[n]); }...
import java.util.Scanner; public class TriangleMaker { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle."); Scanner keyboard = new Scanner(System.in); int size = keyboard.nextInt(); for (int i = 1; i <= size; i++) { for (int j = 0; j < i; j++) { System.out.print("*"); } System.out.println(); } for (int...
In the code shown above are
two parts of two different exercises. HOW WE CAN HAVE BOTH OF THEM
ON THE SAME CLASS BUT SO WE CAN RECALL them threw different
methods. Thank you.
1-First exercise
import java.util.Scanner;
public class first {
public static int average(int[] array){
int total = 0;
for(int x: array)
total += x;
return total / array.length;
}
public static double average(double[] array){
double total = 0;
for(double x: array)
total += x;
return total /...
Provide comments for this code explaining what each line of code does import java.util.*; public class Chpt8_Project { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int [][]courses=new int [10][2]; for(int i=0;i<10;i++) { while(true) { System.out.print("Enter classes and graduation year for student "+(i+1)+":"); courses[i][0]=sc.nextInt(); courses[i][1]=sc.nextInt(); if(courses[i][0]>=1...
Q: Create a version of the UnitConverter application to convert from inches to foot. Read the inches value from the user. A: import java.util.*; class UnitConverter{ public static void main(String[] args){ int inches; int foot; Scanner scan=new Scanner(System.in); System.out.println("Enter inches:"); inches=scan.nextInt(); System.out.println(inches/12+" feet and "+inches%12+" inches."); } } Q: Write a program that converts grams to pounds. (One pound equals 453.592 grams.) Read the grams value from the user as a floating point value. A: import java.util.*; class GramsToPounds{...
JAVA Can you make this return the first letter of first and last name capitalized? import java.io.File; import java.io.IOException; import java.util.*; public class M1 { public static void main(String[] args) { //scanner input from user Scanner scanner = new Scanner(System.in); // System.out.print("Please enter your full name: "); String fullname = scanner.nextLine(); //creating a for loop to call first and last name separately int i,k=0; String first="",last=""; for(i=0;i<fullname.length();i++) { char j=fullname.charAt(i); if(j==' ') { k=i; break; } first=first+j;...