Reverse Number - Get Input And Result In Same Line

Feb 11, 2015

I'm having trouble with a program, reversing numbers. I got the program itself to work! However, I am having an issue with the output.

import java.util.Scanner;
public class p1a {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int n;
int reverse = 0;

[Code] .....

If I were to input, say 2341, I would get this: The reverse of 0 is 1432

The 0 should have been 2341, or whatever I input. What am I doing wrong?

I tried to move line 21 : (System.out.println("The reverse of " +n+ " is "+reverse);

Above the while loop, but instead get:

The reverse of 2341 is 0

How can I get my input and my result together in the same line?

View Replies


ADVERTISEMENT

Write A Program To Reverse Each Word In Line (sentence)

Jul 3, 2014

Write a program to reverse each word in line(sentence)?

View Replies View Related

How To Make File Reader Object Move To Next Line If There Is No More Input On Line

Feb 18, 2015

How do I make the file reader object move to the next line if there is no more input on the line. Here is my text and output file as you can see that my text file column cuts off on the 2nd line after 70. I want to read that next line which is 100 into my labs variable however its reading it into my final exams variable. I'll also post the code but I didn't think it was necessary.

textfile:
100908095
1008070
10070

output:
Labs Projects Tests Final Exams
100908095
1008070100
70

[import java.util.Scanner;
import java.io.*;
public class MyGrades
{
public static void main (String[] args) throws IOException
{
int lab, project, test;
int finalExam;//Par and Player values

[Code]...

not sure if I code wrapped it correctly

View Replies View Related

I/O / Streams :: Find Line Number In A File Using Multi Line String

May 5, 2014

My requirement is to find the line number using multiline string. Here I need to extract the string between FROM and where clause(from the below string) and need to find the line number in the file

SELECT HL.LOCATION_ID,HPS.PARTY_SITE_ID,HCAS.CUST_ACCT_SITE_ID
INTO LN_SITE_LOCATION_ID,LN_LOC_PARTY_SITE_ID,LN_CUST_ACCT_SITE_ID
FROM HZ_LOCATIONS HL,
HZ_PARTY_SITES HPS,

[Code]....

View Replies View Related

Floyd Triangle - Accept Line Number From User And Print Only That Particular Line

Sep 10, 2014

Write a program that accepts the line number from the user and prints only that particular line from the Floyd triangle.

Example:
Input: 2
Output: 2 3

Input: 3
Output: 4 5 6

View Replies View Related

Scanning Next Line Of Input Without Advancing Past Line

Apr 12, 2014

I'm doing an assignment for uni and have come across a small hiccup. What I'm trying to do is scan in a text file and read "commands" for it line by line, E.g:

Student Mary 12345 19
Student Joe 12346 19
Change Joe 19 20
Change Mary 19 20

So that lines that begin with the word "Student" indicate that I should create a new student file with that name, student ID, and age.

"Change" indicates that I should be changing the specified student's current age to the new age etc.

What I'm currently doing is something along the lines of this:

Scanner input = new Scanner(new FileReader(args[0]));
String[] line;
while (input.nextLine().startsWith("Student")) {
line = input.nextLine().split("s+");

[Code] ....

The problem I'm having is that every second line seems to be getting skipped (because I'm calling nextLine() so much?) but I can't think of a way to "peek" at the first word of each line without advancing past it. Is there any way of doing this?

View Replies View Related

Find A String And Display Line Number And Line Itself?

Oct 20, 2014

I'm creating a program that searches a txt file for a given string, then return the number line and the line itself. However, my testFile class isn't detecting my searchWord methods.

The searchWord and recursiveSearch is written in a java class called BasicFile

public List<String> searchWord(String key) throws Exception {
LineNumberReader lnr = new LineNumberReader(new FileReader(f));
return recursiveSearch(lnr.readLine(), key, lnr);
}
public List<String> recursiveSearch(String currentLineText, String key, LineNumberReader lnr)

[code]....

Is it because I'm using a list instead of a string?

View Replies View Related

Printing Number In Reverse With Subroutine

Feb 21, 2015

The code below is running, but when I enter the numbers it shows up with what is just below.

----jGRASP exec: java ReverseOrder
 
Please enter 5 integers with no spaces in between: 12345
 
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
at java.lang.String.charAt(String.java:646)
at ReverseOrder.reverseDigits(ReverseOrder.java:30)
at ReverseOrder.main(ReverseOrder.java:15)

[code]....

View Replies View Related

Store String Input In Array And Print In Reverse Order

Apr 5, 2014

I have a college question ask me to write a class StringRevert which does the following:

-prompting user for an interger n
-creating an array of n string
-repeatedly read character string from user input and store them in the array until end of array is reached or user input -quit(quit should not saved in array)
-print the string from array in reverse order, from last enter to first enter.
-assume user always supplie correct input

This is what I've done so far but how to get it working.

import java.util.Scanner;
public class StringRevert {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("enter value of n: ");
int n1 = in.nextInt();
String[] n = new String[n1];

[Code] ....

View Replies View Related

Program Won't Go To Result After Input

Feb 19, 2014

I have to create a program that takes a list of student names and grades and then takes the 2 highest scores and reads them, but the result won't show. I'm pretty sure it's because I'm not sure about how to code the last part which should output the results. Here's my code.

import java.util.Scanner;
//Reads input and outputs 2 highest scores
//for loop. nested for.
public class HighestScores {
public static void main(String[] args) {
//create values
String studentName;

[Code] .....

View Replies View Related

Using Result Of Conversion - Number Format Exception

Jul 27, 2014

I want to use the result of the conversion. Can i do that??

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class App2 extends Applet {
Label x,y,z;
TextField a,b,c;
Button p,q,r;

[Code] ....

View Replies View Related

How To Change Value In Array Through User Input And Print Out Result

Dec 14, 2014

I have to do a small program about parking. In the 2-d array (parking lot), contains different kinds of cars, the user have to :

1)enter which car he wanna move,
2)what direction (w,a,s,d) and after that,
3) how many moves( not out of bound) he wants to make, and finally
4) we print out the new parking lot for his next move

And i am stuck at how to move the car to corresponding position and then prompt user for the next move?

For example:

I want to move A to the right by 1 (d)

擷取.PNG
print result:
擷取1.PNG

How do I do that? The code that i have right now

public class CarParkGame {
public static void main( String [] args) {
  String carPark[][] = new String [6][6] ;
carPark[0] = new String[] {"A","A","A","0","0","0"};
carPark[1] = new String[] {"b","0","0","0","0","c"};
carPark[2] = new String[] {"b","X","X","0","0","c","<exit>"};

[Code] .....

View Replies View Related

Retrieving Result By User Input Different Combination Using String Tokenizer (MYSQL)

Mar 30, 2014

I am creating a simple Symptom Checker application. The problem I have is that I'm trying to retrieve user input (JTextField) by comma's using StringTokenizer which contacts the database for a result which matches the user's input (SELECT * FROM DIAGNOSIS WHERE ?, ?, ?) . It successfully finds the correct result however only in a particular format. Not different combinations....

for example, if I enter say within the JTextField: "tearful, nausea, lack of motivation" it will find the result successfully (as that is how it is formatted within the particular column (in the database table) i wish to display a result from) however, if i enter a different combination of these symptoms: "nausea, lack of motivation, tearful" - it will not find any result. I'm very unsure how to make it work regardless of what is inputted first, second or last.

Here is the code:

public void actionPerformed(ActionEvent e) {
try {
String abc = fieldsymp1.getText();
StringTokenizer str = new StringTokenizer(abc);
while (str.hasMoreTokens()) {
str.nextToken((", ")).trim();
 
[Code] ....

View Replies View Related

Double RelativeFrequency Value Which Does Calculation / End Result Is Number With Lots Of Decimal Numbers

Nov 24, 2014

for (int i = 0; i < letters.length; i++) {
double relativeFreq = (onesAverage()/letters[i]);
char a = characters.charAt(i);
double j = score[i];
System.out.printf();//ToDo

Above is my current code. Basically- There is a string called characters of length 26 (1 char for each letter in alphabet), a double relativeFrequency value which does a calculation and the end result is a number with lots of decimal numbers. The score array has set double values within the array.

What I want to do is create a print f statement where I print out each of the values I initialized above WITH specific amounts of spaces in between. So it should be in this order:

a (spaces) j (spaces) relativeFreq (newLine).

Between J and relativeFreq there should be 4 spaces, however if the value in relativeFreq is greater than 10 (it won't be greater than 100)- then there should be 3 spaces. Everytime I try to create a printf statement my code ends up crooked for some of the values.

for (int i = 0; i < letters.length; i++) {
double relativeFreq = (onesAverage()/letters[i]);
char a = characters.charAt(i);
double j = score[i];
System.out.printf("%c %.4s %.4s%n",a, j, relativeFreq);

View Replies View Related

Audit File - How To Add Certain Number Of Spaces For Each Line

Feb 7, 2014

I create a Audit file within my Java program, which gets all the System.out.println lines from the code.

For example, the System.out.println lines in my code are as follows:-

Records initially inserted into edited: 70144

Bad license number records removed: 5

Total records in edited: 70139

Records initially inserted into tabedited:7463

I want all my values aligned after the statement, when they are printed to the Audit File.

Instead of just giving spaces by counting the number of charaters in the line, is there any other way to align them.

View Replies View Related

Read Line Of Input For A File - String Tokenizer

Jan 27, 2015

So I'm having a problem with the .hasMoreElement() method. I try to read lineOfInput for a file which contains:

Hobbit, Long,t@gmail.com,475-555-4444,3
Long, Hobbit,b@yahoo.com,445-222-5342,2
Hobbit,Long,b@yahoo.com,465-222-5342,2
Help,Yerp,,455-222-2222,4

but when i get to the ,, on the last line I get a NoSuchElementException and the program crashes.

StringTokenizer inputField = new StringTokenizer(lineOfInput, ",");
while(inputField.hasMoreElements()) //for each fields is a line
{
lastName = inputField.nextElement().toString();
firstName = inputField.nextElement().toString();
email = inputField.nextElement().toString();
phoneNumber = inputField.nextElement().toString();
level = Integer.parseInt(inputField.nextElement().toString());
}

View Replies View Related

Output Contents Of Input File In Alphabetical Order One Word Per Line

Jun 22, 2014

I am having a lot of trouble with this lab. basically I have to make a text processor to read in code put it into a file and output the contents of the input file in alphabetical order one word per line. I have to reference the string varaibles input_filename and output_filename.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.io.File;
public class TextProcessor {

[Code] ....

these are the guidelines to said lab:

1. In the main method define two Strings called input_filename and output_filename. I will be setting these Strings to my own file names for testing your code, so make sure you get these variable names correct, and that you use them properly in the following steps.
2. Read in the text contents of the file referenced by input_filename.
3. Split the contents of the input text into separate tokens, using whitespace as a delimiter.
4. Lower case the tokens.
5. OPTIONAL: remove punctuation
6. Alphabetize the tokens.
7. Using output_filename, write the alphabetized tokens to an output file, one token per line.
8. Be sure to close file streams - no resource leaks!
9. Use methods to separate functionality in your program where possible

View Replies View Related

User Input All Of Data On A Single Line And Implement StringTokenizer To Assign That Data To File

Mar 21, 2015

By using FileReader, FileWriter and their constituents, I am creating a file to contain employee information ( name, age, hours, etc. ). The user is to input all of the data on a single line and we were asked to implement StringTokenizer to assign that data to the file. I have never used the StringTokenizer before, but I have a rough idea how it is to function. I used pw.println to test what I have so far, now I would like to let the user build the initial file with the "first employees" of the company, and then view the file, and then go back and append new employee data to that same file. My question is, how can I take the user input as a StringTokenizer and add that to the file?

In the for loop below, I thought I would see if it would work, but it does not. The loop only executes once and does not allow me to enter data.

public class Records {
public static void main(String [] args) throws IOException {
Scanner input = new Scanner(System.in);
FileWriter fw = new FileWriter("dbs3.java");
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
System.out.println("NEW EMPLOYEE DATA SHEET");
System.out.print("Number of new employees: ");
int number = input.nextInt();

[Code] ....

View Replies View Related

Read File And Input Each Line Of File Into Array

Mar 21, 2015

I am trying to read a file and input each line of the file into an array. I have not set the array size as I was hoping to fill the array using a while loop. Unfortunately the scope of the array does to work inside the while loop so I am being told that the array 'students' has not been initialised even though it has just outside of the while loop. Is it possible to do what I am trying to without having the array initialised in the while loop as surely the array will be reset every time if it was in the while loop? Here is my code:

public static void students(String file) throws FileNotFoundException {
try {
File studentInfo = new File(file);
Scanner input = new Scanner(studentInfo);
String[] students;

[Code] ....

View Replies View Related

Printing First Number In Input

Mar 21, 2014

Write a Java program ExtractNumber.java to read the data via the keyboard, character by character and print the first number (with or without a decimal point) contained in the input. For example, given the input data: Mary works for $43.75 per hour, the program should print 43.75 to the screen.

When i entered the sentence "The lady works for 8 hours at $10.00 per hour"

ERROR: My output was 810.00 instead of simply the first number 8

View Replies View Related

GUI Program - Convert Input Name To Number

Mar 12, 2015

A GUI program that allows user to enter their name and the program will convert it in number using array.

For example if i input "EUNISE" the output should be 521149195

Because the letter equivalent of

a = 1
b = 2
c = 3
d = 4
e = 5
f = 6
g = 7
h = 8
i = 9
j = 10
k = 11
l = 12
m = 13
n= 14
o = 15
p = 16
q = 17
r= 18
s = 19
t = 20
u = 21
v = 22
w = 23
x = 24
y = 25
z = 26

View Replies View Related

Program To Add Space Between Input Number

Nov 2, 2014

I am writing a program where whatever number I input in my scanner, it will space them...

For example: If I input 456, the output should be 4 5 6... here is my code:

import java.util.Scanner;
public class midterm {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
String number = "";

[Code] ....

the current output for this program is:
456 456 456

View Replies View Related

Input Even Number Then It Will Be Outputted Invalid

Dec 7, 2014

import java.io.*;
public class MagicSquare
{
public static void main(String[]args)throws IOException
{
BufferedReader in=new BufferedReader (new InputStreamReader(System.in));
System.out.print("Enter a Magic Number: ");
int number=Integer.parseInt(in.readLine());

[Code] ....

//where should i enter a even number that will be outputted "invalid"
//and it only should accept odd numbers

View Replies View Related

JUnit Test - Read From Text Line By Line And Save Words In FileOnTable

Nov 21, 2014

I have wrote this class who read from text line by line and save the words in fileOnTable.. Now i don't know what to read in ReadOffer to save the words in object offers and return this.. One more question.. What JUnit test can write for this code..?

package com.example.crazysellout.UserSide;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

[Code] ....

View Replies View Related

Counting Number Of Words From User Input

Jan 2, 2015

I tried the following code, OK:

Java Code:

String temp = "hi this sf hello is new what is this";
String[] cmd = temp.split("s");
int num = cmd.length;
System.out.println("number of words are: "+num); mh_sh_highlight_all('java');

However, when i get the input from user , i didnt get the expected result:

Java Code:

System.out.println("Enter the input string to count the words: ");
String[] cmd = new Scanner(System.in).next().trim().split("s");
int num = cmd.length;
System.out.println("number of words are: "+num); mh_sh_highlight_all('java');

Now Result for above code:

Enter the string to count the words:

hi this is new

words are: 1

View Replies View Related

Allow Input To Loop Until 0 Is Entered For Product Number

Sep 15, 2014

What I am trying to do here is allow input to loop until 0 is entered for the product number. When 0 is entered, it should then dump the total for each individual product. I've tried it about a dozen different ways and have yet to be able to get the loop to function as intended. The way I have the code below, the loop will not function at all (where as before it looped, but never finished).

import java.util.Scanner;
public class Sales {
public static void main(String[] args) {
double total1=0.0;
double total2=0.0;
double total3=0.0;
double total4=0.0;
double total5=0.0;
int product;

[Code] ......

View Replies View Related







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